in reply to Recover a variable from a function

Hi Chaoui05,

Another approach is to make the lexical variable accessible to the two subroutines where you need to access to it by enclosing the variable declaration and the sub definition in a common code block.

{ my $start; sub Start_timer : Test(startup) { $start = time(); cprint "#\x037Started at ", (strftime '%A %d %B %Y %H:%M:%S',lo +caltime($start)), "\x030\n"; } sub End_timer : Test(shutdown) { my $end = time; cprint "#\x037Ended at ", (strftime '%A %d %B %Y %H:%M:%S', loc +altime($end)),"\x030\n"; cprintf "#\x035 Total run time=>", $end-$start, " seconds\n"; cprintf ("%02d:%02d:%02d\x030\n",(gmtime($end-$start))[2,1,0]); } }

Replies are listed 'Best First'.
Re^2: Recover a variable from a function
by Chaoui05 (Scribe) on May 19, 2016 at 14:19 UTC
    Thanks Laurent !! Advantage (or disadvantage ) in Perl is that there are a lot of possibilities to do one thing ! Thanks againb for this another approach