Harry Putnam wrote:
> 
> Maybe unrelated to the comments above since this is `do' I'm posting
> about in this post.  But I'm still seeing something here I don't
> understand.
> 
> I'll use my reall example code since that is where I notice this
> phenomena.
> 
> My function to slurp looks like:
> 
> cat /home/hgp/scripts/perl/fnc/lctime
> 
>   sub lctime {
>   ## Fucntion to create a date in my preferred format
>     @lta = localtime;
>   ## Adjust for mnth range of 0-11
>     $lta[4] += 1;
>   ## Adjust for year based on 1900, and remove leading 2 digits so
>   ## 103 first becomes 2003 and then 03
>     ($lta[5] = ($lta[5] += 1900)) =~  s/^..//;

You could use the modulus operator instead of the substitution.

      $lta[5] %= 100;


>   ## Print it out in this format:
>   ## mnthdayyr hr:min:sec wkday (padding with zeros where necessary)
>   ## Like 012003 15:15:51 01
>     printf "%02d%02d%02d %02d:%02d:%02d %02d\n", @lta[4,3,5,2,1,0,6];

Perl subs return the value of the last expression in the sub which in
this case is printf() which returns 1 (true) on success.  You want to
return the actual string using sprintf().

      sprintf "%02d%02d%02d %02d:%02d:%02d %02d\n", @lta[4,3,5,2,1,0,6];


>   }
> 
> How can I print the output of this into a file handle?
> 
> When I try, I get an extra numeral 1 which I'm guessing is a return
> value from misguided referencing.
> 
> Example:
> 
> #!/usr/local/bin/perl -w
>   do "/home/hgp/scripts/perl/fnc/lctime";
>   open(FILE,">somefile");

You should _always_ verify that the file opened successfully.

    open(FILE,">somefile") or die "Cannot open 'somefile' $!";


>   print FILE "An extra numeral <" . &lctime . "> appears\n";
                                      ^
You shouldn't use ampersands unless you need the different behavior they
provide.

    print FILE "An extra numeral <" . lctime() . "> appears\n";


You should probably be using POSIX::strftime instead which is simpler
and faster.

use POSIX 'strftime';

    print FILE strftime( "An extra numeral <%D %T %w> appears\n",
localtime );



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to