Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

variable processing and checking in one statement

by saunderson (Novice)
on Aug 06, 2012 at 22:09 UTC ( #985846=perlquestion: print w/replies, xml ) Need Help??
saunderson has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

i wonder whether it's possible to simplify constructs like

doSthWith($test) if checkFn($test);
further, such that i have to write the variable ($test) only once!?

I often have to deal with such statements and i just don't want to use perl's default variable, so that it yields
$_=$test; doSthWith($_) if checkFn($_);
since $_ is often used by other statements in the same block.

How would you monks do it the neat way?

best regards

Replies are listed 'Best First'.
Re: variable processing and checking in one statement (for my)
by tye (Sage) on Aug 07, 2012 at 00:13 UTC
    for my $t ( $test ) { doWith($t) if check($t); }

    avoids $_ completely. Clearly not worth it if your variable is actually just $test.

    - tye        

      Thank you for your contribution. the for respectively the foreach approach will cetrainly find their ways in my future scripts ...
Re: variable processing and checking in one statement
by BrowserUk (Pope) on Aug 06, 2012 at 22:26 UTC

    You could do:

    checkFn( $_ ) and doSthWith( $_ ) for $test;

    which wouldn't interfere with surrounding uses of $_; and avoids referencing $test twice.

    But I wouldn't unless the variable in question was a complex reference. Ie. a multi-level hash or array lookup.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: variable processing and checking in one statement
by ig (Vicar) on Aug 07, 2012 at 04:37 UTC

    How about


    Have checkFn return the passed data if all is well, or undef if not and have doSthWith check its arguments.

    Or, incorporate the check into doSthWith - depending on where you want to localize the knowledge about the condition, and how you want to handle the race between the check and what you do with $test

    And, I don't advocate this but find the range of possibilities interesting and can't seem to stop myself from updating:

    sub { doSthWith($_[0]) if(checkFn($_[0])); }->($test);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985846]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2018-06-25 18:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (128 votes). Check out past polls.