On 12/19/11 Mon Dec 19, 2011 11:32 AM, "Jonathan Harris"
<[email protected]> scribbled:
> Hi Perl Pros
>
> This is my first call for help
>
> I am a totally new, self teaching, Perl hopeful
>
> If my approach to this script is simply wrong, please let me know as it
> will help my learning!
>
> The script aims to:
>
> 1) Read in a directory either from the command line, or from a default path
> 2) Produce a hash for future checksum
A "hash" of what? File names (directory contents) or file contents?
> 3) Write this (hex digest) to a separate file, in a sub directory of the
> parent, which has the same name and a .md5 extension
Same name as the file or same name as the directory?
> 4) Check the original file for its size
> 5) Add this data to the newly created file on a new line (in bytes)
Will this file contain information for one file or many files?
>
> I have a script that will do most of this, except for analysing the file
> size - I think that the file size being analysed may be the md5 object
> result as the same value is printed to each file
Print out the file size returned by stat. Check if it is the same displayed
by the ls command.
>
> I am running out of ideas and would appreciate any help you could give!
> I have tried using File::stat::OO and File::stat - but to no avail - I
> could be using them incorrectly!
I am afraid I do not understand exactly what you are trying to accomplish. I
can't tell from your program whether or not you will end up with one digest
file for the entire directory, or one digest file for each file in the
directory.
>
> Many thanks in advance...
>
> Jon.
>
> Here are some details:
>
> System:
> Mac OSX 10.7.2
> Perl version 5.12
>
> Script:
>
> #!/usr/bin/perl
> # md5-test-3.plx
> use warnings;
> use strict;
> use Digest::MD5;
>
> my $filesize = 0;
You should declare variables where they are needed and not before.
> my $dir = shift || '/Users/jonharris/Documents/begperl';
> opendir (DH, $dir) or die "Couldn't open directory: $!";
>
> my $md5 = Digest::MD5->new;
> while ($_ = readdir(DH)) {
You are better off using a scalar variable and not the default variable,
which can get reused and overwritten:
while( my $file = readdir(DH) ) {
> $md5->add($_);
You are adding file names to a string to be "digested". Is that what you
want? Or do you want to calculate a digest for the contents of each file?
I have not used Digest::MD5, but if you to calculate the digest for the
contents of each file, you want to create a new digest object, open the
file, and use the addfile() method, then hexdigest() for each file.
> $filesize = (stat(DH))[7];
You are applying stat to the directory read handle. You want to fetch data
for the file (untested):
my $filesize = (stat("$dir/$file"))[7];
Note that you must prefix the file name with its full path.
> #### Is it necessary to put the following into a new loop?
No. It makes no sense to have a one-iteration loop.
>
> foreach ($_) {
> open FH, ">> /Users/jonharris/Documents/begperl/md5/$_.md5" or die $!;
You are appending lines to a file with a name that is based on an existing
file. Why?
> binmode(FH);
There is no need to set the mode of the output file to binary. Both
hexdigest and the file size will be written in ascii characters, not binary
data.
> print FH $md5->hexdigest, "\n", $filesize;
> }
> close FH;
> }
> close DH;
> print "\n$dir\n\n";
>
> #######
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/