Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
Please tell me which is the best method to write a perl code.
Method 1:
$yyyy = (localtime)[5] + 1900; $mm = (((localtime)[4]) + 1); $dd = (localtime)[3]; $hh = (localtime)[2]; $min = (localtime)[1]; $ss = (localtime)[0]; if ($mm < 10) { $mm = "0" . $mm; } if ($dd < 10) { $dd = "0" . $dd; } if ($hh < 10) { $hh = "0" . $hh; } if ($min < 10) { $min = "0" . $min; } if ($ss < 10) { $ss = "0" . $ss; }

Method 2
$yyyy = (localtime)[5] + 1900; $mm = (((localtime)[4]) + 1); $dd = (localtime)[3]; $hh = (localtime)[2]; $min = (localtime)[1]; $ss = (localtime)[0]; $mm = "0" . $mm unless ($mm > 10); $dd = "0" . $dd unless ($dd > 10); $hh = "0" . $hh unless ($hh > 10); $min = "0" . $min unless ($min > 10); $ss = "0" . $ss unless ($ss > 10);

Thanks

Replies are listed 'Best First'.
Re: best way to write code
by tlm (Prior) on Apr 12, 2005 at 12:22 UTC

    Expanding a bit on gellyfish's answer, this is what I would do:

    use strict; use warnings; my ( $ss, $min, $hh, $dd, $mm, $yyyy ) = ( localtime )[ 0..5 ]; $mm += 1; $yyyy += 1900; $ss = sprintf( '%02d', $ss ); $min = sprintf( '%02d', $min ); $hh = sprintf( '%02d', $hh ); $dd = sprintf( '%02d', $dd ); $mm = sprintf( '%02d', $mm ); # or more succinctly with map: # ( $ss, $min, $hh, $dd, $mm ) = # map sprintf( '%02d', $_ ), $ss, $min, $hh, $dd, $mm; print "$ss $min $hh $dd $mm $yyyy\n"; __END__ 10 13 08 12 04 2005
    If the only thing you intend to do with all these variables is stick them in a string, you can achieve the same effect more simply using POSIX::strftime (as gellyfish suggested):
    use POSIX; my $time_string = strftime '%S %M %H %d %m %Y', ( localtime )[ 0..5 ]; print "$time_string\n"; __END__ 10 13 08 12 04 2005

    the lowliest monk

Re: best way to write code
by gellyfish (Monsignor) on Apr 12, 2005 at 11:40 UTC

    None of the above.

    You should either use sprintf or POSIX::strftime

    /J\

      From the above code whether if condition or unless condition is the best?
Re: best way to write code
by bradcathey (Prior) on Apr 12, 2005 at 12:36 UTC

    A combo of Date::Calc and sprintf works for me.

    use Date::Calc qw(:all); my ($year,$month,$day, $hour,$min,$sec) = Today_and_Now(); my $date = sprintf ("%02d-%02d-%d", $month,$day,$year); my $time= sprintf ("%02d:%02d:%02d", $hour,$min,$sec);

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: best way to write code
by cowboy (Friar) on Apr 12, 2005 at 14:38 UTC
    There are lots of ways to do this, (some listed above by others).
    One thing I can see wrong with your code is you are calling
    localtime
    more than once. This can lead to inconsistancies in the numbers. For formatting, try this:
    my @time = localtime; my $yyyy = $time[5] + 1900; my $mm = sprintf("%02d", $time[4] + 1); # pad with 0's # repeat for other variables
Re: best way to write code
by NateTut (Deacon) on Apr 12, 2005 at 15:30 UTC
    I agree with cowboy the multiple localtimes will cause you grief.

    This is a definite case of TMTOWTDI. As long as the way you choose to code works for you, you are blessed. Beware however not all ways of coding a solution to the same problem are equal. Some ways are faster than others. Some ways are more readable than others. Make sure you understand what it is that you are doing and it's ramifications. Experiment with different solutions.
Re: best way to write code
by brian_d_foy (Abbot) on Apr 12, 2005 at 16:15 UTC

    Besides the answers to the specific example, whenever you code you should avoid typing the same thing twice. In your example, you format numbers by using the same statement, although each uses a different variable. That's a lot of repeated code that you have to maintain in parallel. If you get it wrong the first time, you have update every place you copy-and-pasted it.

    There are various looping constructs that will do this, and the other replies show some of those.

    --
    brian d foy <brian@stonehenge.com>
Re: best way to write code
by gam3 (Curate) on Apr 12, 2005 at 16:35 UTC
    If you really don't want to use sprintf, etc. I would suggest this approach;
    my ($ss, $min, $hh, $dd, $mm, $yyyy) = map({$_ < 10 ? '0' . $_ : ''.$_} localtime); $yyyy += 1900; print "$ss, $min, $hh, $dd, $mm, $yyyy\n";
    -- gam3
    A picture is worth a thousand words, but takes 200K.
      Maybe you could use below module:     :)

      SYNOPSIS:

      use ModestCurrentDateModule; my $date = monthName.' '.monthDay.', '.year; my $date2 = monthName.' '.monthDayDouble.', '.year; my $date3 = monthNumber.'-'.monthDay.'-'.year; my $date4 = monthNumber.'-'.monthDayDouble.'-'.year;
      The Module:
      package ModestCurrentDateModule; use strict; use base qw( Exporter ); use vars qw( @EXPORT ); my ($d, $m, $y) = (localtime)[3..5]; my @months = map ucfirst, qw( january february march april may june ju +ly august september october november december ); @EXPORT = qw( monthName monthNumber monthNumberDouble monthDay monthDa +yDouble year ); sub double{ ( '0' x (2 - length $_[0]) ) . $_[0] } sub monthName{ $months[$m] } sub monthNumber{ $m + 1 } sub monthNumberDouble{ double monthNumber } sub monthDay{ $d } sub monthDayDouble{ double monthDay } sub year{ 1900 + $y } 1;
Re: best way to write code
by NetWallah (Canon) on Apr 12, 2005 at 20:35 UTC
    I like to conserve Namespace, so combining that with gam3's method:
    my %T; # Store all TIME fields in a single hash named "T" @T{ qw(sec min hour mday mon year wday yday isdst) } = map({$_ < 10 ? '0' . $_ : ''.$_} localtime); print qq($_\t:$T{$_}\n) for sort keys %T; $T{year}+=1900; print qq($T{sec}, $T{min},$T{hour}, $T{day});

         "There are only two truly infinite things. The universe and stupidity, and I'm not too sure about the universe"- Albert Einstein