in reply to a reference by any other name...

Are you looking for Data::Alias or Devel::LexAlias? That said, these modules are deep voodoo, not to be used in day-to-day coding, and I assume you just haven’t understood exactly how to use references, as GrandFather says. Some actual code sample would be helpful if you want better advice.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: a reference by any other name...
by blogical (Pilgrim) on Feb 20, 2006 at 02:51 UTC
    Devel::LexAlias looks like the sort of operation I'm looking for... of course, I don't regularly risk "flaming death" in the pursuit of laziness...
    If there isn't an appropriate, simple way to do this I'm going to be very surprised. I will be somewhat less surprised if I'm missing something obvious- it seems like such a simple request.
      If there isn't an appropriate, simple way to do this I'm going to be very surprised.

      You can do it pretty easily with package variables:

      @array1 = 0..9; *array2 = *array1; pop @array1; print "@array2\n"; pop @array2; print "@array1\n"

      But I think the flaming death risk is pretty high here, too, though for different reasons.

      Update: you can also use a similar technique to alias to a reference, though again the alias is a package variable, and as such carries all the associated caveats. Example:

      sub foo { my ($ref) = @_; local *nonref = $ref; pop @nonref; print "@$ref\n"; pop @$ref; print "@nonref\n"; } foo([ 0 .. 9 ]);
        @array1 = 0..9; *array2 = *array1;

        That nukes the entire *array2 glob. I think you want

        @array1 = 0..9; *array2 = \@array1;

        Makeshifts last the longest.

        That looks like the sort of laziness I'm looking for- although likely a bit of overkill... aha, pg 79 of the camel 3rd ed.:
        *foo = \$bar;
        But I see that it creates a global, whereas I really only want a lexical....
        local *foo = \$bar;
        would seem to come close though. Although, if I can't use a lexical, perhaps I should just declare our %bar before the function call, and just call it what it is without bothering to pass a reference... that seems like a bad solution though.

        Thanks! I learned something. Of course, I'm always up for learning more, if any other smartymonks care to divulge their secrets.

      Nope, no can do in Perl 5. Perl 6 will have aliasing in the language, but for Perl 5, Devel::LexAlias is the closest you get. It has always annoyed me as well, but there’s no way around it.

      Makeshifts last the longest.