stephanm has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

maybe someone can help me understanding why "die" does not die when it's called within a file that in turn is called with a do statement.

I've created 2 files, calling.pl and called.pl as per below. The output of perl -w calling.pl is given underneath. It seems the "die" statement in called.pl does not get executed.

Can anybody help?

Thanks.

calling.pl:

do 'called.pl':; print "printing from calling program\n"; die "dying from calling program, stopped";

called.pl:

print "printing from called program\n"; print STDERR "printing from called program to stderr\n"; die "dying from called program, stopped";

output:

printing from called program printing from called program to stderr printing from calling program dying from calling program, stopped at calling.pl line 3.

Replies are listed 'Best First'.
Re: Using "die" with "do-file"
by Your Mother (Archbishop) on Jul 06, 2008 at 06:55 UTC

    As you've posted it, you have a syntax error:

    syntax error at calling.pl line 1, near "'called.pl':" Execution of calling.pl aborted due to compilation errors.

    And perldoc -f do explains the rest. :)

    do EXPR Uses the value of EXPR as a filename and executes the c +ontents of the file as a Perl script. do 'stat.pl'; is just like eval `cat stat.pl`;

    More at perldoc -f eval.

      Got it! Thanks.

Re: Using "die" with "do-file"
by apl (Monsignor) on Jul 06, 2008 at 11:55 UTC
    You can't go wrong by having the following towards the top of your scripts:
    use strict; use warnings;