Skip to content


How Well Can You Read Documentation?

(I was going to call this post “How well do you understand context?” but I think this title is more accurate).

I just saw someone recommending this code:

$reversed = reverse(split //, $string);

Looks sensible enough, doesn’t it? But it isn’t. What’s the hidden inefficiency?

Posted in Programming.

Tagged with , , , .


7 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. David Dorward says

    And now I’ve learned something new about reverse. Hooray for DWIM.

  2. Lars Balker says

    Huh.

    I don’t think that’s a sane behaviour for reverse in scalar context (only ever used it with a single argument in scalar context), but I’ll be damned if I can come up with a better alternative.

  3. Conor says

    Why not just: $reversed = scalar reverse $string; ?

    • Dave Cross says

      Well, quite.

      Although your call to scalar is unnecessary.

      • Ludo says

        Why does reverse $string return the original string, but an assignment e.g. : $reversed = reverse $string store the reversed string in the lvalue?

  4. jvector says

    late joining the party. SOme of these ‘dwimminess’ features make me a bit uncomfortable. I feel that if c gives me ‘fred’ then maybe it’s not very /useful/, but at least it’s consistent with my simple user model of the language. When convenience measures like making that result be ‘derf’ are mixed into the language, then it seems to me kind of like encountering an irregular verb in a natural language: it may make life easier on this one occasion for somebody but the whole edifice gets just a bit more complicated … adding to the meme that Perl is ‘too hard’.



Some HTML is OK

or, reply to this post via trackback.