Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

RE: RE: RE (tilly) 2: access to my variables from other subs

by extremely (Priest)
on Oct 21, 2000 at 12:43 UTC ( [id://37795]=note: print w/replies, xml ) Need Help??


in reply to RE: RE (tilly) 2: access to my variables from other subs
in thread access to my variables from other subs

I think it would be useful if subs created in a sub had access to it's parent's scratchpad, and if the parent that created it is no more, then the last instance of the parent sub that was created. I'd even accede that the created sub must be called from within the parent sub -- but with access to the scratchpad of the current instance of the parent sub, not the scratchpad of the instance of the parent sub that did the actual creation (which may be long gone...).

A lot of people think it should work that way but I don't think it ever will. In a way it does and it a way it doesn't act like you'd like.

One, as I understand it, there is just one big scratchpad for the my'ed variables. I do know that all the subs are created at the same "level". They are all first class subroutines of the package they are in. Look at this contrived example:

#!/usr/bin/perl -w use strict; sub one { my $inside=1; sub two { $inside++; my $inner=$inside+1; print "$inside $inner\n"; } print "$inside "; # no return there two(); } two(); two(); two(); one(); two(); &main::one; &main::two; #### prints # 1 2 # 2 3 # 3 4 # 1 2 3 # 3 4 # 1 4 5 # 5 6

In that example, from the last two lines, you can quickly see that perl creates both subs as tho they each were independent. What happens is when perl sees the sub called inside the enclosure, it creates the $inside variable right away and gives it to two. Now, since one hasn't been run yet $inside doesn't exist so perl screams: Variable "$inside" will not stay shared at ./test line 7. To let you know that when you run one that first $inside will be brutally trampled on...

The fact is, since the subs are supposed to be the same level but we stuck one inside the others closure, perl does its best to make it all work out. It even goes back and fixes $inside after the first time we use one. (well not really, actually it pre-made a scratchpad entry for $inside and used that reference in two! Then every time we call one after that, it makes a new scratchpad entry and has no idea it is supposed to tell two about it, since they aren't linked together! It's just that perl told two the hidden real name (reference) of the first $inside it was making for one.)

In effect, what you are asking for is a degenerate subroutine that is a child of one but perl has no way of making one and honestly doesn't need to. What it does it much cooler. If you want a sub that is only used inside another sub and can see the current values, try this:

#!/usr/bin/perl -w use strict; sub three { my $inside=1; my $four = sub { $inside++; my $inner=$inside+1; print "$inside $inner\n"; }; # notice the semicolon, this is a statement! print "$inside "; &$four(); &$four(); } three(); three(); ##### returns # 1 2 3 # 3 4 # 1 2 3 # 3 4

Does that help at all? Just remember that there is only one scratchpad and code has no idea where it is. Only what the compiler told it. =)

--
$you = new YOU;
honk() if $you->love(perl)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found