Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

It would be lovely if reverse $scalar in list context generated a warning and if reverse; did also (because it becomes reverse $_). But these both need to be based on the syntax used not on the arguments present at run-time. reverse @empty and reverse @one_item must not produce warnings just because @empty is empty or @one_item only contains one item.

If I had a time machine, then I might go back and make the behavior of reverse be based on the syntax of arguments provided to it instead of being based on the context in which it was used. My motivation for doing that is because I believe that passing a scalar to it or passing an array to it is a much better indication of the desired behavior than the context it is used in.

My motivation against it is that I suspect that it would be hard to document the distinction and because it would be a unique case of that distinction in the Perl language.

As to your original question, I think that

... map { reverse } @list;

is a good motivation against both potential defaults. I think that expression is the most reasonable/likely way in which reverse might be used without arguments.

Making that default to map { reverse @_ } @list would be rather silly. You say it might have made you realize what was wrong sooner. I can easily see cases where that would make diagnosing the problem very hard, where it would lead to a lot of confusion.

The fact that map { reverse $_ } @list is a complex no-op is a good reason, IMHO, to make 'reverse' with no arguments simply be a fatal error (and an error that mentions if it had been used in list context).

I can imagine quite a few ways to make reverse DWIM much more effectively. But all of them rather conflict with the tools Perl has built for similar purposes (prototypes and their more powerful brothers that are only available for built-in functions along with scalar vs. list context).

So, the concept of having «a single English word, "reverse", that is overloaded to mean either "reverse list" or "reverse string" depending on how it is used» seems quite Perlish but is at odds with Perl due to details of implementation.

It'd be better to have separate functions for "reverse string" and "reverse list". I might even propose "invert" be the verb for lists, but maybe not. I certainly would make the "reverse string" function only accept a single argument.

- tye        

In reply to Re^3: Why does reverse in list context operate on () instead of @_ or @ARGV (warning) by tye
in thread Why does reverse in list context operate on () instead of @_ or @ARGV by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (9)
    As of 2018-06-18 22:06 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (111 votes). Check out past polls.