in reply to Importing constants into another module

The code we're interested in is actually in Audit::Config. Specifically, look at Exporter - you use Exporter in your exporting module (makes sense), set up an @EXPORT array (I use "our @EXPORT = ...", but that's because I never use an older version of perl where our doesn't exist), and Bob's yer uncle. All other modules that "use Audit::Config;" will automatically get the exported constants.

I doubt it's making your module OO that made this stop working - it's probably the use strict ;-)

  • Comment on Re: Importing constants into another module

Replies are listed 'Best First'.
Re^2: Importing constants into another module
by paulski (Beadle) on Feb 03, 2005 at 01:35 UTC
    I thought it was something to do with Exporter before I posted but my attempts to export the constants didn't work. Here's what I've tried:

    Audit::Config.pm:

    package Config; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(HOSTS_FILE HOST_BIN); use constant HOSTS_FILE => '/home/csssec/audit/css_hosts.txt'; use constant HOST_BIN => '/usr/bin/host'; ... 1;
    Check::DNSCheck.pm
    package DNSCheck; use Audit::Config; use strict; sub new { my $class = shift; my $host = shift; my $this = { host => $host, }; bless ($this, $class); } sub run { my $this = shift; my $cmd = HOST_BIN . " $this->{'host'} 2>/dev/null"; my $r = `$cmd`; if ($r !~ /has address/) { return(0); } return(1); } 1;
    I still get the same error message:
    Bareword "HOST_BIN" not allowed while "strict subs" in use at Check/DN +SCheck.pm line 24. Compilation failed in require at ./audit.pl line 6. BEGIN failed--compilation aborted at ./audit.pl line 6.
    I'm sure it's something to do with exporting the constants but as far as I can see, I'm doing it all according to the man page.

    Any ideas?

      You need to use the right namespace in your module. In Audit::Config, make the following change:
      # package Config; package Audit::Config;
        Hehe...I'd figured this out just before you posted. Although I was going mainly off intuition.

        Why was this causing the problem.?

        Is is because Audit::Config.pm clashes with another PERL module called Config.pm in @INC?

        Another weird thing...if I set the package for DNSCheck.pm as:

        package Check::DNSCheck.pm

        my script complains that it can't find the new{} constructor.

        Can't locate object method "new" via package "DNSCheck" (perhaps you forgot to load "DNSCheck"?) at ./audit.pl line 32, <FILE> line 1.

        I'd be interested to know why this is?

        Thanks