in reply to Aliasing Variables

for( $self->{x} ) { # Do stuff to $_ }

Yes, I'd love to see a way to say: my $x= aliasOf($self->{x}); Perl's internals are already up to the task, it just isn't supported in the syntax of the language. /: This may have to do with this being mostly useful as a speed thing and Perl not really being a "speed" kind of language?

Update:

for my $x ( $self->{x} ) { # Do stuff to $x }
but that still isn't as general as it could be.

Update2: ...but you already mentioned that.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: Aliasing Variables
by John M. Dlugosz (Monsignor) on Jul 22, 2001 at 11:01 UTC
    my $x= aliasOf $self->{x} Maybe that should go on our wishlist for Perl 6. Or if it's an easy patch (hint hint!) or an XS that implements my $x; make_alias ($x, $self->{x}} would still help, though not as sugary of a syntax.

    —John

      package alias; use strict; BEGIN { use base qw(Exporter); @alias::EXPORT = qw(make_alias); } sub make_alias { tie $_[0], __PACKAGE__, \$_[1]; } sub TIESCALAR { my($class, $target_ref) = @_; bless $target_ref, $class; } sub STORE { my $self = shift; $$self = shift; } sub FETCH { my $self = shift; $$self; } 1;
      No XS here, works fine with 5.x, but maybe a little slow :)
      use alias; my $self = { x => 1 }; make_alias my $x, $self->{x}; $x = 100; print $self->{x}; # 100

      --
      Tatsuhiko Miyagawa
      miyagawa@cpan.org

        Hey, cool!

        That's certainly a nice way to access class members in a method, though it doesn't give the speed boost that was my primary motivation for asking the question.

        Is this an existing CPAN thing, or did you just make it? I think it could be fleshed out a little, e.g.

        sub somemethod { my $self= shift; my ($a,$b,$c)= member_alias ($self, qw/a b c/); ...
        —John