passing filename path with spaces to subs

Discussion in 'Perl Misc' started by news2003@wanadoo.es, Mar 24, 2008.

  1. Guest

    I have a basic question. It has to do with the way I pass files with
    spaces. I have two versions of the same code. once calls $exifTool-
    >ExtractInfo($file) directly as argument from the command line.


    perl extract.pl /home/john/pictures 2/15.jpg

    with the code like:

    my $file = $ARGV[0];

    # Create a new Image::ExifTool object
    my $exifTool = new Image::ExifTool;

    # Extract meta information from image
    $exifTool->ExtractInfo($file);

    The second version is taking the code from a file
    perl extract2.pl fdir.output.txt

    being the code in this case:

    open( FILE, "< $filename" ) or die "Can't open $filename : $!";

    foreach my $line ( <FILE> ) {
    chomp ($line);
    print $line."\t";
    extractInfo("$line");
    }

    close FILE;

    # read information on files
    sub extractInfo(){
    my $file = "@_";

    # Create a new Image::ExifTool object
    my $exifTool = new Image::ExifTool;

    # Extract meta information from image
    #$exifTool->ExtractInfo($file);

    the input file on the second case is like this
    /home/john/pictures/49.jpg
    /home/john/pictures 2/15.jpg

    The first file works fine but the second fails, I reckon is due to the
    spacein the name. It seems that when I call the script with the file
    name as argument works fine (independently of the presence of the
    space) but when I try to back the process using a file with the file
    location the process fails if the file name contains spaces on the
    path. My Linux box parse the file fine but not when it comes directly
    from the file. I have tried to escape the space with \ but get the
    same result.

    Any ideas?
    Thanks in advance for your help
    John
    , Mar 24, 2008
    #1
    1. Advertising

  2. Sandy Guest

    On Mar 23, 6:09 pm, wrote:
    > I have a basic question. It has to do with the way I pass files with
    > spaces.


    This has nothing to do with Perl. Just quote your argument so the
    shell would not break it into two arguments:

    --- t.pl ---
    #!/usr/bin/perl -w
    use strict;
    my $first = $ARGV[0];
    my $second = $ARGV[1] || '';

    print "First: '$first'\n";
    print "Second: '$second'\n";
    ---

    no quotes
    > ./t.pl blah blah

    First: 'blah'
    Second: 'blah'

    quotes
    > ./t.pl "blah blah"

    First: 'blah blah'
    Second: ''

    /sandy
    http://myperlquiz.com/
    Sandy, Mar 24, 2008
    #2
    1. Advertising

  3. Ben Bullock Guest

    On Mar 24, 12:22 pm, Sandy <> wrote:
    > On Mar 23, 6:09 pm, wrote:
    >
    > > I have a basic question. It has to do with the way I pass files with
    > > spaces.

    >
    > This has nothing to do with Perl.


    Excuse me for saying so, Sandy, but I'm not sure whether you read the
    original poster's question carefully. His problem is with the second
    script, and it is a purely Perl problem.
    Ben Bullock, Mar 24, 2008
    #3
  4. wrote:
    >I have a basic question. It has to do with the way I pass files with
    >spaces. I have two versions of the same code. once calls $exifTool-
    >>ExtractInfo($file) directly as argument from the command line.

    >
    >perl extract.pl /home/john/pictures 2/15.jpg


    Your shell will break this line down into 4 elements:
    - perl
    - extract.pl
    - /home/john/pictures
    - 2/15.jpg

    If you want only three elements then you will have to tell your shell that
    the last space is part of the third element rather than a separator,
    typically by enclosing the element in quotes.

    >being the code in this case:
    >
    >open( FILE, "< $filename" ) or die "Can't open $filename : $!";


    I can't reproduce your problem. This line opens a file just fine, even if
    the name contains a space.

    jue
    Jürgen Exner, Mar 24, 2008
    #4
  5. szr Guest

    Jürgen Exner wrote:
    > wrote:
    >> I have a basic question. It has to do with the way I pass files with
    >> spaces. I have two versions of the same code. once calls $exifTool-
    >>> ExtractInfo($file) directly as argument from the command line.

    >>
    >> perl extract.pl /home/john/pictures 2/15.jpg

    >
    > Your shell will break this line down into 4 elements:
    > - perl
    > - extract.pl
    > - /home/john/pictures
    > - 2/15.jpg
    >
    > If you want only three elements then you will have to tell your shell
    > that the last space is part of the third element rather than a
    > separator, typically by enclosing the element in quotes.
    >
    >> being the code in this case:
    >>
    >> open( FILE, "< $filename" ) or die "Can't open $filename : $!";

    >
    > I can't reproduce your problem. This line opens a file just fine,
    > even if the name contains a space.


    It would be much better to use the 3 argument open(), that way if you
    have an trialing (or leading) spaces (or mode characters) in the
    filename, you wont run into the gotchas of the two arg open(). You might
    also want to take a look at sysopen().

    --
    szr
    szr, Mar 24, 2008
    #5
  6. Guest

    On 24 Mar, 06:18, Jürgen Exner <> wrote:
    > wrote:
    > >I have a basic question. It has to do with the way I pass files with
    > >spaces. I have two versions of the same code. once calls $exifTool-
    > >>ExtractInfo($file) directly as argument from the command line.

    >
    > >perl extract.pl /home/john/pictures 2/15.jpg

    >
    > Your shell will break this line down into 4 elements:
    > - perl
    > - extract.pl
    > - /home/john/pictures
    > - 2/15.jpg
    >
    > If you want only three elements then you will have to tell your shell that
    > the last space is part of the third element rather than a separator,
    > typically by enclosing the element in quotes.
    >
    > >being the code in this case:

    >
    > >open( FILE, "< $filename" ) or die "Can't open $filename : $!";

    >
    > I can't reproduce your problem. This line opens a file just fine, even if
    > the name contains a space.


    Thanks for the answers.

    It's definitely in the version with spaces calling from a file not
    from the @argv[0].


    here my modification.

    % cat test_spaces.txt

    sandbox/noSpaces/63.jpg
    sandbox/with\ Spaces/63.jpg

    the result of the scrip calling this list.
    % perl extract2.pl test_spaces.txt
    sandbox/noSpaces/63.jpg =======1========
    sandbox/noSpaces/63.jpg
    =======2========
    63.jpg 126 kB JPEG 2007:05:12 18:47:54 Canon Canon EOS 350D
    DIGITAL image/jpeg Horizontal (normal)72 72 inches
    1/60 4.0 Program AE 400 0 Evaluative Auto,
    Fired, Red-eye reduction 21.0 mm Normal AI Focus AF
    Auto 557-5800 799 533 799x533 18.0 - 200.0 mm
    2007 05 12 18 47 54
    sandbox/with\ Spaces/63.jpg =======1========
    sandbox/with\ Spaces/63.jpg
    =======2========
    63.jpg
    %

    in the version called from the command line.

    % perl extract.pl sandbox/noSpaces/63.jpg
    63.jpg 126 kB JPEG 2007:05:12 18:47:54 Canon Canon EOS 350D
    DIGITAL image/jpeg Horizontal (normal)72 72 inches
    1/60 4.0 Program AE 400 0 Evaluative Auto,
    Fired, Red-eye reduction 21.0 mm Normal AI Focus AF
    Auto 557-5800 799 533 799x533 18.0 - 200.0 mm
    2007 05 12 18 47 54
    % perl extract.pl sandbox/with\ Spaces/63.jpg
    63.jpg 126 kB JPEG 2007:05:12 18:47:54 Canon Canon EOS 350D
    DIGITAL image/jpeg Horizontal (normal)72 72 inches
    1/60 4.0 Program AE 400 0 Evaluative Auto,
    Fired, Red-eye reduction 21.0 mm Normal AI Focus AF
    Auto 557-5800 799 533 799x533 18.0 - 200.0 mm
    2007 05 12 18 47 54
    %

    I added the print lines to the failing scripts to see what was the
    cause.

    sub extractInfo(){
    my $file = shift;

    print "=======1========\n";
    print "$file\n";
    print "=======2========\n";


    In both cases seems to be the same.

    Notice that I backslashed the version with spaces to avoid the space
    failing in $argv[0]

    the interesting thing is that if I replace the failing code with:

    open( FILE, "< $filename" ) or die "Can't open $filename : $!";

    foreach my $line ( <FILE> ) {
    chomp ($line);
    $line = "sandbox/with\ Spaces/63.jpg";
    print $line."\t";
    extractInfo("$line");
    }

    the code works fine, although always for the same file.

    % perl extract2.pl test_spaces.txt
    sandbox/with Spaces/63.jpg =======1========
    sandbox/with Spaces/63.jpg
    =======2========
    63.jpg 126 kB JPEG 2007:05:12 18:47:54 Canon Canon EOS 350D
    DIGITAL image/jpeg Horizontal (normal)72 72 inches
    1/60 4.0 Program AE 400 0 Evaluative Auto,
    Fired, Red-eye reduction 21.0 mm Normal AI Focus AF
    Auto 557-5800 799 533 799x533 18.0 - 200.0 mm
    2007 05 12 18 47 54
    sandbox/with Spaces/63.jpg =======1========
    sandbox/with Spaces/63.jpg
    =======2========
    63.jpg 126 kB JPEG 2007:05:12 18:47:54 Canon Canon EOS 350D
    DIGITAL image/jpeg Horizontal (normal)72 72 inches
    1/60 4.0 Program AE 400 0 Evaluative Auto,
    Fired, Red-eye reduction 21.0 mm Normal AI Focus AF
    Auto 557-5800 799 533 799x533 18.0 - 200.0 mm
    2007 05 12 18 47 54

    so it has to be in the way $line is pased to the sub extractInfo but I
    can't see how.
    , Mar 24, 2008
    #6
  7. Ben Bullock Guest

    On Mon, 24 Mar 2008 02:13:44 -0700, news2003 wrote:


    > % cat test_spaces.txt
    >
    > sandbox/noSpaces/63.jpg
    > sandbox/with\ Spaces/63.jpg


    ....

    > sub extractInfo(){
    > my $file = shift;
    >
    > print "=======1========\n";
    > print "$file\n";
    > print "=======2========\n";


    #!/usr/local/bin/perl5.10.0

    use warnings;
    use strict;

    # read information on files
    sub extractInfo()
    {
    my $file = shift;
    print "$file\n";
    }

    my $filename = "testspace.txt";

    open(FILE, "<", $filename) or die "Can't open $filename : $!";
    foreach my $line ( <FILE> ) {
    chomp ($line);
    print $line,"\n";
    extractInfo("$line");
    }

    close FILE;

    __END__

    $ ./exif.pl
    Too many arguments for main::extractInfo at ./exif.pl line 19, near
    ""$line")"
    Execution of ./exif.pl aborted due to compilation errors.

    Remove ():

    $ ./exif.pl
    sandbox/noSpaces/63.jpg
    sandbox/noSpaces/63.jpg
    sandbox/with\ Spaces/63.jpg
    sandbox/with\ Spaces/63.jpg

    Perl 5.8 gives the same results.
    Ben Bullock, Mar 24, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    996
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,417
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,677
    alok000707
    Jul 13, 2010
  4. Michael Robertson

    open filename with spaces in path

    Michael Robertson, May 6, 2008, in forum: Python
    Replies:
    6
    Views:
    911
    Chris
    May 7, 2008
  5. Justin C
    Replies:
    18
    Views:
    149
Loading...

Share This Page