passing filename path with spaces to subs

N

news2003

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
 
S

Sandy

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/
 
B

Ben Bullock

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.
 
J

Jürgen Exner

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-

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
 
S

szr

Jürgen Exner said:
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.


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().
 
N

news2003

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.



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.
 
B

Ben Bullock

% 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top