in reply to Re: Current time as variable?
in thread Current time as variable?

it seems that they all work when the codes you gave are in one part. I tried it as

&starttime; &stoptime; sub starttime{ my $start = time; sleep 5; } sub stoptime{ my $end = time; my $elapsed = $end - $start; print $elapsed; }
and it always prints out as 1071679507 seconds

Replies are listed 'Best First'.
Re: Current time as variable?
by b10m (Vicar) on Dec 17, 2003 at 17:08 UTC
    That is because in sub stoptime, your script doesn't know what $start is, since you only declared that in the starttime sub. Now if you'd do it like the following, it would print out 5:
    my $start; my $end; &starttime; &stoptime; sub starttime{ $start = time; sleep 5; } sub stoptime{ $end = time; print $end - $start."\n"; }

    Update: If you'd put "use strict;" in the beginning of your little snippet, Perl would warn you about this with: Global symbol "$start" requires explicit package name at test line 14.

    --
    b10m