Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Possible useless use of map

by JavaFan (Canon)
on Mar 18, 2010 at 15:07 UTC ( [id://829420]=note: print w/replies, xml ) Need Help??


in reply to Re: Possible useless use of map
in thread Possible useless use of map

I personally think that "bare maps...maps that don't use the output of the map" are not a good idea although other Monks would disagree.
Uhm, Perl is context driven. Whether operations "output" 0, 1 or many things isn't up to them. It's up to the context. void context means an operator doesn't return any values.

Replies are listed 'Best First'.
Re^3: Possible useless use of map
by Marshall (Canon) on Mar 20, 2010 at 00:37 UTC
    I guess a few things got mixed together in this thread.
    @output = map{some code}@input;
    In older versions of Perl the @output will be created whether you give it a name like: @output or not. In other words there will be some @some_anon_thing for which you don't know the "name". I understand this has been fixed as of 5.10. So a version like Perl 5.6 has a "penalty" for a bare map.

    On the style issue, I use map{} for short transformations where I use the output of the map{}, maybe @$aref or some such thing. If it gets hairy, I use a foreach(). I personally don't use "bare" map{}'s as a shorthand foreach(). Now doing so is completely legal and so certainly doesn't rise to the level of "hey that's wrong"! I prefer foreach in those situations partly because I have the thing we are looping over right there at the start of the statement and also that since I use map in consistent style, I can see immediately that we are going some kind of transformation, deg F to deg C or whatever. Also Perl is terse enough that I don't see the need to save a few characters by "bare" map vs foreach. Now sometimes the situation arises where I use a map{} within a foreach, like foreach (map{...}@input), but that is using the output of the map and fits with the short transformation idea.

    I am sure that others have different opinions. I've tried to explain what I do and why - I hope that was understandable and appears rational and consistent. I'm not the "code police".

    Coding has a lot of "art" to it. And there are all kind of exceptions for every "rule".

      In older versions of Perl the @output will be created whether you give it a name like: @output or not.
      Wrong. It doesn't now, and it never did. What it did was push a collection of SVs on an internal stack. But it didn't package that list into an array structure. And don't confuse what happens in the implementation with what happens on the language level.
      I understand this has been fixed as of 5.10.
      Off by more than 4 years. The bug was fixed1 as of 5.8.1, which was released in Sep 2003. Oh, and BTW, 5.10 is over 2 years old - can we please stop treating 5.10 as something new and scary? Jesse is gearing up to release 5.12-RC0 very soon.

      1The fix turned out to be a one line patch. Can you imagine, for years people tried to let programmers dance to the drum of language (quite opposite of what Perl is supposed to be), while the fix was rather trivial. And now, 6.5 years down the road, people still use the argument to argue against a map in void context.2

      2I think only people that always use the return value of print are allowed to whine about a map used in void context.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-28 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found