Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

@EXPORT advice needed 1

Status
Not open for further replies.

Xaqte

IS-IT--Management
Oct 4, 2002
971
US
I've got a package that incorporates validation.

I made it an easy switch, so you can turn it on and off via:
Code:
use FieldMap validate => 1;

This is done via the following:
Code:
my $VALIDATE = 0;
my $VALIDATE_MISSING = 1;

sub import {
    my( $pkg, %params ) = @_;
    return unless %params;
    if ( exists $params{validate} ) {
        $VALIDATE = $params{validate};
    }
    if ( exists $params{validate_missing} ) {
        $VALIDATE_MISSING = $params{validate_missing};
    }
}


Now, thats all working good...

However I need to export a sub, so they don't have to call FieldMap::Method().

Now I understand I have to use the Exporter and populate @EXPORT, but I feel like my import routine is inhibiting this.

Thoughts? Suggestions?
 
do u mean ur trying to export a method from the package by default, something like @EXPORT_OK = (method)? The following will help immensly.

-- Look at the note updates on Re: Simple Module Tutorial
by johnnywang (Priest) on Aug 09, 2004 at 22:58 UTC
 
I don't see how the code you posted could interfer with exporting a function/method. Maybe posting more of your code will help clarify.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Maybe I'm misunderstanding things... or I wasn't more specific in my question.

Currently, I have a function in my class that has to be called like so:

Code:
FieldMap::STATIC();

I would like to use:
Code:
STATIC();

I understand the normal way of doing this is like this:
Code:
use FieldMap qw(STATIC);

Is there a way to use this STATIC function without declaring it in the use statement?
 
Yes. In the module, @EXPORT will export anything you want it to by default without having to explictly import it from the calling script. @EXPORT_OK is where you list othe things that can be optionally imported by the calling script.



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
After my package declaration, strict and warnings... I have this:

Code:
use Exporter;
use vars qw(@ISA @EXPORT);
@ISA         = qw(Exporter);
@EXPORT      = qw(STATIC);

In the script using the package, I have:

Code:
use FieldMap;

STATIC('foo');

I get the following error:

Code:
Undefined subroutine &main::STATIC

Any ideas?
 
change:

use vars qw(@ISA @EXPORT);

change to:

our qw(@ISA @EXPORT);

See if that helps.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Thanks Kevin!

However, that gave me a syntax error...

So, I tried:
Code:
require Exporter;
our @ISA         = qw(Exporter);
our @EXPORT      = qw(STATIC);

and I still get the same error:
Code:
Undefined subroutine &main::STATIC
 
Sorry, this line:

our qw(@ISA @EXPORT);

should be:

our(@ISA, @EXPORT);

If you still get the error I'm stumped.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
You were right Kevin, however... I had to modify my import (from my first post):

Code:
sub import {
    my( $pkg, %params ) = @_;
    return unless %params;
    if ( exists $params{validate} ) {
        $VALIDATE = $params{validate};
    }
    if ( exists $params{validate_missing} ) {
        $VALIDATE_MISSING = $params{validate_missing};
    }
    $pkg->export_to_level(1);
}

One note of this though...

I don't know how export_to_level is working with just one param. If someone can explain this, it would be great!

 
I don't know what $pkg->export_to_level(1) does; will research that, but I did not see your package being closed by a tailing 1;, which would mean that perl can load your package successfully.
 
It is closed by a tailing 1;

I just don't have it shown here.
 
There is no way to tell you anything about export_to_level() without seeing the code.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top