in reply to What to use instead of global variables

#!/usr/bin/perl use feature 'switch'; use strict; require "lib.pl"; my $global_variable1='a'; my $global_variable2='b'; my $global_variable3='c'; given( subroutine1() ){ when (0) { subroutine2($global_variable1) } when (1) { subroutine2($global_variable2) } when (2) { subroutine2($global_variable3) } } #!/usr/bin/perl # lib.pl use strict; sub subroutine1 { return rand(3); } sub subroutine2 { print shift; }

or, even better...

#!/usr/bin/perl use strict; require MyLib; my @global_variable = qw( a b c ); subroutine2( $global_variable[subroutine1()] ); #!/usr/bin/perl # MyLib.pm use strict; sub subroutine1 { return rand(3); } sub subroutine2 { print shift; }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'