4

I have a perl-script:

#!/usr/bin/perl 

BEGIN {
        print "That's BEGIN message\n";
BEGIN { print "That's BEGIN-2 message\n"; };
END { print "That's END message\n"; };
BEGIN { print "That's BEGIN-3 message\n"; };
};

It works so:

That's BEGIN-2 message
That's BEGIN-3 message
That's BEGIN message
That's END message

But why? It must prints in line 1 That's BEGIN message or no?

1 Answers1

10

The first BEGIN isn't run until it is completely defined (see documentation). That doesn't happen until the end of the last }.

The others get run as they're completely defined — earlier.

Thomas Dickey
  • 76,765