Red Neckerson has asked for the wisdom of the Perl Monks concerning the following question:

Monks. If I write and employ a package, e.g., MYPACKAGE.PM, and within that package use other standard perl packages, like this:

package MYPACKAGE; use DBI; use CGI; [several sub routines follow] 1;

Do I "carry" modules DBI, CGI.PM, and their symbol tables, or others I name (their functions, routines, etc), into a script by merely declaring the MYPACKAGE.PM within it, as in:

#!/usr/bin/perl5 use MYPACKAGE.PM; print header;###from CGI.PM start_html(blah, blah, blah);###from CGI.PM MYPACKAGE::connect();###connecting to mySQL from a subroutine in MYPAC +KAGE.PM

Or do I have to re-state use DBI; use CGI.PM in the script? Red------

Replies are listed 'Best First'.
Re: Using a package within a package
by jepri (Parson) on May 24, 2001 at 19:24 UTC
    I just got an answer about scoping wrong the other day, so you may wish to take this with a pich of salt.

    The answer is "not like you are doing it". You would have to declare your CGi varibles inside your package (e.g. $query = new CGI;) and then pass them out of your package using EXPORTER. There is a comprehensive treatment of this here. I recommend copying the example whole and then building your module into that.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

Re: Using a package within a package
by blue_cowdawg (Monsignor) on May 24, 2001 at 19:25 UTC

    No the use is for the package itself and not the perl script invoking the package.

    Just for grins I tried what you are attempting just to see what would happen and when I did a print header with my package having declared use CGI qw/:all/; Perl thought that "header" was a file handle.

    That makes perfect sense to me....

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    Peter L. Berghold --- Peter@Berghold.Net
    "Those who fail to learn from history are condemned to repeat it."
    
Re: Using a package within a package
by Sifmole (Chaplain) on May 24, 2001 at 19:47 UTC
    As others have said, "not like you are trying to".

    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:

    [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;
    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.
    So you could do CGI::header() within your primary script.

    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.

Re: Using a package within a package
by $code or die (Deacon) on May 24, 2001 at 19:37 UTC
    If you use @ISA I think you can do this because your module inherits from the modules listed in @ISA. However, it isn't the best or tidiest way of doing this.

    The best thing to do is instatiate a DBI and CGI object in your module and make them accessible from the calling script - either by exporting them or making them part of the object.

    $code or die
    $ perldoc perldoc