11

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.
  • 3
    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 Aug 25 '10 at 03:11
  • 1
    Filesystems usually store the last modification time stored as metadata. If what the OS does is not exactly what you want, you can modify the metadata, no need to store it inside a comment in the sourcecode. That would work under both Windows and Linux. – Vinko Vrsalovic Aug 25 '10 at 03:15
  • 1
    @John:dude, if you put that exact comment as an answer, I would vote it up, and it would probably be accepted. – Matt Briggs Aug 25 '10 at 03:15
  • 2
    I'm more curious about why you would want to do this? Emacs and other editors have special variables that you use. If you're trying to use some sort of version control, I suggest you look at `SVN`, `GIT`, or `Mercurial`. – vol7ron Aug 25 '10 at 03:24

7 Answers7

7

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
  • 257,169
  • 86
  • 333
  • 562
  • 2
    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 Aug 25 '10 at 03:37
6

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
  • 61,471
  • 9
  • 126
  • 175
  • 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! – Jeremy Whalen Aug 25 '10 at 03:26
  • Lol... I read the comments, grocking is another thing entirely ;) btw... your comment sounds like a "Chicks on Speed" reference... – Jeremy Whalen Aug 25 '10 at 04:11
3

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,530
  • 1
  • 10
  • 19
1

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
  • 18,244
  • 7
  • 53
  • 79
1
#! /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
  • 33,846
  • 11
  • 78
  • 129
  • 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" – Jeremy Whalen Aug 25 '10 at 23:44
  • `localtime` is not a variable, it is a subroutine. `print scalar localtime` would yield the same result. – Brad Gilbert Aug 29 '10 at 18:03
0

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

Znik
  • 1,047
  • 12
  • 17
-1

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
  • 40,809
  • 21
  • 119
  • 172