Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Aliasing bites

by Zaxo (Archbishop)
on May 11, 2005 at 07:44 UTC ( [id://455882]=note: print w/replies, xml ) Need Help??


in reply to Aliasing bites

On aliases, I wish Perl had more of 'em. This doesn't really have a lot to do with your meditation, but the topic made it spill out.

I'd like to have lexical variables which are aliases to other values. Currently, you only get that by defining a for loop variable:

my ($foo,$bar,$baz,$i); for my $quux ($foo, $bar, $baz) { $quux = $i++; } print "$foo $bar $baz\n"; __END__ 0 1 2
I think that explicit aliasing which gives value semantics to references would be very desirable. A new alias keyword taking $_ as default argument would allow, for instance, named aliases for subroutine arguments:
sub wibble { return unless @_ > 2; my ($string, $position, $count) = map { alias } @_; # . . . }
where you wish to modify the external arguments.

I expect this sort of thing has been discussed before.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Aliasing bites
by iblech (Friar) on May 11, 2005 at 13:27 UTC
    I'd like to have lexical variables which are aliases to other values.

    Luckily, there's Perl 6 :)

    my $var = ...; my $alias := $var; # That's it. # Or: my $short := %alias_to<a_very>[long()]<name>; $short = ...; # Modifies %alias_to

    --Ingo

Re^2: Aliasing bites
by ambrus (Abbot) on May 11, 2005 at 14:20 UTC

    While I understand that perl6 will have this feature, I belive there will be less need for it because of the other changes in the language.

    The times I feel the need of lexical aliases (in perl5) most are nested datastructures and lexical subs.

    For example, when I have a ref-to-hash $h, I can only access elements like $$h{"foo"} (or the arrow notation), which is ugly. It would be much simpler to alias it to %h and then I could just access the elements as $h{"foo"}. Of course, you can make a copy like my %h = %$h;, but that can be slow, and you have to copy it back with %$h = %h; if you modifiy %h. In perl6, however, $$h{"foo"} will become $h{"foo"}, which is a simple enough notation so the problem will be gone. (As a sidenote, there are nonstandard modules for perl5 that allow lexical aliasing.)

    Lexical subs are very similar. Today, the way to create lexical subs is my $frobnicate = sub { ... }; and we call them by &$frobnicate(@args) (or the arrow notation), which is, again, ugly. In perl6, this shall simplify to $frobnicate(@args).

    Of course, perl6 will have lexical aliases like my %h := %$h and true lexical &-variables in addition to $, @, % so you can use aliasing to solve either of these too. TIMTOWTDI.

    Update: lexical aliases wouldn't solve my second problem. We'd need lexical subs for that.

      Perl6 also won't need the arrow notation to dereference values:

      my %hash = (a => 1, b => 2); my $href = \%hash; say %hash<a>++; # prints 1 say $href<a>; # prints 2

      Update: noticed ambrus already mentioned that. Sorry for the repitition. :-)

Re^2: Aliasing bites
by BUU (Prior) on May 11, 2005 at 08:28 UTC
    Perhaps I've missunderstood your post, but what do aliases get you that references don't? Slightly nicer syntax?

      Compare the output from these two snippets:

      $ pugs -e 'my %hash; %hash<foo>[0]<bar>[0] = 1; my $alias := %hash<foo>[0]; say ref $alias; %hash<foo>[0] = 5; say ref $alias' Hash Int $ pugs -e 'my %hash; %hash<foo>[0]<bar>[0] = 1; my $ref = %hash<foo>[0]; say ref $ref; %hash<foo>[0] = 5; say ref $ref' Hash Hash

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-25 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found