CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Subs not exporting in module

 



swechsler
Novice

May 5, 2011, 6:28 AM

Post #1 of 8 (1134 views)
Subs not exporting in module Can't Post

All my Perl scripting up to this point (and it's been many years of it) has used "require" files rather than .pm modules. I'm trying to get my first module to work, and the subs don't seem to export properly; I can only access them if I specify module::subname. Here are the first few lines of the module:


Code
package Resources; 

use strict;
use warnings;

require "dba_subs.pl";
require Exporter;

my $resource_instance = "live-nydba-01";
my $resource_db = "scheduler";
my $resource_user = "dba_query";

my $dbh;

our $VERSION = "1.0";
our @ISA = qw(Exporter);
our @EXPORT = qw(acquire_resource free_resource check_resource);

sub acquire_resource($$$$$;) {



Your assistance is appreciated.

Thanks!


shawnhcorey
Enthusiast


May 5, 2011, 6:44 AM

Post #2 of 8 (1131 views)
Re: [swechsler] Subs not exporting in module [In reply to] Can't Post

Replace:

Code
require Exporter;

with

Code
# -------------------------------------- 
# Exports
use base qw( Exporter );
our @EXPORT = qw( );
our @EXPORT_OK = qw( );
our %EXPORT_TAGS = (
all => [ @EXPORT, @EXPORT_OK ],
);


Place the names of the subs you want automatically exported in @EXPORT. Place the names of the subs that can be optionally exported in @EXPORT_OK.

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


swechsler
Novice

May 5, 2011, 7:27 AM

Post #3 of 8 (1129 views)
Re: [shawnhcorey] Subs not exporting in module [In reply to] Can't Post

I tried changing "require Exporter" to "use base qw(Exporter)" with no change in behavior. If you look at my original code, I already had @EXPORT defined with the sub names.


shawnhcorey
Enthusiast


May 5, 2011, 7:58 AM

Post #4 of 8 (1127 views)
Re: [swechsler] Subs not exporting in module [In reply to] Can't Post

If you're exporting variables, you have to include their sigils too:

Code
our @EXPORT = qw($acquire_resource $free_resource $check_resource);


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


swechsler
Novice

May 5, 2011, 8:10 AM

Post #5 of 8 (1126 views)
Re: [shawnhcorey] Subs not exporting in module [In reply to] Can't Post

Nope, not exporting variables, just subs.


FishMonger
Veteran / Moderator

May 5, 2011, 8:22 AM

Post #6 of 8 (1125 views)
Re: [swechsler] Subs not exporting in module [In reply to] Can't Post

Try changing the variable declarations to be global instead of lexical vars i.e., use 'our' instead of 'my'.


miller
User

May 5, 2011, 10:55 AM

Post #7 of 8 (1119 views)
Re: [FishMonger] Subs not exporting in module [In reply to] Can't Post

Fishmonger has this one right. Use 'our' to declare your package varibles, not 'my'.


Code
package Resources; 

require "dba_subs.pl";
use Exporter;
our @EXPORT = qw(acquire_resource free_resource check_resource);

our $VERSION = "1.0";

use strict;
use warnings;

our $resource_instance = "live-nydba-01";
our $resource_db = "scheduler";
our $resource_user = "dba_query";

our $dbh;

sub acquire_resource($$$$$;) {


Also, I would strongly discourage you from using a sub with 5 parameters like that. Anytime you have more than 2 parameters, you should use a hash instead.


Code
acquire_resource( 
param1 => 'val1',
param2 => 'val2',
param3 => 'val3',
param4 => 'val4',
param5 => 'val5',
);


Then you can use something like Params::Validate to validate the parameters.

http://search.cpan.org/~drolsky/Params-Validate-0.98/lib/Params/Validate.pm

- Miller


FishMonger
Veteran / Moderator

May 5, 2011, 11:20 AM

Post #8 of 8 (1116 views)
Re: [FishMonger] Subs not exporting in module [In reply to] Can't Post

Here's some info on why you almost never want to use prototypes.
http://www.perlmonks.org/?node_id=861966

As to exporting the vars, my recommendation is to make them lexical vars and provide an accessor sub (or subs) that return the vars.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives