in reply to Re^2: Best practices - if any?
in thread Best practices - if any?

That is just to show the package skeleton in general. It is not required in this case.

There are situations like when you want to initialize some variables in the start or do some cleanup/deallocation at the end, for such cases you might wanna use BEGIN{} and END{}.

package Constructor_Destructor; BEGIN{ our $text; $text = "Hello from BEGIN\n\n"; } sub subroutine{ print $text; } END{ print "DESTROYING...\n"; $text=0; print "Now \$text is $text\n"; print "Exiting with $?\n" } #return 1; #did not return since I am calling from the same package #Use the package: Constructor_Destructor::subroutine();
You can also use multiple BEGIN{} and END{} subroutines, the BEGIN{} ones would execute in the order encountered and the END{} ones would execute in the reverse order they were defined in order to match the BEGIN{} subroutines..


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

Replies are listed 'Best First'.
Re^4: Best practices - if any?
by Anonymous Monk on Feb 21, 2010 at 14:42 UTC
    "You can also use multiple BEGIN{} and END{} subroutines"

    It's exactly for that reason that I figured having emtpy ones doesn't make much sense...

      It may not make much sense if you are with good experience in packages and modules... But the OP might be relatively new to this and that there are many new monks out there that might see a package skeleton structure educational ergo for their sake my response has such been.
        Actually BEGIN and END are usually not needed. Their usage is uncommon enough that I would not show them to newcomers as part of a "typical module skeleton".