in reply to Using a package within a package
I did however want to add a little bit of observation regarding this. Observe the following code, where the square-brackets indicate the name of the file the code is contained in:
If you set up these packages you will notice that they run, and will print the header and the printme routine. However pm.pl never directly includes either CGI or Bar. The package routines are available via the package name::routine invocation method.[pm.pl] #!/usr/bin/perl -w use strict; use Foo; print CGI::header(); Bar::printme(); [Foo.pm] package Foo; use CGI; use Bar; 1; [Bar.pm] package Bar; sub printme { print "PRINTME FROM BAR \n"; } 1;
However I personally would view this as a bad idea and sloppy coding. A script should never rely on another script to include a library it needs; Include the libraries you need. If you are worried about overhead of including a library multiple times, don't; Perl handles this and only uses/requires a library once, no matter how many times it is requested. There may be an exception or two to this rule, if anyone out there knows of them -- please speak up.
|
|---|