Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Array Plus Operator

by SleepyJay (Beadle)
on Dec 15, 2017 at 14:41 UTC ( [id://1205586]=perlquestion: print w/replies, xml ) Need Help??

SleepyJay has asked for the wisdom of the Perl Monks concerning the following question:

I am deep in some legacy code (not written by me) that looks like this (it's surrounded by an if):
@{ $self->bar || +[] }

What's with that '+' in there? I mean, I get that it is usually used for disambiguation (e.g. @{ $self->bar || +shift }). But, what makes this plus needed?

(I'm not a Perl beginner, but I have never seen Perl like this: is this a dumb question?)

Replies are listed 'Best First'.
Re: Array Plus Operator
by shmem (Chancellor) on Dec 15, 2017 at 14:58 UTC

    There's no difference between these two lines

    @{ $self->bar || +[] }; @{ $self->bar || [] };

    since there is no ambiguity. [] returns a scalar - a reference to an anonymous, empty array - in either case. But the original author perhaps thought it was needed. AFAIK this doesn't behave different in any perl5.

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      That's what I assumed, but thanks for the sanity check! Odd looking legacy code gives me deep paranoia when changing it!

      The current theory in the office is that someone "fat-fingered" it... since the "+" and "[" are so close on the keyboard... otherwise... NFI

      Or maybe you could pass in a default to use if $self->bar was false... so it was originally @{ $self->bar || +shift } and then the code evolved and someone replaced the "shift" with "[]" and left the "+" around, JIC

        ... someone "fat-fingered" it... since the "+" and "[" are so close on + the keyboard...

        But "+" also requires a shift, so it would have to be a "hyper-caffeinated-fat-finger."


        Give a man a fish:  <%-{-{-{-<

Re: Array Plus Operator
by QM (Parson) on Dec 18, 2017 at 11:09 UTC
    With legacy code, and things like this that don't make sense superficially, think about other elements that might have been there before that needed disambiguation, but were changed to what you see.

    Here's an overly dramatic explanation of a different kind. Just imagine that originally there was an empty hash constructor, with a leading plus: +{}. Sometime later it was changed to an empty array, +[]. Since the expression compiled with the plus, and didn't change the result, it was overlooked by the author and left to furrow future brows.

    (I must admit Poe's Dupin is my favorite detective, even above Sherlock, precisely for this mind-reading ability, fantastic as it must be.)

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

Re: Array Plus Operator
by Anonymous Monk on Dec 16, 2017 at 00:47 UTC
    Legacy-code should always give you deep paranoia. It goes with the territory. Make each change as you find them, one at a time, taking care to bracket them or to tag them in your change-control system. If you find that you need to make such a change, let it be a source-control "commit" of its very own.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205586]
Approved by 1nickt
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (None)
    As of 2024-04-25 02:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found