Why doesn't Perl give error for try / catch when exiting Try :: Tiny

I moved some code and did not use "Use Try :: Tiny" in a piece of code. When I run it, perl runs both blocks of code, hence the catching lights that thankfully let me see the error. It seems like try and catch are being used as labels. I thought the labels needed a colon after them? Why didn't pearl catch it?

here's the code:

#!/grid/common/bin/perl

use strict;
use warnings 'all';

foo();

sub foo {

    try {
    print("hi\n");
    }
    catch {
    die "FATAL: this went wrong, <$@>";
    }

}

      

+3


source to share


1 answer


This is the indirect syntax of the object that bites you.

try {
  print("hi\n");
}
catch {
  die "FATAL: this went wrong, <$@>";
}

      

Receives interpretation as:



(do { print("hi\n") })->try(
   (do { die("FATAL: this went wrong, <$@>") })->catch
)

      

Yes indeed.

There is a module called indirect that can give you a compile warning when an indirect method is encountered.

+12


source







All Articles