You can declare a variable in a scope that includes 2 functions:
{ my $myvar sub foo{ $myvar = "Hello"; } sub foo1{ my $myvar1 = $myvar; } }
This is not very elegant, but it can be difficult to maintain since in foo1 where $myvar was set. It is probably best to pass the variable as an argument.
sub foo { my $myvar = "Hello"; return $myvar; } sub foo1 { my( $myvar)= @_; my $myvar1 = $myvar; }
Note that all $myvar 3 $myvar are different variables in different areas.
As a side note, using prototypes ( sub foo() ) is probably not a good idea unless you really know what they are doing, which is probably not the case (see Prototyping Problem for a discussion of prototypes)
source share