Simply Hao on Mon, 14 Feb 2000 17:10:04 -0500 (EST)


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Perl Ripping Event


Here's a snippet of my email to a vendor that sent us a bogus fix.

> The following should be used to help debug variable scoping
> issues:
>
>     use strict;
>
> Because the $sec variable is unused, I changed the following line:
>
>     ($sec, $min, $hr, $day, $mon, $yr) = localtime (time);
>
> To:
>
>     my ($min, $hr, $day, $mon, $yr) = (localtime)[1..5];
>
> my is the Perl keyword for local variables.  See scoping note above.
>
> Although we probably won't be around by then, this would break in
> the year 2100 and beyond:
>
>     $yr = $yr - 100;
>
> Instead, it's better to use modulo (division remainder):
>
>     $yr %= 100;
>
> If we use that, we can get rid of the following test altogether:
>
>     if ($yr >= 100)
>
> I'm not sure whether numbers in the timestamp besides the year
> should be zero-padded.  But from the comments in the script, I
> guessed they might be significant, so I used the sprintf function
> to format the string (and cut down on redundant code).
>
> I really don't understand the two-digit year format, as it seems
> to require a dubious century-guessing algorithm.

Here's the script they sent me:

    #!/usr/local/bin/perl

    print (&time_stamp( ))  ;

    ################################################################
    #  &time_stamp( )  ;                                           #
    #     Returns current local date/time coded as .SDH data:      #
    #         Formats date for import into US Windows settings     #
    #         as mmm dd yy hh:mm.  Metric import settings would    #
    #         be dd mm yy hh:mm.  Seconds are not included.        #
    ################################################################

    sub time_stamp
      {$DT = "&FDT="  ;
       ($sec, $min, $hr, $day, $mon, $yr) = localtime(time)  ;
     $DT .= (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];

       if ($yr >= 100)  
         {$yr = $yr - 100  ;
         }

       if ($yr < 10)
         {$DT .= "+" . $day . "+0" . $yr . "+" . $hr . "%3A" . $min;
         }
         else
         {$DT .= "+" . $day . "+" . $yr . "+" . $hr . "%3A" . $min;
         }
      }

Here's the script after my revisions:

    #!/usr/local/bin/perl -w
    use strict;

    sub time_stamp () {
        my ($min, $hr, $day, $mon, $yr) = (localtime)[1..5];
        sprintf '&FDT=%s+%02d+%02d+%02d%%3A%02d',
            ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
             'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')[$mon],
            $day, $yr % 100, $hr, $min;
    }

    print time_stamp;

-Hao
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**