How can I transfer the subroutine variable value into another subroutine variable, Can I use global variable.
sub foo(){
my $myvar = "Hello";
}
sub foo1(){
my $myvar1 = $myvar; # how can I get the "Hello" from $myvar.
}
I tried to use package and global variable, but failed.
Package Bar;
our $bar;
Thank you.
You could declare the variable in a scope that includes the 2 functions:
That is not really elegant though, and can be hard to maintain, as it is not clear in
foo1where the value of$myvarwas set. It is probably better to pass the variable as an argument.Note that all 3
$myvarare different variables, in different scopes.As a side note, using prototypes (
sub foo()) is probably not a good idea, unless you really know what they are doing, which is likely not to be the case ( see The problem with prototypes for a discussion on prototypes)