Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Checking Wrong Condition

by Divakar (Sexton)
on Apr 25, 2012 at 08:04 UTC ( #967009=perlquestion: print w/replies, xml ) Need Help??
Divakar has asked for the wisdom of the Perl Monks concerning the following question:

Hi Folks,

When i execute my script as below rel=102b file=ccmsg.2
it is coming out of the script after printing below statement. it is checking the wrong condition.. i dont know why. can someone please help?
[Error] - Wrongly passed the parameter. Please use correct format as below [Example] - <scriptname> rel=<release> file=<file_name>(./check_build_ rel=102b file=ccmsg.1)

I am checking the input parameters passing to the script and validating the parameters before return. below is my subroutine.

if ($no_of_arg == 2) { my($inp1,$inp2)=@ARGV; chomp($inp1,$inp2); print "inp1 is $inp1 inp2 is $inp2\n"; print "inp1 is $inp1 inp2 is $inp2\n"; my ($opt1,$value1,$opt2,$value2)=""; if ($inp1 =~ /^rel=\w+$/i) { ($opt1,$value1)=split(/=/,$inp1); } if ($inp2 =~ /^file=\w+$/i) { ($opt2,$value2)=split(/=/,$inp2); } if ($inp1 !~ /^rel=\w+$/i or $inp2 !~ /^file=\w+$/i) { print "\n[Error] - Wrongly passed the paramete +r.\n"; print " Please use correct format as +below\n"; print "\n[Example] - <scriptname> rel=<release +> file=<file_name>($script_name rel=102b file= ccmsg.1)\n\n"; exit; } return($value1,$value2); }
Thank you..

Replies are listed 'Best First'.
Re: Checking Wrong Condition
by moritz (Cardinal) on Apr 25, 2012 at 08:22 UTC
      Thanks. i didnt recognize that.
Re: Checking Wrong Condition
by BrowserUk (Pope) on Apr 25, 2012 at 08:29 UTC

    The '.' in ccmsg.2 doesn't match \w+ in /^file=\w+$/i

    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?

      Thank you. i didnt recognize that.
Re: Checking Wrong Condition
by 2teez (Vicar) on Apr 25, 2012 at 09:15 UTC

    The regex you used are not matching, are your if statements in a subroutine? If not why use a 'return'
    However, the code below could guide you:

    use warnings; use strict; die "Enter proper CLI arugments: ......." unless @ARGV == 2; ## check your arguments passed from CLI my ( $num1, $num2 ) = show_now(@ARGV); print $num1, "\t", $num2, $/; sub show_now { my ( $inp1, $inp2 ) = @_; print "inp1 is $inp1 inp2 is $inp2\n"; my ( $opt1, $value1, $opt2, $value2 ) = ""; if ( $inp1 =~ /rel=.+?/i ) { ## changed the regex ( $opt1, $value1 ) = split( /=/, $inp1 ); } if ( $inp2 =~ /file=.?+/i ) { ## changed the regex ( $opt2, $value2 ) = split( /=/, $inp2 ); } if ( $inp1 !~ /rel=.+?/i or $inp2 !~ /file=.+?/i ) { print "\n[Error] - Wrongly passed the parameter.\n"; print " Please use correct format as below\n"; print "\n[Example] - <scriptname> rel=<release> file=<file_name>(\$script_na +me rel=102b file= ccmsg.1)\n\n"; #exit; ## not really needed } return ( $value1, $value2 ) if wantarray(); }

    Hope this helps

      thanks for the response.

      yes. i am using the if condition in my subroutine. can you please explain more about wantarray. i read in perldoc. but couldnt understand much as how it will work here..

        Subroutine can return both scalar and list context values, so return a list context values as in our case here. One can use wantarray() to tell which context as stated in the perldoc -f wantarray documentation, and I quote "Returns true if the context of the currently executing subroutine or "eval" is looking for a list value. Returns false if the context is looking for a scalar. Returns the undefined value if the context is looking for no value (void context)."


        my @values=get_values(); # subroutine called print @values; ## print 12345 not 1,2,3,4,5 sub get_values{ my @init_val=grep $_=>1..5; if(wantarray){ # test list context return @init_val; }else{return join",",split} }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-05-21 23:18 GMT
Find Nodes?
    Voting Booth?