Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

use constant and exporter

by Flame (Deacon)
on Jan 28, 2003 at 03:18 UTC ( [id://230471]=perlquestion: print w/replies, xml ) Need Help??

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

According to the constant pragma page, constants declared using the constant pragma, can be exported. The question is, how?

I have tried to export them as below, but for some reason it doesn't seem to work.

package MyPackage; use Data::Dumper; use 5.006; use Tests; use constant { VALID => 1, INVALID => 2, ERROR => 0, }; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw(VALID INVALID ERROR) ], 'codes' => [ qw(VALID INVALID ERROR)], ); our @EXPORT_OK = qw(@{ $EXPORT_TAGS{'all'} }); our @EXPORT = qw(); #Intentionally left blank our $VERSION = v0.0.1;

When I use MyPackage qw(:all); and then attempt to use the VALID, INVALID or ERROR constants, I get error messages saying bareword "VALID" not allowed while "strict subs". Am I doing something wrong here?

Thanks





My code doesn't have bugs, it just develops random features.

Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Replies are listed 'Best First'.
Re: use constant and exporter
by artist (Parson) on Jan 28, 2003 at 04:10 UTC
Re: use constant and exporter
by batkins (Chaplain) on Jan 28, 2003 at 03:36 UTC
    try replacing the curly braces around the constants with parentheses.
    use constant ( VALID => 1, INVALID => 2, ERROR => 0, );
      According to the docs, for multiple declarations you use a hashref.. Their example, copy-pasted:
      use constant { SEC => 0, MIN => 1, HOUR => 2, MDAY => 3, MON => 4, YEAR => 5, WDAY => 6, YDAY => 7, ISDST => 8, };

      So I don't think that's it. I'll try your suggestion anyway though.

      Edit: Just tried it, none of the constants worked, even those that did previously. (They work inside the module they were declared in, but won't export.)



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

Re: use constant and exporter
by tall_man (Parson) on Jan 28, 2003 at 04:30 UTC
    Your EXPORT_OK statement is incorrect. It should look like this:
    our @EXPORT_OK = @{ $EXPORT_TAGS{'all'} };
    The extra "qw" around it messes it up. I tried your example after making that correction (in perl 5.6.1, also defining each one on one line) and the constants imported correctly.
      Ok, the code now reads:
      package MyPackage; use Data::Dumper; use 5.006; #use Data::Validate::OO::Tests; use Tests; #use constant { # VALID => 1, # INVALID => 2, # ERROR => 0, # }; use constant VALID => 1; use constant INVALID => 2; use constant ERROR => 0; use strict; use warnings; require Exporter; use AutoLoader qw(AUTOLOAD); our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => ['VALID','INVALID','ERROR'], 'codes' => ['VALID','INVALID','ERROR'], ); our @EXPORT_OK = @{ $EXPORT_TAGS{'all'} }; our @EXPORT = qw(); #Intentionally left blank our $VERSION = v0.0.1;
      And I'm testing it with this:
      use MyPackage qw(:all); use Data::Dumper; use strict; use warnings; print STDOUT VALID;


      It still yields the same error...



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

        I took your example exactly Flame, except that I commented out the "use Tests" (I don't have that module) and the "use Autoloader" (it causes errors if the package isn't installed, and I was testing it from the current directory). The example worked with no problem.

        (It also works with MyPackage::VALID, but I wouldn't call that an import.)

        I'm wondering if perhaps you aren't testing the package you think you are. Is the right copy of MyPackage.pm in your @INC path first? Did you reinstall the package after making the changes?

        use MyPackage qw(:all); use Data::Dumper; use strict; use warnings; print STDOUT MyPackage::VALID;

        Untested.

        John J Reiser
        newrisedesigns.com

Re: use constant and exporter
by Aristotle (Chancellor) on Jan 28, 2003 at 11:58 UTC
    It should work like this. I know I've exported constants before, anyway.
    package MyPackage; use 5.006; use strict; use warnings; use Data::Dumper; use constant VALID => 1; use constant INVALID => 2; use constant ERROR => 0; use base qw(Exporter); our @EXPORT_OK = qw(VALID INVALID ERROR); our %EXPORT_TAGS = ( 'all' => \@EXPORT_OK, 'codes' => [ qw(VALID INVALID ERROR)], ); our @EXPORT; #Intentionally left blank our $VERSION = v0.0.1;

    Makeshifts last the longest.

      Hmm, that didn't seem to work at first... but I think I solved my problem. I wasn't really using package MyPackage... the package was Data::Validate::OO and I was using "use OO" because I was testing from within the same dir as Data::Validate::OO was being written in... so... it looks like it was failing to call export properly... I'm now using 'use lib' to use the full package name and it works again.



      My code doesn't have bugs, it just develops random features.

      Flame ~ Lead Programmer: GMS (DOWN) | GMS (DOWN)

        Well, that explains it. use Foo does this:
        BEGIN { require Foo; Foo->import; }
        If you use OO, require will find your module, but of course the module will still create the Data::Validator::OO package and not an OO package, causing the following OO->import to fail (silently).

        Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://230471]
Approved by chromatic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 18:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found