Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Reverse in scalar context

by tachyon (Chancellor)
on Jul 12, 2001 at 00:45 UTC ( [id://95847]=perlquestion: print w/replies, xml ) Need Help??

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

particle was talking to me about reverse and not being able to get it to work in scalar context. The problem with reverse is the classic Perl scalar versus list context one. Many perl functions can return either a scalar or a list, depending on how they are called. localtime() is a common one that can return suprising results if you wanted the scalar human readable form and insted call it in list context. Reverse to exhibits some interesting behaviour on the surface.

print "The time is ",localtime(); printf "\nor do you prefer %s\n\n", scalar localtime(); $a = "1234"; $b = "4321"; print "Hmm '$a' eq reverse '$b'\n" if $a eq reverse $b; print "But reverse \$b = ", reverse $b; print "\nWhich is not equal to \$a = ", $a; # assign a scalar context to reverse print "\n\nApplying scalar context to reverse\n"; $b = "4321"; $b = reverse $b; print "Now \$b = '$b', which is reversed\n"; # you can assign scalar context to reverse like this to $b = "4321"; printf "Here too reverse \$b = '%d'\n", scalar reverse $b; printf "And also here reverse \$b = '%d'\n", eval reverse $b;

This prints:

The time is 4434612610141920
or do you prefer Thu Jul 12 06:34:44 2001

Hmm '1234' eq reverse '4321'
But reverse $b = 4321
Which is not equal to $a = 1234

Applying scalar context to reverse
Now $b = '1234', which is reversed
Here too reverse $b = '1234'
And also here reverse $b = '1234'

In the example where reverse seems to fail it actually does not. Perl assumes list context in the print and thus reverses the one element list containing $b - this of course does not reverse the content of $b. To get it to work you need to force scalar context. It took me a while to get to grips with this behaviour.

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Reverse in scalar context
by John M. Dlugosz (Monsignor) on Jul 12, 2001 at 01:02 UTC
    I ponder the meaning of eval reverse $b. Is that wise? when $b='bus' for example prints nothing (it's undef).

    —John

      What do you mean with "prints nothing"? What are you trying to print? Note that you are trying to eval the string sub which is a syntax error:
      $ perl -w $b = "bus"; eval reverse $b; print $@; __END__ syntax error at (eval 1) line 2, at EOF
      And if the code doesn't compile, eval will return undef.

      -- Abigail

        If you read the post I was replying to, you'd know that I was not trying to print anything! Rather, I was pointing out this very problem, albeit more subtly, in the original node. I'm quite aware of what it does--I crafted it specifically to do that, yet with a real word might not be immediatly obvious.

      Interesting. Reverse of 'bus' is 'sub'. Hmm. Is it wise, or necessary? - no you have scalar at your disposal to force scalar context safely. Eval-ing strings (especially some strings ;-) reversed or not is potentially dangerous. It ended up there because I wondered what would happen if I put it there, it worked on the test case, so it stayed. Just simple curiosity. Never used it before and I doubt I'll use it again.

      $b = '}"n\!nhoJ olleH" tnirp{b bus'; print eval reverse $b; &b; $_ = 'web&lave'; print eval reverse; sub AUTOLOAD {print}

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: Reverse in scalar context
by dragonchild (Archbishop) on Jul 12, 2001 at 00:55 UTC
    Excuse me if I seem a little ... less than sympathetic, but context is one of the most Perl-ish things out there. In addition, the Camel book does mention something concerning context, specifically with reverse. (First words in the function description of reverse are "In list context, ...").

    I guess I just feel that, with the number of times context is mentioned on the site per day, that ayone who reads it has encountered the concept. *shrugs*

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found