in reply to Re: Type globs, strict, and lexical variables
in thread Type globs, strict, and lexical variables

I spoke too soon. using local still soes not present accidental clobbering:
my $f = "boot"; {local *tmp =\$f; $tmp = "loop"; GG::cc () ;print "$f\n...\n"}; sub cc { $tmp="cc did it"}; print "$f\n...\n";
sub cc, happens to use the same dummy variable name $tmp. as a result as the local expression $f gets clobbered. What I want is something that makes $tmp scoped like a my variable.

THe problem I am trying to solve here is simply to always have to write de-reference code when I pass in a reference. I want an alais in the form of a localy scoped variable to the object being dereferenced. It looks like you might be able to do this with a tie or with Data::alias but those are both very heavyweight solutions.

Replies are listed 'Best First'.
Re^3: Type globs, strict, and lexical variables
by ikegami (Patriarch) on Jul 17, 2006 at 17:10 UTC
    strict won't let you do that.
    use strict; sub test { my $f = "boot"; local *tmp = \$f; print("$tmp\n"); # boot our $tmp = "loop"; print("$tmp\n"); # loop test2(); print("$tmp\n"); # loop } sub test2 { #$tmp = "test2 did it"; # XXX strict error my $tmp = "test2 did it"; # lex var alternative local our $tmp = "test2 did it"; # pkg var alternative } test();

    Yes, test2 can change the value of $tmp, but only if test2 doesn't properly localize its variables. Perl gives you plenty of ways of shooting yourself in the foot, but they can all be avoided easily and safely with a little care. In this case, that means local shoule be used whenever our is used, unless there is a need to modify the variable in the parent scope.

Re^3: Type globs, strict, and lexical variables
by xmath (Hermit) on Jul 19, 2006 at 09:16 UTC
    Data::Alias isn't that heavyweight I think... I know it's a bit largeish module, but unlike tie() its use imposes virtually no overhead at runtime, and very little at compile time (no source filter).

    And it solves your problem nicely, and probably more efficiently than anything involving typeglobs:

    alias my @bar = @$foo;

    UPDATE: It actually benchmarks faster than local *bar = $foo on my machine, with the benefit of being truly lexically scoped.

Re^3: Type globs, strict, and lexical variables
by ysth (Canon) on Jul 20, 2006 at 00:58 UTC
    sub cc, happens to use the same dummy variable name $tmp.
    But dummy variables should always be lexicals. In fact, I don't think you've absorbed the concept that everything possible (which is just about everything) should be lexical. Are you dealing with legacy code?