Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Alias vs Reference

by ikegami (Pope)
on Jun 16, 2012 at 21:02 UTC ( #976598=note: print w/ replies, xml ) Need Help??


in reply to Alias vs Reference

The most general you can do without a module is

my ($y) = map \$_, $x; # Reference to an alias.
my $y = sub { \@_ }->(@x); # Reference to an array of aliases.


Comment on Re: Alias vs Reference
Select or Download Code
Re^2: Alias vs Reference
by BrowserUk (Pope) on Jun 17, 2012 at 01:43 UTC
    The most general ...

    That method of aliasing is interesting and occasionally useful, but it is hardly "The most general".

    Besides that a reference to an array of aliases is hardly as convenient as a typeglob aliased array, it is also a very fragile structure.

    As soon you do anything that modifies the referenced array -- rather than the things aliased by its contents -- the aliases cease to be aliases.

    Eg. Here I've used splice to modify the array, but push, pop, shift, unshift, assignments to $#$silly and anything else will silently break the aliasing affect resulting in two entirely separate, unconnected arrays:

    #! perl -slw use strict; my @a2 = 'a'..'z'; my $silly = sub{ \@_ }->( @a2 ); splice @{ $silly }, 12, 1, 'the middle'; print "@{ $silly }"; print "@a2"; __END__ C:\test>junk a b c d e f g h i j k l the middle n o p q r s t u v w x y z a b c d e f g h i j k l m n o p q r s t u v w x y z

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      That method of aliasing is interesting and occasionally useful, but it is hardly "The most general".

      It's not limited to a scope like for and map aliasing. I did forget about glob aliasing.

      my $y = do { local *temp = \$x; \our $temp };

      It's more general in its breadth of types of aliases it can create, but it can't create an array of aliases.

      resulting in two entirely separate, unconnected arrays:

      You seem to have misread what I wrote. I didn't say it aliased the arrays.

        You seem to have misread what I wrote. I didn't say it aliased the arrays.

        No. I know you you never said that.

        I was just pointing out that the difference between "a reference to an array of aliases" and an aliased array is subtle enough that it is easy to be surprised by the difference in their behaviors as code evolves over time.

        typeglob aliasing on the other hand is easy to understand and reliable in its behavior. Much under used in my opinion.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

        The start of some sanity?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-26 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls