Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Alias vs Reference

by cheekuperl (Monk)
on Jun 16, 2012 at 04:13 UTC ( [id://976523]=perlquestion: print w/replies, xml ) Need Help??

cheekuperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,
What exactly is the difference between an alias and a reference?
for (@arr) #Aliases $_ to each element in @arr { print; # prints the aliased value } $aref=\@arr; #Makes $aref reference to @arr
However, I am wondering how can I create an alias explicitly (without using any extra module).

Replies are listed 'Best First'.
Re: Alias vs Reference
by BrowserUk (Patriarch) on Jun 16, 2012 at 04:42 UTC
    What exactly is the difference between an alias and a reference?
    • An alias is another name that refers to the same thing as the first.

      In your first example, $_ is another name that can be used to refer to each of the elements $arr[0], $arr[1] etc. in turn.

    • A reference, is the "address" of the thing it references.

      If your second example, $aref refers to the entire array. To get to individual elements of that array you must dereference it and index it. eg. $aref->[0], $aref->[1] etc.

    how can I create an alias explicitly (without using any extra module).

    You can do this using typeglobs.

    Eg.

    #! perl -slw use strict; ## An array with some contents my @array = 'a'..'z'; ## Tell strict we are going to use a global our @alias; ## Set the array reference of the typeglob *alias ## to point to the lexical array we set up earlier *alias = \@array; ## Now when we use @alias, we are (also) using @array print $alias[ 0 ], ' : ', $alias[ 25 ]; ## changes to either affect both ## Or rather change via either name ## affect the same storage $alias[ 0 ] = 'fred'; $array[ 25 ] = 'bill'; print $alias[ 0 ], ' : ', $alias[ 25 ]; print $array[ 0 ], ' : ', $array[ 25 ]; __END__ C:\test>junk a : z fred : bill fred : bill

    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?

      Thanks! That clears all confusion!
Re: Alias vs Reference
by eyepopslikeamosquito (Archbishop) on Jun 16, 2012 at 07:46 UTC

    Some places aliases are used:

    • $_ in foreach, map and grep and named loop variables in foreach
    • $a and $b in a sort block
    • $a and $b in List::Util's reduce function and List::MoreUtils's pairwise function
    • Each element of @_ for the actual arguments in a subroutine call
    • By packages importing symbols
    • By an our declaration, which creates a lexically scoped alias
    • You can explicitly create an alias via typeglobs

    See also "alias" in perlglossary and Item 118 "Access the symbol table with typeglobs" in Effective Perl Programming.

    Update: added named loop variables in foreach. Thanks Jenda.

      Not just $_ in foreach. Named loop variables in foreach are also aliases.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.

Re: Alias vs Reference
by Anonymous Monk on Jun 16, 2012 at 04:24 UTC

    What exactly is the difference between an alias and a reference?

    You have to de-reference a reference , but not an alias.

    However, I am wondering how can I create an alias explicitly (without using any extra module).

    Use foreach :) or map or grep ... or use a sub, @_ is always aliased, but as soon as you make a copy, the alias is broken

    Unless you use a module from cpan, you can't go nuts with aliases

Re: Alias vs Reference
by ikegami (Patriarch) on Jun 16, 2012 at 21:02 UTC

    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.
      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.

Re: Alias vs Reference
by Anonymous Monk on Jun 18, 2012 at 13:39 UTC
    Also, be mindful in the course of any human conversation that the two terms might simply be used as though they were synonymous ... with none of the deep-meaning that someone else might ascribe to each.   When walking on the razor edge of meaning, be sure that the conversant is actually using the meaning that you think he is; that you are not reading too much into it.   Perl has many idioms, e.g. "perldoc perlvar" for doing and saying the same things in different ways.   Everyone has their own preference; gets used to doing it and saying it that way; might not even be aware of alternates.   So, know where they are coming from.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://976523]
Approved by BrowserUk
Front-paged by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found