Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by tye (Sage)
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        

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://989860]
[stevieb]: shmem thanks for the 'insight' :P
[shmem]: good thing that Sun already took "OpenWindows", otherwise I'd not stop to shudder imagining an "OpenWindows" from MS
[shmem]: more garbage in, more garbage out that would be
[stevieb]: I found that win10 broke a C# library I was using for one project while enhancing tests for a Perl dist, which breaks other Perl dists, and I'm about to throw my hands up on berrybrew. win2k12 broke one thing, win10 breaks something...
[stevieb]: ...unrelated which requires replacing a lot of code and a whole lib. I'm about to go nix only ffs
[shmem]: stevieb: what you're doing sounds afwully complex. Too much for me this evening to provide brighter insight ;-)
[stevieb]: I don't even own a Windows computer. Both my girl and I have a laptop each with Linux. I'm supporting Windows in some of my projects and I can't even guage whether it's worth it or not.
[stevieb]: shmem It's something I desired to have years ago, which is why I took over berrybrew. Cross-platform build/test automation locally, or over the network Test::BrewBuild
[shmem]: sounds good.
[shmem]: but I'm crumbling smaller stones. remember...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2017-03-28 22:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (342 votes). Check out past polls.