sub invalid {
print("INVALID\n");
}
sub check_with_if {
print("Trying with if-statement\n");
my $year = shift;
if ($year =~ /^(\d{4})$/) {
$year = $1;
} else {
&invalid($year);
}
print("\$1 is $1 and \$year is $year"."\n");
}
sub check_with_conditional {
print("Trying with conditional expression\n");
my $year = shift;
($year =~ /^(\d{4})$/) ? $year = $1 : &invalid($year);
print("\$1 is $1 and \$year is $year"."\n");
}
check_with_if("2011");
check_with_conditional("2011");
check_with_if("zzzzzzz");
check_with_conditional("zzzzzzz");