Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: NEWBIE Brain Teaser

by nysus (Deacon)
on Apr 15, 2001 at 08:27 UTC ( #72639=note: print w/ replies, xml ) Need Help??


in reply to Re: Re: NEWBIE Brain Teaser
in thread NEWBIE Brain Teaser

Yes, although I do not know if "referencing" is the accurate term. As the post above mine pointed out, the book "Learning Perl" uses the term alias. How that is different from referencing, I'm not entirely certain and maybe a more monkish Monk than I could answer that.


Comment on Re: Re: Re: NEWBIE Brain Teaser
Re: Re: Re: Re: NEWBIE Brain Teaser
by merlyn (Sage) on Apr 15, 2001 at 08:29 UTC
      Alright, then in learning from this brain teaser not only have I learned the original point, but also stumbled upon another concept that I need to read up on. Thank you!

      I take it then that aliasing was meant to make our lives easier both in how we code as well as in how we think about the whole process?

        Yes, that is what aliasing is supposed to do.

        For instance consider this:

        # No aliasing... foreach my $thing (@array) { # $thing is now an *alias* for something in @array # In reality there is a layer of indirection here # from the concept of $thing to the array, but you # do not have to write the dereferencing since Perl # hid it for you in $thing # ... } # Alias over
        See? There is indirection needed to do what happens in that loop, but you don't have to think about it, and it does not appear as explicit dereferences in your code. It just works.

        That is the difference between an alias and a reference. With a reference the indirection shows up as your having to tell Perl to dereference things. With an alias it is hidden inside the definition of the variable.

        That is an amusing difference between Perl and, say, C. The same code written in C is in reality more direct than in Perl. But the Perl code *looks* more direct because you don't see the indirections...

Re: Re: Re: Re: NEWBIE Brain Teaser
by hiroki (Novice) on Apr 16, 2001 at 19:29 UTC
    i am not a "more monkish Monk", but i did get curious about this behavior of foreach. This is an alias (example modified from _Advanced Perl Programming_ (O'rielly) by Sriram Srinivasan):
    $a = 10; # saclar a @a = (1, 2, 3); # array a *b = *a; # aliases b to a $a++; # increments $a :) $b++; # same as saying $a++ print "$b, $a"; # prints: 12 12 @b[0] = 4; # same as saying $a[0] = 4
    as you can see, the aliasing on line 3 makes any manipulation of $b, @b or %b manipulate $a, @a, %a respectively. hope this helps and is not too confusing. if i'm wrong, tell me :)
      That aliasing command relies on typeglobs to work.

      Therefore it will only work with global variables. (ie you must localize with local, not my.) I stay away from that except when it really doesn't make sense not to. :-)

      The mostly widely used form of that kind of aliasing in modern Perl is for exporting symbols using Exporter.

      FWIW one goal for Perl 6 is to kill typeglobs entirely. The functionality should be available, but by a different mechanism...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2014-07-13 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (249 votes), past polls