Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return nothing from a subroutine?

Tags:

perl

I want to validate a condition before doing the next step, but only raise a warning and skip the current value instead of die-ing.

How do I rewrite validate_me() subroutine without returning any value?

(Update) please note that the following code works as expected, it just that I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.

foreach my $file ( @files ) {
    validate_me($foo, $bar, $baz) or next;
    do_something();
}  

sub validate_me {
    my ($foo, $bar) = @_;

    if ( $foo > $bar ) {
        carp("Something goes awol");
        return 0;
    }

    if ( $bar != $baz ) {
        carp("Odd");
        return 0;
    }

    return 1; # success
}

Thanks.

like image 397
est Avatar asked Oct 22 '09 01:10

est


3 Answers

Check the documentation for return. If you want to return nothing, don't give return an argument:

 return;
like image 125
brian d foy Avatar answered Oct 31 '22 19:10

brian d foy


First, there's no reason to use a label in your loop, a next; by itself will automatically do the next iteration of the enclosing loop.

As to your question, I'm a little confused about what you're trying to accomplish. Your code as written will return zero, which is a false value, from validate_me if any of your tests fail, and that should cause the next iteration of the loop. There's nothing in validate_me that would cause anything to die.

like image 24
friedo Avatar answered Oct 31 '22 18:10

friedo


Update:

I want something else instead of returning 1 or 0 from validate_me() that still allow the code to behave the same.

Is this what you mean? See eval.

my @files = qw( a b c d e f );

FILES:
foreach my $file ( @files ) {
    eval { validate_me($file) };
    if (my $ex = $@) {
        warn "'$file' failed validation: $ex";
        next FILES;
    }
    print "$file passed\n";
}

sub validate_me {
    my ($file) = @_;
    die "Invalid filename\n" if 0.5 > rand;
    return; # explicitly return nothing
}

Now, the sane thing to do would be to refactor validate_me into its components:

for my $file ( @files ) {
    if ( is_something_gone_awol($file, $mile) ) {
        warn "Something has gone awol: '$file' and '$mile'\n";
    }
    elsif ( is_something_odd($file, $smile) ) {
        warn "Something is odd: '$file' and '$smile'\n";
    }
    else {
        do_something($file);
    }
}

sub is_something_gone_awol {
    my ($foo, $bar) = 0;
    return $foo gt $bar;
}

sub is_something_odd {
    my ($bar, $baz) = @_;
    return $bar ne $baz;
}
like image 23
Sinan Ünür Avatar answered Oct 31 '22 19:10

Sinan Ünür