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