Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Suggestion for teaching Perl

by nysus (Deacon)
on Apr 14, 2001 at 01:31 UTC ( #72489=monkdiscuss: print w/ replies, xml ) Need Help??

Since I'm a newbie, I can't contribute too much over at "Seekers of Perl Wisdom". Instead, I've given some money and bought a t-shirt. Still, I yearn to find a way to more directly contribute to the PerlMonk community---the most entertaining thing I've ever found on the internet. So I offer this suggestion:

How about if we start a "Brain Teaser" section to PerlMonks? You could have "Newbie", "Intermediate" and "Perl Monk Master" sections. People could measure their skill level by seeing which brain teasers they are able to answer. It's also a great way for everyone to contribute through the offering of brain teasers. So here's my first brain teaser, at the "Newbie" level, of course.

Problem:
What do you think the output of the following code is? Can you explain why? If you are stumped, click "Reply" to see the answer.

#!/usr/bin/perl -w use strict; my $aloha = "hello"; print reverse($aloha);

2001-04-14 Edit by Corion : Fixed some typos per request

Comment on Suggestion for teaching Perl
Download Code
Re: Suggestion for teaching Perl
by nysus (Deacon) on Apr 14, 2001 at 01:32 UTC
    RTFM! :-)
Re: Suggestion for teaching Perl
by nysus (Deacon) on Apr 14, 2001 at 01:34 UTC
    That last post is a cruel joke, of course.

    The above script yields "hello." Are you wondering why didn't it produce "olleh"? Then the explanation appears here (skip down this page a bit for the actual explanation) and the newbie comes away smarter and more endeared to PerlMonks.org!

Re: Suggestion for teaching Perl
by premchai21 (Curate) on Apr 14, 2001 at 03:23 UTC
    At the risk of metooing, I think that's a good idea.
Re: Suggestion for teaching Perl
by bobione (Pilgrim) on Apr 14, 2001 at 14:38 UTC
    Well... Let's try to answer (as I am a newbie :)
    I found your question very easy. I am sure I am wrong so I take my camel book to see reverse's section and I seek something which I would not have seen before.
    I am not sure that reverse ($aloha) is assumed as a LIST or a string. But my answer is "hello" ! ... and reverse ("$aloha") will return "olleh".

    BoBiOne KenoBi ;)
      You are correct that reverse in a scalar context will reverse the bytes in its argument. So, my $x = reverse($aloha); would result in assigning $x to "olleh".

      However, the output of this particular piece of code is not "olleh" at all! The output of above code is just plain old "hello".

      Why? Well, it's because in this instance, reverse is being used in a list context. To prove to yourself that it is being used in a list context, run this bit of code through Perl:

      #!/usr/bin/perl -w use strict; my $aloha = "hello"; my $adios = "goodbye"; print reverse($aloha, $adios);
      What happens now? The result is "goodbyehello"! This is precisely how you would expect reverse to work in a list context: it reverses the order of the elements in the list.

      Finally, try this piece of code:

      #!/usr/bin/perl -w use strict; my $aloha = "hello"; print scalar(reverse($aloha));
      The scalar function forces a scalar context an indeed, the output yields "olleh".

      So what is putting reverse($aloha) in a list context? It is our friend, the print function. If you kindly refer to your Camel book, or your Perldoc, or your Learning Perl book or whatever handy refernce you have a available, you will see that print places its arguments into a list context.

      So, looking back at our original piece of code, the reason why it prints just plain old "hello", is that it is reversing the elements in the list and not the bytes in the string. Since there is only one element in the list, the list reversed is precisely same as the original list!

      Monks, please chime in if this explanation is not exactly accurate.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-07-12 00:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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








    Results (237 votes), past polls