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

After reading arturo's Variable scoping in perl, and dominus's cooping with scooping, I am still left with a question. my gui.pl contains all my gtk2 code, my func.pm contains all the functions that do all the work, and a global hash that contains a bunch of configuration options. I would like to reference the functions and var contained in func.pm from gui.pl without the func:: infront of everything, if I don't declaire packge func; at the top of my func.pm file I don't see anyway of referencing a global var locaed there in, but as soon as I declair it a package, there by gaining access to a global var by way of $func::var, I need to start referencing all my subs there-in by func:: ..is there a way around all this?

Help!
thanks in advance,
daN.

Replies are listed 'Best First'.
Re: using package functions and vars withouth packagename::
by davido (Cardinal) on Apr 06, 2004 at 15:43 UTC
    This is an example of where you can use Exporter. The Exporter module allows you to export any functions and even package globals that you care to export from a module to its calling program.

    package YourModule; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(munge frobnicate); # symbols to export on request use ModuleName qw(frobnicate); # import listed symbols frobnicate ($left, $right) # calls YourModule::frobnicate

    Dave

      Thank you, I'm using export now and everything works perfectly. I had initially tried to use it, but my package had the use strict and I was using my to declaire @ISA and @EXPORT it wasn't working so I thought I was on the wrong track. I'm using our now and all is working perfectly.


      daN.
        You can still use strict while satisfying the need for @ISA and @EXPORT to be package globals in one of a couple ways. Here are some contrived examples:

        # Example 1... The best pre-Perl v5.6.0 way. package foobar; use strict; use warnings; use vars qw(@ISA @EXPORT); @ISA = qw(Exporter); @EXPORT = qw(foo bar); # Example 2... The best Perl v5.6.0 + way. package foobar; use strict; use warnings; our @ISA = qw(Exporter); our @EXPORT = qw(foo bar); # Example 3... The 'base' way. package foobar; use strict; use warnings; use base qw(Exporter); # But you still have to either 'use vars' or 'our' for the # export list @EXPORT. our @EXPORT = qw(foo bar);


        Dave

        You wouldn't have maded that mistake if you had created your module stub using h2xs ...

        [tomdlux@localhost /home/tomdlux]$ h2xs -X My::MultiLevel::ModulePath Defaulting to backwards compatibility with perl 5.8.0 If you intend this module to be compatible with earlier perl versions, + please specify a minimum perl version with the -b option. Writing My/MultiLevel/ModulePath/ModulePath.pm Writing My/MultiLevel/ModulePath/Makefile.PL Writing My/MultiLevel/ModulePath/README Writing My/MultiLevel/ModulePath/t/1.t Writing My/MultiLevel/ModulePath/Changes Writing My/MultiLevel/ModulePath/MANIFEST

        --
        TTTATCGGTCGTTATATAGATGTTTGCA

Re: using package functions and vars withouth packagename::
by bart (Canon) on Apr 06, 2004 at 15:50 UTC
    That's what import is basically for... The idea is to make aliases to your selection of package variables and subs, into the package where the module is used. That means that other variables (though, of the same name) come to life into the caller package, which reference the same values/subs as your original package variables/subs... Modify one, and the other follows, that's what aliasing means.

    import is supposed to be a sub, called as a class method, whenever your module of the same name as the package, is used. As it's a class method, you'll see two difference with a normal sub:

    1. You'll see the name of the package which invokes the method, as an extra first parameter
    2. Inheritance works.
    The latter is the basis of how Exporter works. Your module package inherits from Exporter, and as a result, the import method from Exporter is called. And that module's import method can handle the importing of your subs and variables, in a standard and convenient way — with very little work required on your part. All you have to do is set up some package variables: @EXPORT, @EXPORT_OK, and possibly a few more. See the docs for Exporter, if you're curious.
Re: using package functions and vars withouth packagename::
by borisz (Canon) on Apr 06, 2004 at 15:41 UTC
    Use Exporter read perldoc Exporter.
    Boris