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
With your updated code, I can see where your error lies. While
Works as a
globIs interpreted as a readline() on the file handle
$dirname.If you want to avoid ambiguity you can use the function for
glob: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
…is a very bad idea. These two pragmas will help you identify problems with your code.