Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Executing a subroutine inside a die

Status
Not open for further replies.
Jun 19, 2001
86
US
I need the ability to execute a subroutine inside of a die subroutine. The die would be called if a file handle couldn't be opened. Should I just do a:

open(FILE, "file.txt") or &mySub;

or

open(FILE, "file.txt") or die("it died");

Thanks in advance.

Nathan
 
You can actually do a combination of the two:
Code:
sub open_error {

    my $file = shift;
    print "Failed to open '$file'. Reason: $!\n";
    exit;
}

my $infile = 'file.txt';
open(FILE, $infile) or die &open_error($infile);
If you pass a subroutine to die it will execute the subroutine before dying. If the subroutine doesn't explicitly exit the program then die() will do it for you (you can test this by removing the 'exit;' from the subroutine).

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top