Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^3: Why does reverse in list context operate on () instead of @_ or @ARGV (warning)

by tye (Cardinal)
on Aug 26, 2012 at 18:08 UTC ( #989860=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Why does reverse in list context operate on () instead of @_ or @ARGV
in thread Why does reverse in list context operate on () instead of @_ or @ARGV

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        


Comment on Re^3: Why does reverse in list context operate on () instead of @_ or @ARGV (warning)
Select or Download Code
Re^4: Why does reverse in list context operate on () instead of @_ or @ARGV (warning)
by moritz (Cardinal) on Aug 27, 2012 at 18:39 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989860]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (12)
As of 2014-12-18 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls