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

Re^3: do $n exist in grep?

by davido (Cardinal)
on May 12, 2021 at 16:58 UTC ( [id://11132503]=note: print w/replies, xml ) Need Help??


in reply to Re^2: do $n exist in grep?
in thread do $n exist in grep?

I use a similar idiom in formulating a return responses or external calls, sometimes:

my ($stdout, $stderr, $ret) = capture { system('foo', ($somearg ? ('--somearg' => $somearg ) : ()), ($otherarg ? ('--otherarg' => $otherarg) : ()), ); };

...or...

return { status => $s, ($optional_field ? (optional_field => $optional_field) : ()), };

Caveat emptor. The need to do either of these may constitute code smell, but sometimes purity has to give way to get it done-ery.


Dave

Replies are listed 'Best First'.
Re^4: do $n exist in grep?
by choroba (Cardinal) on May 12, 2021 at 17:02 UTC
    It's also possible to shorten the lines with
    ('--somearg' => $somearg ) x !! $somearg, ('--otherarg' => $otherarg) x !! $otherarg,

    but only if the left hand side of x doesn't have any side-effects (because unlike the ternary operator, foo() x 0 still calls foo).

    See also PerlX::Maybe.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

      That's interesting. I'm not sure it qualifies as coding for clarity, but perhaps neither does my example.


      Dave

Re^4: do $n exist in grep?
by Marshall (Canon) on May 12, 2021 at 20:46 UTC
    Interesting:
    return { status => $s, ($optional_field ? (optional_field => $optional_field) : ()), };
    I personally would not make a hash key optional like that.
    I would always return the hash key and then an undef value for that key if it is not valid.
    But as is often said "there is more than one way to do it".
    It could also be that I haven't thought this completely through and haven't considered what
    the calling code looks like and how it processes this returned info.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2026-01-17 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (121 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.