rsiedl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am attempting to write a script which will check back with the main server for the latest version.

Currently I am using LWP::Simple to fetch a copy of the script from the main server and would like to pull $VERSION out of that.

I have several questions:

1. Can anyone see a problem with this method or suggest a better one?
2. Is there a way to temporarily evaluate the code I pull in so I can get the version via something like $temp::VERSION?

Any help would be appreciated. I've attached my test code below

Cheers,
Reagen

#!/usr/bin/perl use strict; use warnings; use LWP::Simple; BEGIN { use vars qw($VERSION $PID); $VERSION = '0.01'; $PID = "/var/run/test.pid"; # create our running pid file open PID, "> $PID"; print PID $$; close PID; # check we are the latest version my $content = get("http://www.someurl.com/software/$0"); die "Couldn't get it!" unless defined $content; # can we eval $content to get $VERSION? $SERVER_VERSION = 0; print "Server version: $SERVER_VERSION\n"; print "Local version: $VERSION\n"; } # end-begin # cleaning up END { `rm -f /$PID`; } # end-end exit;

Replies are listed 'Best First'.
Re: Checking for latest version
by Anonymous Monk on Jan 06, 2010 at 03:20 UTC
    Why would you be using eval? See Perl::Version

    http://code.google.com/p/get-flash-videos/

    sub update { if($::SCRIPT_NAME) { my $browser = FlashVideo::Mechanize->new; $browser->get("http://get-flash-videos.googlecode.com/svn/wiki/Ver +sion.wiki"); if(!$browser->response->is_success) { die "Unable to retrieve version data: " . $browser->response->st +atus_line; } my $version = ($browser->content =~ /version: (\S+)/)[0]; my $base = ($browser->content =~ /from: (\S+)/)[0]; my $info = ($browser->content =~ /info: (\S+)/)[0]; my $url = $base . $::SCRIPT_NAME . "-" . $version; die "Unable to parse version data" unless $version and $base; my @v = split /\./, $version; my @V = split /\./, VERSION; my $newer = 0; my $i = 0; for(@v) { $newer = 1 if !defined $V[$i] || $_ > $V[$i]; last if $V[$i] > $v[$i]; $i++; } if($newer) { info "Newer version ($version) available, downloading.."; die "Cannot update -- unable to write to $0\n" unless -w $0; my $new_file = $0 . ".new"; $browser->mirror($url, $new_file); if($browser->response->is_success && -f $new_file) { rename $0, "$0.old" or die "Unable to rename $0 to $0.old: $!" +; rename $new_file, $0 or die "Unable to rename $new_file to $0: + $!"; chmod 0755, $0; info "New version installed as $0"; info "(previous version backed up to $0.old)."; info $info; exit 0; } else { die "Download failed: " . $browser->response->status_line; } } else { print STDERR "You already have the latest version.\n"; } } else { info "Development version, not updated"; } update_plugins(); }
    Also see PAR::WebStart
Re: Checking for latest version
by mykl (Monk) on Jan 06, 2010 at 10:26 UTC

    Would you consider comparing by the datestamp on the file rather than the version number? Then you could just use the mirror() method of LWP::Simple to check that the script is up to date, and download the newer version if not. However, whether a datestamp comparison is safe will depend on your particular application - whether an older version could ever get a newer datestamp.

    --

    "Any sufficiently analyzed magic is indistinguishable from science" - Agatha Heterodyne