Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: access to my variables from other subs

by joe (Acolyte)
on Oct 21, 2000 at 04:56 UTC ( [id://37765]=note: print w/replies, xml ) Need Help??


in reply to access to my variables from other subs

Okay Adam... use strict is in place. Here is another example of the problem:
use strict; my $evaled = 0; # very simplified... sub makesub { my($name) = @_; return "sub $name { print \"\$var \"; }"; } sub a { my($var) = @_; if(!$evaled) { eval(makesub("somesub")); $evaled=1; } } a("what"); somesub(); a("the"); somesub();
prints:
what what
I'd like it to print "what the". Basically I don't want to have to eval every single time, it could be in the millions... but I'd still like access to the my variables of the sub that it was first created in. I don't even mind if I have to call the sub from the one that encloses it. i.e:
sub a { my($var) = @_; if(!$evaled) { eval(makesub("somesub")); $evaled=1; } somesub(); # only place I call it from. # only want to eval once per # program execution, but have # access to the my variables for # each calling of "a" }
Perhaps it's impossible.

Replies are listed 'Best First'.
RE: Re: access to my variables from other subs
by Adam (Vicar) on Oct 21, 2000 at 05:02 UTC
    #!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.

      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, $/ } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://37765]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-03-28 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found