IanW said:
I see the way you've done it in the modified code below, however I didn't
think there was anything wrong with a few global scope vars as long as you
don't forget you've used them globally and then try and use the same names
in another unrelated part of the script... but it's not a huge script and I
can keep track of those things easily enough.
You've just listed two conditionals that aren't especially guaranteed,
and given the proviso that your reasoning is only valid if the script's
size remains as it is now. This paragraph sounds a lot more like an
argument *against* doing it the way you did rather than *for*.
I don't quite get the reasoning behind using poor programming practices
for "quick and dirty" scripts. Why not just do things the "right" way
each time? Programming definately involves developing habbits. It's
much better, in my opinion, to use short scripts to develop *good*
programming habbits.
lexical is one of those words that I've never got my head round in
programming terms,
Lexical, at least as far as Perl is concerned at any rate, simply means
"scope exists only in the physical block in which it was declared". If
a variable is declared within a block, it is visible only in that
block, regardless of any other subroutines or control paths called from
within that block. (Contrast with dynamic scope (such as with local)
in which the scope of the temporary value extends to any subroutines
called from within the same block as the declaration).
Must admit I get a bit lazy in CGI scripts with that, because to be
user-friendly, it means more than just adding "die..." bit to the end of the
open line.
Er, does that mean it's more user friendly to let the program attempt
to read from or write to a possibly-closed filehandle? ;-)
I've also never come across a directory or file that wouldn't
open on any of my scripts..
Again, this goes back to developing the right kinds of habbits. Just
because you haven't encountered an error yet is no reason not to guard
against that error in the future.
is there any reason for doing it that way over my original line using a
regexp? is it a performance thing?
Performance aside, I think this is more readable than the regexp
equivalent. However. . .
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw/cmpthese/;
sub re {
my @files;
opendir my $dh, '.' or die "Cannot open current directory: $!";
while (my $file = readdir ($dh)){
next if $file =~ /^\.{1,2}$/;
push @files, $file;
}
}
sub eqor {
my @files;
opendir my $dh, '.' or die "Cannot open current directory: $!";
while (my $file = readdir ($dh)){
next if $file eq '.' or $file eq '..';
push @files, $file;
}
}
cmpthese(10000, {Regexp=>\&re, Equality=>\&eqor} );
__END__
Benchmark: timing 10000 iterations of Equality, Regexp...
Equality: 15 wallclock secs (12.72 usr + 2.44 sys = 15.16 CPU) @
659.63/s (n=10000)
Regexp: 16 wallclock secs (13.20 usr + 2.64 sys = 15.84 CPU) @
631.31/s (n=10000)
Rate Regexp Equality
Regexp 631/s -- -4%
Equality 660/s 4% --
Obviously, a rather miniscule benefit...
Would the following work, a a shortened version of those 3 lines?
($fcount, $dcount, $fsize) += ($fc, $dc, $fs);
Why ask if something would work? Why not try it for yourself and see?
(The answer is "no", however. += expects a scalar on each side. Read
perldoc perlop to see what the comma operator does in scalar context,
and see if you can use that to predict the results).
For syntax similar to what you'd like that to do, check out the
pairwise() function in the List::MoreUtils module from CPAN
Paul Lalli