in reply to Re^2: Best practices - if any?
in thread Best practices - if any?
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{}.
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..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();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Best practices - if any?
by Anonymous Monk on Feb 21, 2010 at 14:42 UTC | |
by Anonymous Monk on Feb 21, 2010 at 14:51 UTC | |
by JavaFan (Canon) on Feb 21, 2010 at 19:15 UTC |