Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't call method "try" without a package or object reference

Tags:

try-catch

perl

I have a try catch block in perl

try {
    //statement 1
    //statement 2
};
catch Error with
{
    print "Error\n";
}

When I run the perl program I get the following error

Can't Call method "try" without a package or object reference at...

like image 947
Alwin Doss Avatar asked Dec 20 '22 19:12

Alwin Doss


1 Answers

Perl does not provide try or catch keywords. To trap "exceptions" thrown by die, you can set a $SIG{__DIE__} handler or use eval. Block form is preferred over string form, as parsing happens once at compile time.

eval {
    // statement 1
    // statement 2
}
if ($@) {
    warn "caught error: $@";
}

There are various modules that provide more traditional try-like functionality, such as Try::Tiny.

like image 181
ephemient Avatar answered Dec 28 '22 23:12

ephemient