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

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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.


In reply to Re: Re: Suggestion for teaching Perl by nysus
in thread Suggestion for teaching Perl by nysus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

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

      What is your favourite meta-syntactic variable name?














      Results (18 votes), past polls