P
Patrick Hartman
Hi, I am trying to use ? : statements instead of if/else blocks
wherever possible to keep my code shorter. Below is a conditional
statement that for some reason is not working as expected with the ? :
method:
#!/usr/bin/perl -w
use strict;
# image name
my $image = 'yf446_mk_nb_0_0_6_ia';
# break out seperate pieces
my($bare_style,$color,$brand,$slot1,$slot2,$shot,$type) = $image =~ m/
^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;
# this is not working as expected, returns the 'else' result
{
my $style;
$brand eq 'nb' ? $style = $bare_style . $color : $style =
$bare_style;
print "?: version: $style\n";
}
# this works as expected
{
my $style;
if ($brand eq 'nb') {
$style = $bare_style . $color;
}
else {
$style = $bare_style;
}
print "if/else block version: $style\n";
}
Any ideas why this would not work in the first statement? I appreciate
it,
Patrick
wherever possible to keep my code shorter. Below is a conditional
statement that for some reason is not working as expected with the ? :
method:
#!/usr/bin/perl -w
use strict;
# image name
my $image = 'yf446_mk_nb_0_0_6_ia';
# break out seperate pieces
my($bare_style,$color,$brand,$slot1,$slot2,$shot,$type) = $image =~ m/
^(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)_(\w+)$/;
# this is not working as expected, returns the 'else' result
{
my $style;
$brand eq 'nb' ? $style = $bare_style . $color : $style =
$bare_style;
print "?: version: $style\n";
}
# this works as expected
{
my $style;
if ($brand eq 'nb') {
$style = $bare_style . $color;
}
else {
$style = $bare_style;
}
print "if/else block version: $style\n";
}
Any ideas why this would not work in the first statement? I appreciate
it,
Patrick