Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

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


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

The hard part was getting any sort of "trigger" on the occurrence of alias (which is just a declared but undefined sub). The breakthrough was the discovery of PL_check and the fact it's editable.

This array contains a list of "check functions" which are invoked whenever a node is created in the op-tree. I used it to trigger on OP_RV2CV (the occurrence of "alias" or "copy") and OP_ENTERSUB (the completion of the op-subtree that invokes it). I do some initial modifications of the op-tree, and in particular mark is so I can find it later.

When alias/copy has been seen I also temporarily override PL_peepp. There I scan the optree for markings and do the actual work, which consists mostly of setting the op_ppaddr of various ops inside alias to aliasing equivalents.

A final piece of the puzzle is supporting alias BLOCK which involves a lexer hack, but is mostly unrelated to the rest.

It may indeed be applicable in other situations. I was already thinking about how I could make this mechanism more reusable.

Any further details you want?


Comment on Re^2: RFC: Is there more to alias?
Select or Download Code
Re^3: RFC: Is there more to alias?
by fergal (Chaplain) on Aug 24, 2004 at 23:00 UTC
    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?
      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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-11-28 17:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (199 votes), past polls