Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re^4: Repurposing reverse

by ikegami (Pope)
on Jan 31, 2011 at 08:56 UTC ( #885215=note: print w/replies, xml ) Need Help??

in reply to Re^3: How to reverse a (Unicode) string
in thread How to reverse a (Unicode) string

The documentation is quite clear: In scalar context, the reverse function operates on strings


You're once again trying to make some esoteric point about the distinction between strings and bytes,

No. Quite the opposite, I'm saying there is no distinction. reverse doesn't know or care what the string is, and has no way of knowing. It will reverse the characters (elements?) of the string.

What is a character in a language is well-understood and rarely, if ever, subject to debate

You opened the debate! Again, a common CS definition is used here: A string is a sequence of elements named characters. Would you care to suggest an alternative?

What is a character in a language is well-understood and rarely, if ever, subject to debate. In the case of Unicode, "character" is well-defined, too: It's a "grapheme." It's as simple as that. Read the Unicode Standard.

Unicode has four definitions for "character", and none correspond exactly to that of "grapheme".

But it's irrelevant. Again, reverse function isn't to manipulate text.

There are four characters in the word "Café", not five.

Yes, but irrelevant. reverse's function isn't to manipulate words. There are five characters in the string chr(0x43).chr(0x61).chr(0x66).chr(0x65).chr(0x301).

Your insistence that there's a difference between "string" and "text" may have some strange basis in the arcane details of the internals of Perl,

Again, quite the opposite. It has nothing to do with Perl internals. It's not even specific to Perl. A string is a data type. Text is one of many things that can be stored in a string.

my $x = "abcd"; # String? yes. Text? my $host = inet_ntoa($x); # No, "packed" IP address
my $x = "abcd"; # String? yes. Text? print("The password is $x\n"); # Yes.

You're suggesting that Perl should reverse strings as if they're text. For example, you say


should return


But that's wholly inappropriate for water level measurements or for anything else the string might be.

There is need for a function that reverses strings (chr(101).chr(769) ⇒ chr(769).chr(101)). There is also a need for a function that reverses text (chr(101).chr(769) ⇒ chr(101).chr(769)). reverse does the former.

Replies are listed 'Best First'.
Re^5: How to reverse a (Unicode) string
by Jim (Curate) on Jan 31, 2011 at 09:28 UTC

    You're just blowing the same old tired, incomprehensible smoke here as in so many other discussions on PerlMonks about Unicode. Until you can make a compelling case for why this Perl code…

    use utf8; binmode STDOUT, ':encoding(UTF-8)'; print reverse "Réaliste";

    …should produce different output than this Perl code…

    use utf8; binmode STDOUT, ':encoding(UTF-8)'; print join '', reverse "Réaliste" =~ m/\X/g;

    …you're just arguing for the sake of argument about esoteric matters that aren't relevant at all to the topic at hand.

      in so many other discussions on PerlMonks about Unicode.

      The discussion isn't about Unicode, it's about reverse. reverse doesn't perform a Unicode operation.

      Until you can make a compelling case

      So it doesn't break this code. Not everyone limits themselves to using strings as you do. I'm sorry that I'm failing to make you see that, but I'm at wits end.

        The discussion isn't about Unicode…

        Yes it is about Unicode. "Unicode" is in the subject. Please go start your own discussion of water level measurements somewhere else.

        reverse doesn't perform a Unicode operation.

        Yes it does. Every time it reverses a Unicode string, it performs a Unicode operation.

        What kind of operation is this Perl code performing?

        use utf8; $utf8_text =~ s/\p{General_Category=Currency_Symbol}+/€/g;

        Not a Unicode operation?

        So we're in agreement, the documentation for reverse needs to be updated to clarify what it does, right?
        And finally, scalar reverse() reverses by character rather than by byte.
        perldoc -f reverse
        In scalar context, concatenates the elements of LIST and returns a string value with all characters in the opposite order.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://885215]
[1nickt]: tobyink perl -MTypes::Standard= is_Int -Mstrict -wE 'say 1 if is_Int 1.0'
[jdporter]: so that I can use the existing expand unix util. Otherwise, I'll probably use Text::Tabs.
[1nickt]: pryrt I guess I don;t really care if user 42 logs on as 42.0 ... more of an academic question at this point.
[LanX]: jdporter: open PIPE,'-|' ?
[LanX]: oh you want the result line by line?
[jdporter]: ok, LanX, then what?
[jdporter]: It doesn't have to be line by line. Just "my program" "writes" to the external prog and also/then "reads" from it.
LanX open (You are not allowed to open to a command that pipes both in and out, but see IPC::Open2, IPC::Open3, and Bidirectional Communication with Another Process in perlipc for alternatives.)
[jdporter]: IPC::Open2, I guess
[jdporter]: yes, that

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (11)
As of 2017-05-24 20:29 GMT
Find Nodes?
    Voting Booth?