in reply to When is my variable not really mine?

Nobody can refer to a lexical variable outside its lexical scope by name.

However, if you hand a reference to that variable to another area, or even a reference to a closure that has visibility of the lexical variable, the variable can be tweaked at will.

Note that a subroutine has limited "call-by-reference" semantics as well, if you directly alter @_ rather than copy the values, as is routinely suggested.

And even if you had a copy of a top-level value, if that copy contains references, then the references are pointers into your original data structure. To fully insulate yourself, you must perform a deep copy.

-- Randal L. Schwartz, Perl hacker

  • Comment on Re: When is my variable not really mine?

Replies are listed 'Best First'.
RE: Re: When is my variable not really mine?
by gaspodethewonderdog (Monk) on Sep 07, 2000 at 17:49 UTC
    Well I'm not playing with references and I'm not passing this variable to anybody else, it is mine and mine alone *muhahahaha*.

    I'm starting to worry though that this code isn't being called as I'd expected and is possibly being eval'd into existance or manipulated in some way, because I've been looking more and more into it and it doesn't make sense.

    I was hoping that this problem could easily be explained by 'I know too little', but maybe I should be leaning towards 'the guy who wrote this is a code slingin' cowboy'. It isn't the first weird thing I've seen him do *chuckles*

      I think we need more information. Merlyn is correct in that a 'my' variable cannot be referenced outside of the block, though a reference passed outside certainly can affect the value.

      What I don't understand is how you can know that the value of your variable has changed unless you're doing some execution of code within your block. If you're entering a block with a 'my' variable, leaving that block to execute some other code, and returning to it, the variable is recreated and undefined on the 2nd run. Consider:

      sub a { my $var = 1; &b; # Can't touch $var eval $set_var_to_2; # Code in this variable can affect $var print "$var\n"; }
      If we could see some code we could explain what's going on, probably. Also make sure you're using 'strict'. This will catch lots of problems like this.
        Well as it so happens the engineer decide to do some magic to the code. So what I've written wasn't being run as a script but instead being mutilated by his processes and then eval'd. Yeah! :)

        Anyway, what was basically going on was that he was chucking a bunch of information in the code to 'massage' it how he needed it to work so that variables could then be exported to all sorts of other places.

        Yes this is weird, yes this is definitely not how I'd have even imagined to do it, and yes from now on when I need something fixed in the code I think I'll let him do it and let him have his silly job security.

        At least I can safely know that I'm not loosing my mind and forgetting how to program... phew!