Click here to get back home

Re: passing filename path with spaces to subs

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: passing filename path with spaces to subs Jürgen Exner 03-24-2008
Posted by Jürgen Exner on March 24, 2008, 2:18 am
Please log in for more thread options
news2003@wanadoo.es 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

Posted by szr on March 24, 2008, 3:15 am
Please log in for more thread options
Jürgen Exner wrote:
> news2003@wanadoo.es 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



Posted by news2003 on March 24, 2008, 5:13 am
Please log in for more thread options
> news2...@wanadoo.es 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 =3D=3D=3D=3D=3D=3D=3D1=3D=3D=3D=3D=3D=3D=3D=3D
sandbox/noSpaces/63.jpg
=3D=3D=3D=3D=3D=3D=3D2=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D=3D=3D=3D=3D=3D=3D1=3D=3D=3D=3D=3D=3D=3D=
=3D
sandbox/with\ Spaces/63.jpg
=3D=3D=3D=3D=3D=3D=3D2=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D shift;

print "=3D=3D=3D=3D=3D=3D=3D1=3D=3D=3D=3D=3D=3D=3D=3D\n";
print "$file\n";
print "=3D=3D=3D=3D=3D=3D=3D2=3D=3D=3D=3D=3D=3D=3D=3D\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 =3D "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 =3D=3D=3D=3D=3D=3D=3D1=3D=3D=3D=3D=3D=3D=3D=
=3D
sandbox/with Spaces/63.jpg
=3D=3D=3D=3D=3D=3D=3D2=3D=3D=3D=3D=3D=3D=3D=3D
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 =3D=3D=3D=3D=3D=3D=3D1=3D=3D=3D=3D=3D=3D=3D=
=3D
sandbox/with Spaces/63.jpg
=3D=3D=3D=3D=3D=3D=3D2=3D=3D=3D=3D=3D=3D=3D=3D
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.

Posted by Ben Bullock on March 24, 2008, 6:24 am
Please log in for more thread options
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.

Similar ThreadsPosted
Re: passing filename path with spaces to subs March 23, 2008, 11:22 pm
globbing files with spaces in the path January 6, 2006, 4:57 pm
File-Find skips directories with spaces depending on path separator on Windows January 14, 2006, 8:20 pm
Exec/System call with spaces in exec path problem ... December 2, 2005, 1:41 pm
Subs: Returning two hashes January 8, 2005, 1:52 am
embedded perl path $ENV problem September 22, 2004, 4:29 pm
Carp & parameters passed to subs March 24, 2006, 4:41 am
REs Returning undef Not Being Passed to Subs April 6, 2006, 1:26 pm
B::Lint does not detect undefined subs August 7, 2006, 12:17 pm
lists,shift,subs and dumb question November 5, 2004, 5:23 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap