tags:

views:

227

answers:

6

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.
+4  A: 

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.)

cjm
You are absolutely correct! I should have realized that before I ever created the post, that will teach me to post while running a low grade fever. I suppose it may be useful in certain circumstances to track the last date of execution within the script itself. Although a good log would seem to be the prefered method. Thanks, I appreaciate the quickness of your answer!
jwhalen72
`jwhalen72` might not read comments, but `cjm` does :)
vol7ron
Lol... I read the comments, grocking is another thing entirely ;) btw... your comment sounds like a "Chicks on Speed" reference...
jwhalen72
A: 

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

vol7ron
+1  A: 

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.

GreenMatt
I added a bit to put the timestamp in a more readable format... not sure if this has to do with running strawberry Perl on my windows PC or if it is simply how the "localtime(time) function works...
jwhalen72
`localtime(time)` is pointless `localtime()` calls `time` for you.
Brad Gilbert
@Brad Gilbert: It seems you're right. I modified the answer.
GreenMatt
+5  A: 

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.

Martin York
Well, that depends on your VC. Git doesn't do keyword expansion. (Well, it can, but it's not trivial to set up and not recommended.)
cjm
+3  A: 

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.

John
+1  A: 
#! /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
Brad Gilbert
Nice! it's interesting how the var when printed yields the nice format "Wed Aug 25 16:41:05 2010" while when invoked directly as in "print localtime;" it yields "21411925711032361"
jwhalen72
`localtime` is not a variable, it is a subroutine. `print scalar localtime` would yield the same result.
Brad Gilbert