[ create a new paste ] login | about

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

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


Output:
1
2
3
4
5
6
7
8
9
10
Trying with if-statement
$1 is 2011 and $year is 2011
Trying with conditional expression
$1 is 2011 and $year is 2011
Trying with if-statement
INVALID
$1 is  and $year is zzzzzzz
Trying with conditional expression
INVALID
$1 is  and $year is zzzzzzz


Create a new paste based on this one


Comments: