Reputation: 849
I have a subroutine in Perl sub findfiles , I have to pass a quoted value "*/*" as input parameter since it complains without quoting ,on the other hand in my subroutine I needed it to be unquoted (may be!)The problem is when I print the value to check ,I don't see any quote,or any thing but may be there are some special hidden character or something I don't know ? My codes work properly when I use */*directly but not when I pass it as as an input parameter Do you have any idea?
sub findfiles {
$dirname=$_[0];
my @temp = grep {-f} <$dirname>;
print @temp;
}
&findfiles("*/*"); doesnot work
but
sub findfiles {
$dirname=$_[0];
my @temp = grep {-f} <*/*>;
print @temp;
}
does its job
Upvotes: 2
Views: 150
Reputation: 67908
With your updated code, I can see where your error lies. While
my @temp = grep {-f} <*/*>;
Works as a glob
my @temp = grep {-f} <$dirname>;
Is interpreted as a readline() on the file handle $dirname
.
If you want to avoid ambiguity you can use the function for glob
:
my @temp = grep -f, glob $dirname;
You might also be interested in using File::Find, which finds files recursively.
NOTE: This problem could have been avoided if you had warnings turned on. As a rule of thumb, coding in perl without using
use strict;
use warnings;
...is a very bad idea. These two pragmas will help you identify problems with your code.
Upvotes: 8
Reputation: 29854
Do you know about File::Find
?
use File::Find ();
File::Find::find( sub { say $File::Find::name if -f; } => $my_root );
Or what about File::Find::Rule
(see file
)?
say foreach File::Find::Rule->file->in( $my_root );
Upvotes: 1
Reputation: 943591
The problem is when I print the value to check ,I don't see any quote
$test="*/*"
^string delimiter
^^^string
^string delimiter
When you print a string (be it from a string literal, a scalar or whatever) you print the string.
The delimiters don't get printed. They just tell perl where the edges of the data are.
Upvotes: 2