in reply to Best practices - if any?

"I am not writing a new module or class. Just putting couple of big subs to a safe place"
If these subs are part of the same package then this is possible to achieve and then you can use the spread package by 'require'ing the filenames.pl which contain that package (File boundaries are not considered to be package boundaries in Perl). However, I wouldn't rule this out as your path to follow because I am not sure of how your code looks like..

Following is an example of the same package's subroutines being spread over a couple of files and then accessed and checked for which __package__ they belong.

#FileOne.pl package ProgramSpread; BEGIN{} sub subroutine1{ print "Hello from the sub 1 in ", __PACKAGE__, "\n"; } return 1; END{}
#FileTwo.pl package ProgramSpread; #The same package above BEGIN{} sub subroutine2{ print "Hello from the sub 2 in ",__PACKAGE__,"\n"; } return 1; END{}
#using the package require "FileOne.pl"; #File names containing the package require "FileTwo.pl"; ProgramSpread::subroutine1(); ProgramSpread::subroutine2();
UPDATE: Though this is possible, it is still not recommended for proper design.


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

Replies are listed 'Best First'.
Re^2: Best practices - if any?
by Anonymous Monk on Feb 21, 2010 at 13:47 UTC

    Why BEGIN{} ... END{} ??

      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.
        "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...