Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Executing data based on command line arguments

by iphone (Beadle)
on Dec 15, 2010 at 04:11 UTC ( [id://877199]=perlquestion: print w/replies, xml ) Need Help??

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

I have a block of statements that need to be executed everytime in my code,but when a command line option,say "c" is passed,this block of statements need to be executed based on an "if" condition,is there a way I can achieve this without doing "if else"?

if (grep( /$number/, @list))# this if should come into the picture onl +y when command line option c is given, { #Block of statements... }

Replies are listed 'Best First'.
Re: Executing data based on command line arguments
by ELISHEVA (Prior) on Dec 15, 2010 at 07:35 UTC

    When you find yourself avoiding if...elsif...else because you have group of statements or conditions that you don't want to repeat in two places, that is usually a good sign that those statements/conditions should be wrapped up in a subroutine. Is there any reason why you have avoiding doing this so far?

Re: Executing data based on command line arguments
by PeterPeiGuo (Hermit) on Dec 15, 2010 at 04:15 UTC

    Assume there is nothing there to complicate the matter, the following will do:

    if (c is given && your grep) { ... }

    Peter (Guo) Pei

Re: Executing data based on command line arguments
by eff_i_g (Curate) on Dec 15, 2010 at 04:33 UTC

      I know how to check command line options,let me be explicit in my query again.Lets use the below sample code. In the below code

      1."print $line"should get executed no matter what (even if the option is not c),but 2. when option is "c"am checking for line in @array and then printing $line.

      if ($options(C) && grep /line/,@array) { print $line; }

      I know we can do the following,but how do I satisfy both conditions without using if-else,as an example I gave print $line,there are many more instructions in my script which i dont want to repeat

      if ($options(C) && grep /line/,@array) { print $line; } else #when option "c" is not given { print $line; }
        if ($options{foo} and satisfy_bar()) { poit(); } if (not $options{foo}) { narf(); }
        I think you can get your desired result by doing something like:
        if( ! $option{c} || grep /line/, @array ) { print $line; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://877199]
Approved by planetscape
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-13 21:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found