"Perl for Perl Newbies" - Part 4Exceptionsdie and eval

6.1. die and eval

The statement die throws an exception which can be any Perl scalar. The statement eval { ... } catches an excpetion that was given inside it, and after it sets the special variable $@ to be the value of the exception or undef if none was caught.

Here's an example:

#!/usr/bin/perl

use strict;
use warnings;


sub read_text
{
    my $filename = "../hello/there.txt" ;
    open I, "<$filename"
        or die "Could not open $filename";
    my $text = join("",<I>);
    close(I);

    return $text;
}

sub write_text
{
    my $text = shift;
    my $filename = "../there/hello.txt";
    open O, ">$filename"
        or die "Could not open $filename for writing";
    print O $text;
    close(O);
}

sub read_and_write
{
    my $text = read_text();

    write_text($text);
}

sub perform_transaction
{
    eval {
    read_and_write();
    };
    if ($@)
    {
        print "Could not perform the transaction. Reason is:\n$@\n";
    }
}

perform_transaction();

Written by Shlomi Fish