in reply to newkid confused: reverse then unreverse a string

my $foo = "My lawyer is named will"; my $oof = scalar reverse $foo;
You may find japhy's sexeger interesting.

Update: Chady, it's a matter of self-documentation and of habit. By using explicit scalar() I say what I expect to happen, and with the habit of using it I can drop it into list context without concern. About the link, I'm delighted to have recommended it for a bowdlerware application.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re: Re: newkid confused
by Chady (Priest) on Apr 06, 2002 at 17:43 UTC

    I've been always using my $oof = reverse $foo; is there any particular reason for the scalar? isn't it already in a scalar context? will it break someday?


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
      Your way is fine. Many people make it an explicit scalar reverse because it isn't always a scalar context. Take for example:
      my $foo = "This is not a palindrome"; print reverse $foo;
      print implies a list context, so reverse will take the list ($foo), and reverse it to ($foo) and print that result. Probably not what is intended. It also can take a while to debug, when the perfectly acceptable-looking reverse statement "doesn't work".

      Thus, if you get in the habit of being explicit about your context, you don't get caught.