[ create a new paste ] login | about

Link: http://codepad.org/vEPnhWfH    [ raw code | output | fork ]

Perl, pasted on Sep 6:
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");


Output:
1
2
3
4
5
6
Trying with if-statement
$1 is 2011
2011
Trying with conditional expression
$1 is 2011
2011


Create a new paste based on this one


Comments: