Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by fergal (Chaplain)
on Aug 24, 2004 at 23:00 UTC ( [id://385539]=note: print w/replies, xml ) Need Help??


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

Interesting but scary! It looks like the same technique could be used to implement compile-time checking of method calls, so I could do something like
type Dog my $a; $a->bark; $a->$trick; # compile time warning maybe $a->perform_brain_surgery; # compile time error
a bit like Java interfaces except far less painful. So it would pick up typos in method names but doesn't try to look at the Class of $a (although an optional assertion might be useful).

Rereading your original posting, I can't follow

$x = alias [$y, $z]; alias push @x, $y;
Doesn't something have to be aliased to something else? What is being aliased to what in these examples?

Replies are listed 'Best First'.
Re^4: RFC: Is there more to alias?
by xmath (Hermit) on Aug 24, 2004 at 23:12 UTC
    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.
      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-25 09:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found