Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Dynamic directory handles?

Reply
Thread Tools

Dynamic directory handles?

 
 
IanW
Guest
Posts: n/a
 
      12-22-2005
I have a chunk of code that counts files.dirs and size for a directory tree.
It goes like this:

================
#use strict;

my $basedir = "j:/files/sw-test";
my $fcount = 0;
my $fsize = 0;
my $dcount = 0;
dircount();

sub dircount {
my($cdir) = shift;
$cdir .= "/" if $cdir ne "";
my $dh = "DH" . length($cdir);
opendir($dh,"$basedir/$cdir");
while(my $fl = readdir($dh)){
next if $fl =~ /^\.{1,2}$/;
if(-d "$basedir/$cdir$fl"){
$dcount++;
dircount("$cdir$fl");
}
else{
$fcount++;
$fsize += -s "$basedir/$cdir$fl";
}
}
close($dh);
}

print "$fcount files and $dcount directories totalling $fsize bytes in
size";
================

If I "use strict" it says "Can't use string ("DH0") as a symbol ref while
"strict refs" in use at D:\test.pl line 14". What's the best way to get
round this, since I need a dynamic dir handle for the routine to work
properly.

Thanks
Ian


 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-22-2005
"IanW" <(E-Mail Removed)> wrote in
news:doe5kj$ms$(E-Mail Removed):

> I have a chunk of code that counts files.dirs


First off, you are better off doing this using the File::Find module
rather than using recursion. If this is not a learning exercise, then I
would also urge you to look at File::Find::Rule to simplify matters when
processing items one by one.

> ================
> #use strict;
>
> my $basedir = "j:/files/sw-test";


The program should check @ARGV for an argument, and supply a reasonable
default if one is not present.

> my $fcount = 0;
> my $fsize = 0;
> my $dcount = 0;


These are values that should be returned by your sub. You might want to
check for calling context in the sub and supply an appropriate scalar
value.

> dircount();
>
> sub dircount {
> my($cdir) = shift;
> $cdir .= "/" if $cdir ne "";
> my $dh = "DH" . length($cdir);


Using lexical dirhandles, this should not be necessary.

> opendir($dh,"$basedir/$cdir");


You should *always* check if calls to open/opendir succeeded.

> while(my $fl = readdir($dh)){
> next if $fl =~ /^\.{1,2}$/;
> if(-d "$basedir/$cdir$fl"){
> $dcount++;
> dircount("$cdir$fl");
> }


I do prefer using File::Spec for path manipulation.

> If I "use strict" it says "Can't use string ("DH0") as a symbol ref
> while "strict refs" in use at D:\test.pl line 14". What's the best way
> to get round this, since I need a dynamic dir handle for the routine
> to work properly.


Here is a revised version of your code. I cannot vouch for its accuracy,
since there seems to be discrepancy between the output of this script
and that of du on my system (probably because the space taken by zero-
length files is not taken to account). As always, corrections welcome:

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec::Functions qw(canonpath catfile);

my $basedir = canonpath($ARGV[0] || '.');
my ($fcount, $dcount, $fsize) = dircount($basedir);


printf("%d files and %d directories totalling %d bytes in size\n",
$fcount, $dcount, $fsize);

sub dircount {
my ($cdir) = @_;
my ($fcount, $dcount, $fsize) = (0, 0, 0);
my $path = catfile($basedir, $cdir);
opendir my $dh, $path or die "Cannot open directory '$path': $!";
while (my $fl = readdir($dh)){
next if $fl eq '.' or $fl eq '..';
if(-d (my $d = catfile($path, $fl))){
$dcount++;
my ($fc, $dc, $fs) = dircount($d);
$fcount += $fc;
$dcount += $dc;
$fsize += $fs;
} else {
$fcount++;
$fsize += -s $d;
}
}
close($dh);
return ($fcount, $dcount, $fsize);
}





--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      12-22-2005
IanW <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> I have a chunk of code that counts files.dirs and size for a directory tree.
> It goes like this:
>
> ================
> #use strict;
>
> my $basedir = "j:/files/sw-test";
> my $fcount = 0;
> my $fsize = 0;
> my $dcount = 0;
> dircount();
>
> sub dircount {
> my($cdir) = shift;
> $cdir .= "/" if $cdir ne "";
> my $dh = "DH" . length($cdir);
> opendir($dh,"$basedir/$cdir");
> while(my $fl = readdir($dh)){
> next if $fl =~ /^\.{1,2}$/;
> if(-d "$basedir/$cdir$fl"){
> $dcount++;
> dircount("$cdir$fl");
> }
> else{
> $fcount++;
> $fsize += -s "$basedir/$cdir$fl";
> }
> }
> close($dh);
> }
>
> print "$fcount files and $dcount directories totalling $fsize bytes in
> size";
> ================
>
> If I "use strict" it says "Can't use string ("DH0") as a symbol ref while
> "strict refs" in use at D:\test.pl line 14". What's the best way to get
> round this, since I need a dynamic dir handle for the routine to work
> properly.


Just leave $dh undefined instead of setting it to a string value.
opendir() will then create an anonymous directory handle. So change

my $dh = "DH" . length($cdir);

to

my $dh;

Also, you call dircount() without an argument. Presumably you wanted
to say

dircount( $basedir);

A better solution would be to use the standard module File::Find:

use File::Find;

sub dircount {
my $cdir = shift;
find sub {
if ( -d ) {
++ $dcount;
} else {
++ $fcount;
$fsize += -s;
}
}, $cdir;
}

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      12-22-2005
IanW <(E-Mail Removed)> wrote:

> I have a chunk of code that counts files.dirs and size for a directory tree.



You have a whole bunch of problems, some big, some small.

I'll mention the lesser ones in comments about your code below,
but the 3 big ones are:

1) You should always enable warnings (and strict) when
developing Perl code.

2) You get a dynamic dirhandle the same way you get a dynamic
filehandle, so:

perldoc -q filehandle

How can I make a filehandle local to a subroutine? How do I pass file-
handles between subroutines? How do I make an array of filehandles?

3) There is an already-invented (and tested) wheel for doing
recursive directory searching, the File::Find module.

You can read the module's docs with:

perldoc File::Find


>================
> #use strict;



You lose all of the benefits of that statement when you comment it out!


> my $basedir = "j:/files/sw-test";
> my $fcount = 0;
> my $fsize = 0;
> my $dcount = 0;
> dircount();
>
> sub dircount {
> my($cdir) = shift;



$cdir will be undef for the top-level call.


> $cdir .= "/" if $cdir ne "";



You have all of the path components separated already, so I'd
paste the dir separator in myself on each usage instead of
burying one inside of a variable's value.


> my $dh = "DH" . length($cdir);
> opendir($dh,"$basedir/$cdir");



You get a dynamic dirhandle when the variable is undef.

Your variable is not undef, so there is no dynamic dirhandle...


You should always check the return value to see if you actually
got what you asked for:

opendir($dh,"$basedir/$cdir") or die "could not open '$basedir/$cdir' $!";


> while(my $fl = readdir($dh)){
> next if $fl =~ /^\.{1,2}$/;
> if(-d "$basedir/$cdir$fl"){
> $dcount++;
> dircount("$cdir$fl");



If $fl is a symlink to a "higher" directory, then your
code will go into an infinite loop here.



Applying the minimum changes to fix (IMO) your code, I get:

------------------------
#!/usr/bin/perl
use warnings;
use strict;

my $basedir = '/home/tadmc/temp';
my $fcount = 0;
my $fsize = 0;
my $dcount = 0;
dircount();

sub dircount {
my($cdir) = shift || '';
opendir(my $dh,"$basedir/$cdir") or die "could not open dir $!";
while(my $fl = readdir($dh)){
next if $fl =~ /^\.{1,2}$/;
if(-d "$basedir/$cdir/$fl"){
$dcount++;
dircount("$cdir/$fl");
}
else{
$fcount++;
$fsize += -s "$basedir/$cdir/$fl";
}
}
close($dh);
}

print "$fcount files and $dcount directories totalling $fsize bytes in size\n";
------------------------



Recasting it to use the tried-and-true module, I get:

------------------------
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;

my $basedir = '/home/tadmc/temp';
my $fcount = 0;
my $fsize = 0;
my $dcount = 0;
find( \&dircount, $basedir );

sub dircount {
return if $_ eq '.' or $_ eq '..';
$dcount++ if -d;
return unless -f; # only care about plain files at this point
$fcount++;
$fsize += -s;
}

print "$fcount files and $dcount directories totalling $fsize bytes in size\n";
------------------------


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      12-22-2005

"A. Sinan Unur" <(E-Mail Removed)> wrote in message
news:Xns97344FEAA98EDasu1cornelledu@127.0.0.1...

> First off, you are better off doing this using the File::Find module
> rather than using recursion. If this is not a learning exercise, then I
> would also urge you to look at File::Find::Rule to simplify matters when
> processing items one by one.
>
>> ================
>> #use strict;
>>
>> my $basedir = "j:/files/sw-test";

>
> The program should check @ARGV for an argument, and supply a reasonable
> default if one is not present.


It will actually be a subroutine that forms part of a larger CGI script and
the basedir will actually be passed from a form field.

>> my $fcount = 0;
>> my $fsize = 0;
>> my $dcount = 0;

>
> These are values that should be returned by your sub. You might want to
> check for calling context in the sub and supply an appropriate scalar
> value.


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.

>> dircount();
>>
>> sub dircount {
>> my($cdir) = shift;
>> $cdir .= "/" if $cdir ne "";
>> my $dh = "DH" . length($cdir);

>
> Using lexical dirhandles, this should not be necessary.


lexical is one of those words that I've never got my head round in
programming terms, but I see in the example you've not given $dh a value,
which ties in with what Anno says about opendir creating an anonymous
handle.

>> opendir($dh,"$basedir/$cdir");

>
> You should *always* check if calls to open/opendir succeeded.


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. I've also never come across a directory or file that wouldn't
open on any of my scripts..

>> while(my $fl = readdir($dh)){
>> next if $fl =~ /^\.{1,2}$/;
>> if(-d "$basedir/$cdir$fl"){
>> $dcount++;
>> dircount("$cdir$fl");
>> }

>
> I do prefer using File::Spec for path manipulation.
>
>> If I "use strict" it says "Can't use string ("DH0") as a symbol ref
>> while "strict refs" in use at D:\test.pl line 14". What's the best way
>> to get round this, since I need a dynamic dir handle for the routine
>> to work properly.

>
> Here is a revised version of your code. I cannot vouch for its accuracy,
> since there seems to be discrepancy between the output of this script
> and that of du on my system (probably because the space taken by zero-
> length files is not taken to account). As always, corrections welcome:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use File::Spec::Functions qw(canonpath catfile);
>
> my $basedir = canonpath($ARGV[0] || '.');
> my ($fcount, $dcount, $fsize) = dircount($basedir);
>
>
> printf("%d files and %d directories totalling %d bytes in size\n",
> $fcount, $dcount, $fsize);
>
> sub dircount {
> my ($cdir) = @_;
> my ($fcount, $dcount, $fsize) = (0, 0, 0);


ahh yes of course I was thinking of sth along those lines, though thought
I might have to use qw//

> my $path = catfile($basedir, $cdir);
> opendir my $dh, $path or die "Cannot open directory '$path': $!";
> while (my $fl = readdir($dh)){
> next if $fl eq '.' or $fl eq '..';


is there any reason for doing it that way over my original line using a
regexp? is it a performance thing?

> if(-d (my $d = catfile($path, $fl))){
> $dcount++;
> my ($fc, $dc, $fs) = dircount($d);
> $fcount += $fc;
> $dcount += $dc;
> $fsize += $fs;


Would the following work, a a shortened version of those 3 lines?

($fcount, $dcount, $fsize) += ($fc, $dc, $fs);

> } else {
> $fcount++;
> $fsize += -s $d;
> }
> }
> close($dh);
> return ($fcount, $dcount, $fsize);
> }


thanks

Ian



 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      12-22-2005
> Just leave $dh undefined instead of setting it to a string value.
> opendir() will then create an anonymous directory handle. So change
>
> my $dh = "DH" . length($cdir);
>
> to
>
> my $dh;


I like that solution

> Also, you call dircount() without an argument. Presumably you wanted
> to say
>
> dircount( $basedir);


well, $basedir is a global var and I put it in all the places it's needed
anyway

> A better solution would be to use the standard module File::Find:
>
> use File::Find;
>
> sub dircount {
> my $cdir = shift;
> find sub {
> if ( -d ) {
> ++ $dcount;
> } else {
> ++ $fcount;
> $fsize += -s;
> }
> }, $cdir;
> }


that's very concise, thanks! I looked at the File:Find module docs before
but the document made my eyes glaze over. I suppose it's one of those
modules that really useful once you've taken teh time to plow through the
docs and understand it properly.

Regards
Ian



 
Reply With Quote
 
IanW
Guest
Posts: n/a
 
      12-22-2005

"Tad McClellan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> IanW <(E-Mail Removed)> wrote:
>
>> I have a chunk of code that counts files.dirs and size for a directory
>> tree.

>
>
> You have a whole bunch of problems, some big, some small.


oh dear.. I was worried that might happen!

> I'll mention the lesser ones in comments about your code below,
> but the 3 big ones are:
>
> 1) You should always enable warnings (and strict) when
> developing Perl code.
>
> 2) You get a dynamic dirhandle the same way you get a dynamic
> filehandle, so:
>
> perldoc -q filehandle
>
> How can I make a filehandle local to a subroutine? How do I pass
> file-
> handles between subroutines? How do I make an array of filehandles?
>
> 3) There is an already-invented (and tested) wheel for doing
> recursive directory searching, the File::Find module.


The only thing that sometimes puts me off using modules for relatively
simple things like this, is that I wonder how much extra resources they use
or whether they compromise performance in some way. That is, File: Find must
be quite a sizable module with a stack of function/options, so couldn't that
mean lots more memory to run, or is that an incorrect presumption?

> You can read the module's docs with:
>
> perldoc File::Find
>
>
>>================
>> #use strict;

>
>
> You lose all of the benefits of that statement when you comment it out!


yes, I know - I had it commented out to double check that the script worked
without use strict.

>> my $basedir = "j:/files/sw-test";
>> my $fcount = 0;
>> my $fsize = 0;
>> my $dcount = 0;
>> dircount();
>>
>> sub dircount {
>> my($cdir) = shift;

>
>
> $cdir will be undef for the top-level call.
>
>
>> $cdir .= "/" if $cdir ne "";

>
>
> You have all of the path components separated already, so I'd
> paste the dir separator in myself on each usage instead of
> burying one inside of a variable's value.


there was a reason I did that, but I can't recall what it was now (bear with
me - it's nearly home-time and my brain is frazzled!)

>> my $dh = "DH" . length($cdir);
>> opendir($dh,"$basedir/$cdir");

>
>
> You get a dynamic dirhandle when the variable is undef.
>
> Your variable is not undef, so there is no dynamic dirhandle...
>
>
> You should always check the return value to see if you actually
> got what you asked for:
>
> opendir($dh,"$basedir/$cdir") or die "could not open '$basedir/$cdir'
> $!";
>
>
>> while(my $fl = readdir($dh)){
>> next if $fl =~ /^\.{1,2}$/;
>> if(-d "$basedir/$cdir$fl"){
>> $dcount++;
>> dircount("$cdir$fl");

>
>
> If $fl is a symlink to a "higher" directory, then your
> code will go into an infinite loop here.


it's a script that will only run on my Windows servers, so that wasn't an
issue

> Applying the minimum changes to fix (IMO) your code, I get:
>
> ------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
>
> my $basedir = '/home/tadmc/temp';
> my $fcount = 0;
> my $fsize = 0;
> my $dcount = 0;
> dircount();
>
> sub dircount {
> my($cdir) = shift || '';


that's a neat way of avoiding getting a warning (yes, I did have use
warnings in there for a while .. is there any particular reason you use
single quotes there instead of double quotes? I tend to use "" for pretty
much everything. Also, I don't ever seem to use "||" - "or" would work as
well in that scenario wouldn't it?

> opendir(my $dh,"$basedir/$cdir") or die "could not open dir $!";
> while(my $fl = readdir($dh)){
> next if $fl =~ /^\.{1,2}$/;
> if(-d "$basedir/$cdir/$fl"){
> $dcount++;
> dircount("$cdir/$fl");
> }
> else{
> $fcount++;
> $fsize += -s "$basedir/$cdir/$fl";
> }
> }
> close($dh);
> }
>
> print "$fcount files and $dcount directories totalling $fsize bytes in
> size\n";
> ------------------------
>
>
>
> Recasting it to use the tried-and-true module, I get:
>
> ------------------------
> #!/usr/bin/perl
> use warnings;
> use strict;
> use File::Find;
>
> my $basedir = '/home/tadmc/temp';
> my $fcount = 0;
> my $fsize = 0;
> my $dcount = 0;
> find( \&dircount, $basedir );
>
> sub dircount {
> return if $_ eq '.' or $_ eq '..';
> $dcount++ if -d;
> return unless -f; # only care about plain files at this point
> $fcount++;
> $fsize += -s;
> }
>
> print "$fcount files and $dcount directories totalling $fsize bytes in
> size\n";
> ------------------------


thanks
Ian


 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      12-22-2005
At 2005-12-22 12:32PM, IanW <(E-Mail Removed)> wrote:
> "Tad McClellan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> >> #use strict;

> >
> > You lose all of the benefits of that statement when you comment it out!

>
> yes, I know - I had it commented out to double check that the script worked
> without use strict.


If your code runs with strict, it will certainly run without.

[...]
> > my($cdir) = shift || '';

>
> that's a neat way of avoiding getting a warning (yes, I did have use
> warnings in there for a while .. is there any particular reason you use
> single quotes there instead of double quotes? I tend to use "" for pretty
> much everything. Also, I don't ever seem to use "||" - "or" would work as
> well in that scenario wouldn't it?


I use single quotes to remind myself (and perl) that I have a literal
string that needs no interpolation.

'||' and 'or' have different operator precedences. Note also that '||'
has higher precendence than '=' which is higher than 'or'. So,
my($cdir) = shift || '';
is the same as
my($cdir) = (shift || '');

Test:
$x = undef || 'alternate';
print '$x is ', (defined $x ? "'$x'" : 'undefined!'), "\n";

Conversly,
my($cdir) = shift or '';
is the same as
( my($cdir) = shift ) or '';
and thus $cdir may still be undefined.

Test:
$y = undef or 'alternate';
print '$y is ', (defined $y ? "'$y'" : 'undefined!'), "\n";

Another way of proving default values is the '||=' operator, as in:
my $cdir = shift;
$cdir ||= ''; # set cdir to the empty string if previously undefined.

--
Glenn Jackman
Ulterior Designer
 
Reply With Quote
 
Tintin
Guest
Posts: n/a
 
      12-22-2005

"IanW" <(E-Mail Removed)> wrote in message
news:doenus$765$(E-Mail Removed)...
> > 3) There is an already-invented (and tested) wheel for doing
> > recursive directory searching, the File::Find module.

>
> The only thing that sometimes puts me off using modules for relatively
> simple things like this, is that I wonder how much extra resources they

use
> or whether they compromise performance in some way. That is, File: Find

must
> be quite a sizable module with a stack of function/options, so couldn't

that
> mean lots more memory to run, or is that an incorrect presumption?


The "overhead" of File::Find is absolutely miniscule. Wouldn't you rather
write efficient code using tried and true
methods, rather than the "overhead" of hacking your own code?


 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      12-22-2005
IanW wrote:
> "A. Sinan Unur" <(E-Mail Removed)> wrote in message
> news:Xns97344FEAA98EDasu1cornelledu@127.0.0.1...
> >> my $fcount = 0;
> >> my $fsize = 0;
> >> my $dcount = 0;

> >
> > These are values that should be returned by your sub. You might want to
> > check for calling context in the sub and supply an appropriate scalar
> > value.

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

> >> dircount();
> >>
> >> sub dircount {
> >> my($cdir) = shift;
> >> $cdir .= "/" if $cdir ne "";
> >> my $dh = "DH" . length($cdir);

> >
> > Using lexical dirhandles, this should not be necessary.

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

> > while (my $fl = readdir($dh)){
> > next if $fl eq '.' or $fl eq '..';

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

>
> > if(-d (my $d = catfile($path, $fl))){
> > $dcount++;
> > my ($fc, $dc, $fs) = dircount($d);
> > $fcount += $fc;
> > $dcount += $dc;
> > $fsize += $fs;

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Session is lost if a directory is created under virtual directory Jack Wright ASP .Net 1 04-19-2005 03:04 PM
Directory and files names in temporary asp.net directory =?Utf-8?B?QmFzIEhlbmRyaWtz?= ASP .Net 5 01-31-2005 01:56 PM
list all css files in a directory and sub directory TJS ASP .Net 1 06-23-2004 10:49 AM
question regarding overriding of web.config in the root directory..in a web app in a virtual directory dotnetprogram ASP .Net 1 12-27-2003 06:02 AM



Advertisments