Perl seems to be much cleverer than I thought :-)

print "\L\uhElLo"; # Hello
I thought there would be "hello", but no: it says "Hello", (I wanted to say so) as if I typed
print "\u\LhElLo"; # Hello
But this changement doesn't work with lc and ucfirst:
print lc("\uhElLo"); # hello print lc(ucfirst("hElLo")); # hello print ucfirst(lc("hElLo")); # Hello
I do love perl!

Best regards,
perl -e "s>>*F>e=>y)\*martinF)stronat)=>print,print v8.8.8.32.32.32"

Replies are listed 'Best First'.
Re: Cleverness - \L\u in strings
by BUU (Prior) on May 25, 2002 at 20:15 UTC
    Im not sure about the \L and \u charachters, clearly they have some defined order of operations, which appears to be from left to right. I.e. for your first example, first the \L triggers, making everything lower case, then the \u triggers, making the first uppcase. This can be easily seen with the functions below, as you can clearly see the order of operations.
      The order in which \L and \u are applied isn't necessarily left to right... you get the same result from "\u\LhElLo" as you do from "\L\uhElLo". Rather, perl is behaving as it thinks the programmer intended by putting \u and \L right next to each other, in either order.

      (Somewhat off-topic)... I could swear that I've read an anecdote before that went something like this: Someone expected "\L\uhElLo" to produce "hello", not "Hello", because he knew that they were implemented internally with ucfirst and lc. He expected the behavior to be the same as lc(ucfirst("hElLo")). But he was pleasantly surprised to find that it actually produced "Hello". When he mentioned this to Larry, Larry just smiled (and maybe said something pithy). Has anyone else heard this anecdote? I've spent probably an hour looking through my Perl books and doing Google searches, trying to find it, and it's driving me nuts.

      -- Mike

      --
      just,my${.02}