in reply to Current time as variable?

einerwitzen,
As been pointed out, the basic idea is:
my $start = time; # Quiz is taken my $end = time; my $elapsed = $end - $start;
The result will be in the total number of elapsed seconds. The only thing I would add is how to convert that into plain english. There are a handful of modules that can do that. I took a crack at it myself as a learning experience, but I also reference the other modules if you want to use something that is supported.

Cheers - L~R

Replies are listed 'Best First'.
Re: Re: Current time as variable?
by einerwitzen (Sexton) on Dec 17, 2003 at 16:50 UTC
    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
      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