Cannot call the try method without specifying a package or object

I have a catch try 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:

Cannot call the try method without reference to the package or object ...

+4
source share
2 answers

Perl does not provide try or catch keywords. To catch the โ€œexceptionsโ€ thrown by die , you can set the $SIG{__DIE__} or use eval . The shape of the block is preferable to the shape of the string, since parsing occurs 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 .

+4
source

You probably need one of the CPAN modules, for example Try::Tiny :

 use Try::Tiny; try { # statement 1 # statement 2 } catch { print "Error\n"; }; 
+4
source

All Articles