views:

1109

answers:

3

I'm modifying a pre-existing script in Xcode to customize my file headers. The script is Perl and it's not my best langage. :)

I just need to insert the current date in the header in dd/mm/yy format.

Here is my script :

#! /usr/bin/perl -w
# Insert HeaderDoc comment for a header
#
# Inserts a template HeaderDoc comment for the header.
use strict;

# get path to document
my $headerPath = <<'HEADERPATH';
%%%{PBXFilePath}%%%
HEADERPATH
chomp $headerPath;
my $rootFileName = &rootFileNameFromPath($headerPath);

print "/*";
print " * $rootFileName\n";
print " * Project\n";
print " *\n";
print " * Created by Me on ";
# in bash it would be something like that :
# date +%d/%m/%y | awk '{printf "%s\n", $1}';
print " * Copyright 2009 My_companie. All rights reserved.\n";
print " *\n";
print " */\n";

sub rootFileNameFromPath {
    my $path = shift;

    my @pathParts = split (m'/', $path);
    my $filename = pop (@pathParts);
    my $rootFileName = "$filename";
    $rootFileName =~ s/\.h$//;
    return $rootFileName;
}

exit 0;

I've just modified the print command so don't ask me for the rest of the code :)

+2  A: 
@time = localtime(time);
$mday = $time[3];
$mon = $time[4]+1;
$year = $time[5]+1900;
print "$mday/$mon/$year\n";

Should do it.

Edit:

printf "%02d/%02d/%4d",$mday,$mon+1,$year+1900";

Will take care of the padding with zeroes too.

trex279
it gaves me some error : "Global symbol "$sec" requires explicit package name" do you see the point?
claferri
made a few changes to code. But seems to be working for me without the error you're getting.
trex279
try removing use strict;
trex279
@trex279: still have some warning about $sec and the other unused variable, but seems to do the trick, thx.
claferri
updated code to remove warnings
trex279
declare the variables by using 'my' before the first list, instead of removing the use strict, which is not usually considered a good idea.
mirod
Just learning perl. So pardon my ignorance. :)
trex279
+14  A: 

Rather than removing strict (!), why not just make the code strict clean?

my ($mday, $mon, $year) = (localtime(time))[3, 4, 5];

$mon  += 1;
$year += 1900;

printf "%02d/%02d/%02d\n", $mday, $mon, $year % 100;

Maybe even better (since more familiar looking to someone who asked in terms of Bash):

# At the top, under use strict;
use POSIX qw/strftime/;

# then later...
my $date = strftime "%d/%m/%y", localtime;
print "$date\n";

Funny coincidence: Perl Training Australia publishes semi-regular tips (you can get them via email or online), and just today there's a new one on strftime.

Telemachus
POSIX's strftime is the correct way to format dates... that or another class to do it, like Date::Manip.
R. Bemrose
Date::Manip is very powerful, but many of its features have high performance penalty. Pesonally, I normally use the first example here.
spoulson
@spoulson: Yup, the Date::Manip documentation itself has a whole section explaining why you generally don't need/want Date::Manip. I generally use POSIX's strftime since it automagically takes care of the details that I might otherwise forget or mess up (add 1 to month, 1900 to years).
Telemachus
+7  A: 

You could also use DateTime and related modules, which is of course complete overkill for a little script like this. But for a larger app, you should use solid modules rather than doing everything the long way. For the record, with DateTime you'd write:

DateTime->today()->strftime('%d/%m/%y');

Or you could use the more modern CLDR format language:

DateTime->today->format_cldr('dd/MM/YYYY');
Dave Rolsky