Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Perl script modify itself?

Tags:

perl

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.
like image 692
Jeremy Whalen Avatar asked Aug 25 '10 03:08

Jeremy Whalen


1 Answers

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.

like image 183
Martin York Avatar answered Sep 23 '22 18:09

Martin York