in reply to Re: access to my variables from other subs
in thread access to my variables from other subs

#!perl -w use strict; # ALWAYS! a("what"); somesub(); a("the"); somesub(); { my $var; # shared only between a() and somesub() sub a { $var = shift } sub somesub { print $var, $/ } } # proof that $var is scoped correctly: my $var = 'global'; print $var, $/; somesub(); print $var, $/;
I should point out that ALL named subroutines are global to the package in which they are named. Thus declaring a named subroutine inside another routine does not do what you want. Anonymous subroutines, on the other hand, are scoped locally (for relatively obvious reasons). Of course, the routine can only see variables that are in the scope where the routine was declared...

Update: I'm looking at this, and it doesn't look like your original question. It seems you wanted named routines that came from the same code but did different stuff based on some third party manipulator function... let me think on that.

Replies are listed 'Best First'.
RE: RE: Re: access to my variables from other subs
by Adam (Vicar) on Oct 21, 2000 at 05:16 UTC
    Okay, here you go:
    #!perl -w use strict; # ALWAYS! a("what"); b("the"); { use vars ('$var'); local $var; # shared only between a() and somesub() sub a { local $var = shift; _somesub() } sub b { local $var = shift; _somesub() } # sub routines with a leading _ usually denotes a private func. sub _somesub { print $var, $/ } }