in reply to Recover a variable from a function
The simplest approach is to not use a lexical variable but a global variable:
use vars '$start'; sub Start_timer : Test(startup) { $start = time; ... }; sub End_timer : Test(shutdown) { print time - $start; ... }
If you really want to avoid global variables for things like that, you could use the state keyword and a function to read the start time:
sub get_or_init_start_time { state $start ||= time; }
You need to call this function in two places, once to initialize the $time variable and once to retrieve the value.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Recover a variable from a function
by Chaoui05 (Scribe) on May 19, 2016 at 07:45 UTC | |
|
Re^2: Recover a variable from a function
by Chaoui05 (Scribe) on May 19, 2016 at 08:36 UTC | |
by Corion (Patriarch) on May 19, 2016 at 08:47 UTC | |
by Chaoui05 (Scribe) on May 19, 2016 at 09:12 UTC |