A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label die. Show all posts
Showing posts with label die. Show all posts

Do or die

This is wrong:

return something() or die "something went wrong";

This is right:

return something || die "something went wrong";

(source)

The former will generate a warning:

Possible precedence issue with control flow operator

Ctrl-C doesn't interrupt loop in Bash script

If you want Ctrl-C to be able to stop your loop in Bash, put this inside the loop:

trap "echo Exited!; exit;" SIGINT SIGTERM

(source)

When to use "or" and when to use || (double pipe) in Perl

Use || for assigning to a variable:

$a = $b || $c

Use "or" to control program flow:

$a = $b or die 'error: $b is unexpectedly false'

Mnemonic: || is a symbol like variables and numbers are, while "or" and "die" are both english words.

(source, source)

Rollback within a txn_do for DBIC


try {
    $self->schema->txn_do(sub {
        # something went bad
        die 'argh';
    });
}
catch ($e) {
    # log me
    # no roll back needed
}

Always get a stack trace when Perl dies

Always get a stack trace when Perl dies or warns:

use Carp::Always;
or

perl -MCarp::Always -e'die "argh"'