AtomicPorkchop
AtomicPorkchop

Reputation: 2675

How could I add a simple updater for my perl scripts?

My buddy gave me an idea for my VMware script written in perl, he said I should have a way to update the script with an argument like -u.

The problem is I have no idea how I could do that. The only things that come to mind on how I could update the script via the internet would be something will wget, my amazon S3 account or an FTP server.

Whats the easiest way to do something?

--update--

I revised my question and was thinking more about what I am trying to accomplish.

I guess I am pretty much trying to make is a quick and dirty script that does a simple version check to a server or site where I will host my scripts. I don't know what options I can use yet all I got right now is a Dropbox and an amazon S3 account. I would think I could do something with S3.

Upvotes: 1

Views: 88

Answers (1)

reinierpost
reinierpost

Reputation: 8611

This is just an untested first draft, but you'll get the idea:

use strict;
use warnings;
use Getopt::Std;  # for getopts
use LWP::Simple;  # for get

my %opt; getopts('u',%opt);

if ($opt{u}
&& -w $0
&& (my $newcontent = get("http://your.script.s/loca/tion"))
&& open(my $me, '<', $0))
{
    $/ = 0;
    my $currcontent = <$me>;
    close($me);
    if ($newcontent ne $currcontent)
    {
        warn "updating to latest version ...\n";
        if (open($me, '>', $0))
        {
           print $me $newcontent;
           close($me);
           exec $0, @ARGV;  # call it - if that's what you want to do
        }
    }
}

Refactor the if nesting, add error handling, be more cautious about using $0, etc.

Upvotes: 3

Related Questions