Jeremy Whalen
Jeremy Whalen

Reputation: 113

Can a Perl script modify itself?

I would like to have my scripts keep track of thier last date of revision internally as a comment. Is this possible? It seems to me that it would need to grab the date and then open its script file for an append, write the data and save the file.

Thanks Everone, great answsers one and all. Based on the code snippet left by GreenMatt I threw this together...

#!/usr/bin/perl -w 

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime time;
$year += 1900;
$mon +=1;

open SELF, ">> letterhome.pl" or die "Unable to open self"; 
#print SELF "# ran/modified at " . join(' ', localtime(time)) . "\n"; 
print SELF "# ran/modified at $hour:$min:$sec on $mon/$mday/$year.\n"; 
close(SELF); 

# ran/modified at 31 48 23 24 7 110 2 235 1  
# unformated result of using localtime(time)  

#Results using formated time/date 
# ran/modified at 0:1:43 on 8/25/2010.
# ran/modified at 0:2:40 on 8/25/2010.
# ran/modified at 0:4:35 on 8/25/2010.

Upvotes: 11

Views: 2207

Answers (7)

Znik
Znik

Reputation: 1136

Very old question, and this is the worst practice. If you want to know when script has been run, implement any logging system for that script.

If you want to know about modifications, do it as others wrote to you, with any control version system, subversion or git (the most popular, free for any use). Of course, on that way, you can write manually change log with bigger changes, or ..... do that log separately with different filename extention, for example <your_script>.changelog

Upvotes: -1

GreenMatt
GreenMatt

Reputation: 18570

The following worked on a FreeBSD system. It appends to the end, which sounds acceptable to you, but doesn't conform to the "normal" way of documenting changes within a file - at least for me, as I've almost always seen it done at the beginning. You'll probably want to change the way the date/time is displayed.

#!/usr/bin/perl -w
open SELF, ">> selfModify.pl" or die "Unable to open self";
print SELF "# ran/modified at " . join(' ', localtime()) . "\n";
close(SELF);

Whether this is wise or not I'll leave for you to decide.

Upvotes: 1

Brad Gilbert
Brad Gilbert

Reputation: 34110

#! /usr/bin/env perl
use warnings;
use strict;
use autodie;

{
  open my $self, '>>', $0;
  my $time = localtime;
  print {$self} "# ran on $time\n";
}

__END__
# ran on Wed Aug 25 16:41:05 2010

Upvotes: 1

Loki Astari
Loki Astari

Reputation: 264331

You can get your version control system to do this automatically.

But if you are using version control then this step is really not nesaccery in the first place.

Upvotes: 7

John
John

Reputation: 1540

By request adding my comment as an answer.

Sounds like you already know how to do it. If it is a perl script on a unix/linux box then permissions should not be an issue, if it is on a windows box it might not let you as the file is in use.

Upvotes: 3

vol7ron
vol7ron

Reputation: 42095

Sounds like you already know how to do it. If it is a perl script on a unix/linux box then permissions should not be an issue, if it is on a windows box it might not let you as the file is in use

-- John

Upvotes: -1

cjm
cjm

Reputation: 62089

It is possible, but that doesn't make it a good idea. For one thing, it wouldn't update the date until you ran it.

If you're using a good editor, it may have a way to insert a timestamp automatically when you save the file. For example, I set up Emacs to do that in HTML files using write-contents-hooks. (It would need some modification to work with Perl code, but cjm-html-timestamp in cjm-misc.el would give you a starting point.)

Upvotes: 6

Related Questions