Gossamer Forum
Home : General : Perl Programming :

my c = (cond) ? a : b; question

Quote Reply
my c = (cond) ? a : b; question
What's wrong with the following expression (which doesn't work):
Code:
my $p = (defined $IN->param('p')) ? $IN->param('p') : $a->{p};
In my eyes this is completely equivalent to
Code:
my $p;
if (defined $IN->param('p')) {
$p = $IN->param('p');
}
else {
$a->{p};
}
which works...

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] my c = (cond) ? a : b; question In reply to
Yeah it should work fine. What happens?

It should even work without the paretheses.
Quote Reply
Re: [Paul] my c = (cond) ? a : b; question In reply to
$p always take the second value in the first version.... when I use the second version, it also takes the first value when it should.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] my c = (cond) ? a : b; question In reply to
It works ok for me.

Code:
#!/perl/bin/perl

use CGI qw/:standard/;

my $foo = { p => 'foo' };
my $p = defined param('p') ? param('p') : $foo->{p};

print header;
print $p;

That prints foo when you don't enter a p parameter and prints bar if you do script.cgi?p=bar

Last edited by:

Paul: Apr 24, 2002, 4:29 AM
Quote Reply
Re: [Paul] my c = (cond) ? a : b; question In reply to
Thanks Paul. Yes, it worked also for me.

The mistake I made was the following: p is the name of a checkbox field on a form. I thought that if the checkbox is unchecked param('p') will still be defined (and zero), but it isn't.

Ivan
-----
Iyengar Yoga Resources / GT Plugins