On 06/08/2011 15:14, Emeka wrote:
John,
Thanks for making things pretty simple for mere mortals ..
Hi Emeka
chomp( my $raw_file = glob "@ARGV" );
I am of the view that glob sub is used for as tree (that is to get all the
files in a folder and all its sub-folders. From the above, it seems like it
could be used for something else... Someone should help me out here.
my @file_list = glob "@ARGV";
is a simple way of getting a list of all files that match the list of
filename patterns passed on the command line. But in scalar context (as
Timothy originally posted) it will fetch only the first file in that
list, and it is wrong to chomp it as it is not terminated by an
additional newline.
You are mostly correct, except that glob will not search a directory
tree of files. You can use wildcards, such as
glob '~/*/*'
which will list all files in any directory immediately within the home
directory, but to search throughout a directory tree of arbitrary depth
you need File::Find or something similar.
That is all glob does. It is of no use in any other way. What can be
confusing is that <*.pl> calls glob '*.pl' whereas <filehandle> calls
readline filehandle.
Take a look at
perldoc -f glob
and
perldoc File::Glob
(which is the module that implements the glob operator).
The use of $&, $' and $` will slow down *ALL* regular expressions in the
program. Better to just use capturing parentheses.
if (/^(\d{4})/ ) { $yr = $1 } # get the year
if ( /^([A-Z].+)/ ) {
$cat = $1; # get the Category
$cat = join "", split /,/, $cat; # remove the comma in front
$ln.= " $yr: " . $cat; # add both the year and Category
}
if ( /(.*?)\--.+/ ) { $win = $1; # get the winner
What is the idiomatic Perl , $1 or $[`,&,'] ? And what makes [$&, $', $`]
to slow down *ALL* regular expressions in the program.
perldoc prelre says this:
WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in
the program, it has to provide them for every pattern match. This may
substantially slow your program. Perl uses the same mechanism to produce
$1, $2, etc, so you also pay a price for each pattern that contains
capturing parentheses.
so no recent well-written code will use $& etc. although they are still
available for backward compatability.
Reading the same source again, it also says this
So avoid $&, $', and $` if you can, but if you can't (and some
algorithms really appreciate them), once you've used them once, use
them at will, because you've already paid the price.
so there is a niche for their continued use, but I have never come
across anything that isn't better written using captures.
HTH,
Rob
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/