Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: RFC: Is there more to alias?

by xmath (Hermit)
on Aug 24, 2004 at 23:12 UTC ( #385541=note: print w/ replies, xml ) Need Help??


in reply to Re^3: RFC: Is there more to alias?
in thread RFC: Is there more to alias?

Doesn't something have to be aliased to something else? What is being aliased to what in these examples?

$x = alias [$y, $z];

$x->[0] is an alias to $y and $x->[1] is an alias to $z.

alias push @x, $y;
$x[-1] is an alias to $y.


Comment on Re^4: RFC: Is there more to alias?
Select or Download Code
Re^5: RFC: Is there more to alias?
by fergal (Chaplain) on Aug 24, 2004 at 23:46 UTC
    Does the first 1 autovivify the arrayref in $x (as $x->[0] = 1 would) or must $x already have an array ref in it? What happens if I do shift @$x? Is $x->[0] now an alias to $z and the alias to $y disappears? Or (unlikely) does it effectively do @{$x} = @{$x}[1..($#$x-1)] which amounts to
    $y = $z; $z = $x->[2]; $x->[2] = $x->[3]; .... $x->[$#$x - 1] = $x->[$#$x]; pop @$x;
    Fun and games!

    The second example one seems very odd to me. To me, it reads as alias(push(@x, $y)) but I guess perl doesn't parse it as that. Why do this rather than just alias $x[-1] = $y or should that be alias $y = $x[-1]?

    So you can use , or = to separate the aliased things? And you can put alias on either side of the =?

    Anyway, time for bed. I dreamt about object pascal last night, I hope I don't dream about aliases to night!

      You still misundestand how Data::Alias works, and you seem to see too much magic where there is none. Data::Alias does no parsing, perl does the parsing, so all syntax is just perl's syntax. Like I said in the description, "alias" itself is a nop. To perl, it's thin air. In fact, if you look at the operations executed (-MO=Concise,-exec), for example for alias push @x, $y:

      3 <0> pushmark s 4 <#> gv[*x] s 5 <1> rv2av[t3] lKRM/1 6 <#> gvsv[*y] sM 7 <@> push[t5] vK/2 - <1> ex-list vK

      See the call to alias? Nope, it isn't even there, apart from a miniscule stub "ex-list" (a nop). Alias just changes the semantics of whatever is inside it, and in a very simple way: whereever perl normally copies data, aliasing occurs instead.

      So, let's analyze the cases. Take $x = alias [$y, $z] for example. What does it do? The [$y, $z] is just the array constructor which normally creates a new array, fills it with copies of $y and $z, and returns a reference to the array. Within the scope of "alias", it therefore creates a new array, fills it with aliases to $y and $z, and returns a reference to the array, which it then normally assigned to $x.

      alias push @x, $y is indeed as you noted parsed as alias(push(@x, $y)). This push would normally add a copy of $y onto the end of @x, so within "alias" it adds an alias to $y onto the end of @x. This obviously differs from alias $x[-1] = $y which would overwrite the last element of @x with an alias to $y.

      You finally mentioned using a comma... ($x, $y) involves no copying, therefore alias($x, $y) does nothing remarkable. It behaves 100% identical to plain ($x, $y).

      All cleared up now? :-)

      Time for me to get some sleep too.. zZ

        I didn't think you were parsing anything but I misunderstood what alias does and so assumed that my parsing of alias push @x, $y was wrong. I'm used to thinking of "alias this to that" rather than "turn on aliasing for this bit of code".

        I know you changed the lexer to make this unnecessary but I think syntax like

        alias { push(@x, y) }
        would give a clearer indication of what is going on. It would also cause problems with scoping when you want to use my:-(

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (11)
As of 2014-08-01 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (8 votes), past polls