Front page | perl.beginners |
Postings from December 2011
Re: File Size Script Help - Working Version
Thread Previous
|
Thread Next
From:
Igor Dovgiy
Date:
December 30, 2011 03:43
Subject:
Re: File Size Script Help - Working Version
Message ID:
CADGQiHFHty0LGc6Co5r7EFBJsBU3NqRpsZeAgOmM5tUY3cuajg@mail.gmail.com
Hi Jonathan,
Argh, really stupid mistake by me. ) But let's use it to explain some
points a bit further, shall we?
A skilled craftsman knows his tools well, and Perl programmer (with CPAN as
THE collection of tools of all sizes and meanings) has an advantage here: even
if documentation is a bit vague about what's going on, we are (usually)
able to check the code itself to find the answers. )
By browsing File::Spec source (found via 'Source' link within the
'File::Spec' page at CPAN)...
http://cpansearch.perl.org/src/SMUELLER/PathTools-3.33/lib/File/Spec.pm
...we soon discover that this module is essentially an adapter for modules
like File::Spec::Unix, File::Spec::Mac, File::Spec::Win32 etc.
So our search goes on (as your mention of .DS_Store file implies) over
there:
http://cpansearch.perl.org/src/SMUELLER/PathTools-3.33/lib/File/Spec/Mac.pm
Now we may either check the documentation (which clearly states that only
the last argument to catfile is considered a filename, and all the others
will be concatenated with catdir), or look right into the code - and come
to the same conclusion:
sub catfile {
my $self = shift;
return '' unless @_;
my $file = pop @_;
return $file unless @_;
my $dir = $self->catdir(@_);
$file =~ s/^://s;
return $dir.$file;
}
So what should we do now? ) Of course, give milk to our cat... and
arguments to File::Spec's catfile! ) Like this:
File::Spec->catfile($path, $dircontents . '.md5')
... or this...
File::Spec->catfile($path, "$dircontents.md5")
(check 'variable interpolation in Perl' to see why it's possible - and why
this is essentially the same as previous codeline)
... or even this ...
File::Spec->catfile($path, join '.', $dircontents, 'md5')
(but that would be a bit overkill, of course :)
Speaking of overkills: you used regex (=~ /^\./) to check whether the line
begins with a dot - or not. )
It's ok for this task, but you probably should know that these checks may
be also done with (substr($line, 0, 1) eq '.') code,
which will be a bit (up to 30% at my PC when Benchmark'ed) faster.
-- iD
2011/12/30 Jonathan Harris <jtnharris@googlemail.com>
> I tried to use your suggestion
> open my $wr_fh, '>', File::Spec->catfile($path, $dircontents, '.md5') or
> die $!, $/
> but it returned an error on the command line:
> 'Not a directory'
> At which point the program dies (which is what it is supposed to do!)
> I used it inside the loop - sorry to bug you for clarification
>
>
> ####
> if ($dircontents=~/^\./ || -d $dircontents) {
> next;
> }
>
> This is also to avoid the file .DS_Store
>
>
Thread Previous
|
Thread Next