Hi Lee,

On Wed, 12 Jun 2013 06:44:19 +0200
lee <l...@yun.yagibdah.de> wrote:

> Hi,
> 
> what I'm trying to do is create a list of sha-2 sums of files the names
> of which are stored in a file.  The purpose is to verify from time to
> time whether any of the listed files have been modified or not in the
> meantime.
> 
> So I can read the list of file names; what I don't understand is what
> the most efficient way would be to create a sha-2 sum for a file.
> 
> It doesn't even need to be sha-2 since there are no security issues
> involved.  The only purpose is to figure out reliably if any of the
> files on the list have been modified or not.  I don't want to go by file
> modification dates because that isn't entirely reliable.
> 
> I've been googling for examples of how to create a sha-2 sum of a file
> in perl without success.  What I'm looking for is something like:

In general, you should not Google for Perl information, because Google tends to
find outdated results. Instead, use MetaCPAN - http://metacpan.org/ with a
fallback to DuckDuckGo - https://duckduckgo.com/ .

> 
>    $hash = create_sha2_sum( $filename);
> 

This MetaCPAN search should be instructive:

https://metacpan.org/search?q=sha

Especially https://metacpan.org/module/Digest::SHA

You can write create_sha2_sum like that (untested):

<CODE>
sub create_sha2_sum
{
        my ($filename) = @_;
        
        my $sha = Digest::SHA->new(512);
        $sha->addfile($filename);
        
        return $sha->hexdigest();
}
</CODE>

> Do you know of any examples I could look at?  Or is there a better way
> to figure out if a file has been modified?
> 

You can try looking at the mtime() from
http://perldoc.perl.org/File/stat.html , but that's not as reliable as
calculating a checksum of the contents.

Regards,

        Shlomi Fish

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Why I Love Perl - http://shlom.in/joy-of-perl

Doing linear scans over an associative array is like trying to club someone to
death with a loaded Uzi.     — Larry Wall

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to