tags:

views:

108

answers:

2

I would like to get solution for a issue on in perl program.

$parallel_on=='YES'? my $pid = $pm->start and next; :0;

I want being the statement like this. but I am not getting solved. Can please any one solve this?

+5  A: 

The ? : syntax is for assigning to variables, not for making one line if statements. Since you are using two statements in the true part of the condition, do us all a favor and use a real if statement. It's cleaner and you don't waste 5 minutes asking how to do it in SO.

my $pid = 0;

if ($parallel_on eq 'YES') {
    $pid = $pm->start;
    next;
}
wm_eddie
this is cleaner approach
ram
Could have been stated with `--$snarkiness`, though.
Adam Bellaire
+10  A: 
$parallel_on=='YES'? my $pid = $pm->start and next; :0;

There's a lot wrong there. First, there's an extra semicolon; get rid of it.

$parallel_on=='YES'? my $pid = $pm->start and next :0;

Next, = and and have lower precedence than ?:, so you'd need to enclose the true condition in parentheses:

$parallel_on=='YES'? (my $pid = $pm->start and next) :0;

At this point, it will compile, but still not work. == is for numeric comparisons, and 'YES', being a string not beginning with digits, has a numeric value of 0, so the condition is almost always going to be true. (For example, 'NO'=='YES' is comparing 0==0, which is true.) Use eq instead:

$parallel_on eq 'YES'? (my $pid = $pm->start and next) :0;

The next part, I'm not sure how to help you with, since I'm not sure what the rest of your code looks like. my $pid declares a new lexical, with scope to the end of the enclosing block, but as soon as you assign to it, you use next to exit (and perhaps reenter) the block, thus losing the value you stored in $pid. You probably want to declare $pid before the block.

my $pid;
...
    $parallel_on eq 'YES'? ( $pid = $pm->start and next) :0;

Now you have a 0; hanging out, serving no purpose, when the condition is false. Get rid of it:

    if ( $parallel_on eq 'YES' ) { $pid = $pm->start and next }

Unless you meant to assign it to $pid when the condition is false?

    $pid = $parallel_on eq 'YES' ? $pm->start : 0 and next;
ysth