in reply to Compare file content with a variable content

I would have probably used a different approach and not relied on the modified header from the server. For tasks like these, I'm in love with MD5 ...
#!/usr/bin/perl -w use strict; use MD5; use LWP::Simple; my $url='http://www.mrnick.binary9.net'; my $file='sitechanges.txt'; ## grab the contents my $content=get($url) || die $!; ## grab the MD5 sum from our datafile my $oldmd5=''; if (-f $file) { open(IN,"<$file") || die $!; $oldmd5=<IN>; close IN; } ## generate the new MD5 my $newmd5=MD5->hexhash($content); ## same? if ($newmd5 ne $oldmd5) { ## file has change ... do something ## .... (insert your mailing code here) ## then save the new md5 open(OUT,">$file") || die $!; print OUT $newmd5; close OUT; }
But to answer your question, change
if ($modified = $string) { print "the file is the same";
to
if ($modified eq $string) { print "the file is the same";

Update: The size of the files you are grabbing IS very important. 18MB is quite bit for casual downloading for comparison. In that case, I would also rely on the headers returned from HEAD (including Content-Length).

Replies are listed 'Best First'.
Re: Re: Compare file content with a variable content
by Skyhawlk (Initiate) on May 20, 2001 at 15:27 UTC
    If you're dealing with small file then MD5 will be good. The problem is that these files are 12 -18MB each and I have no control on them (someone else is maintaining these files for us). Grabbing 18 MB file from the internet each time sounds like a bad idea to me - doing HTTP HEAD (I call it "giving HEAD" :-)) is more reasonable and faster.
Re: Re: Compare file content with a variable content
by Skyhawlk (Initiate) on May 20, 2001 at 11:11 UTC
    So you're generating an MD5 hash for your file on the URL? how does it work? Ar you automating this somehow? I'm using the header since I need the "last modified" date anyhow.
      The MD5 algorithm is described in RFC 1321 and has it's (unofficial) homepage here. RSA Labs currently 'owns' it, FAQ is here.
      It's basically a fingerprint algorithm.

      For some reason using LWP::Simple's head() doesn't work all that well for me...

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.