in reply to exporter and multiple namespaces

Yes, import should run each time the module gets 'use'-d by a script/module/program/etc. 'use' is essentially a BEGIN block that first requires the other module, then runs its import class method. The file won't get re-loaded (ie. required) each time you use it, but the import method will get called each time.

You can test this by writing in your own import method and putting some debugging statements inside of it, eg. using caller to see who's calling import.

But, in a sort of unrelated to your question thing, you say that you're going to be using these functions in almost every one of your scripts and modules. In that case, might I suggest thinking about an OO approach? It may not fit your model, in which case go with the exported functions; but if it does fit, you'll be saving on a lot of exported symbol table entries, if you're really going to export functions to each of your namespaces. Just a thought, anyway; I'm not saying "go OO", just to think about it if you haven't already.

Replies are listed 'Best First'.
Re: Re: exporter and multiple namespaces
by AidanLee (Chaplain) on May 01, 2001 at 23:17 UTC

    I am quite happy to use OO much of the time, however at this point I was trying to be minimally syntactically invasive. By this i mean I'd like to be able for other developers using my library to be able to go from this:

    open(FILE,"foo.txt") or die "couldn't open file";

    to this:

    use csLogs::Error; open(FILE,"foo.txt") or csDie(...);

    The use of the error module is supposed to be as far away from the spotlight as possible, so I am looking to keep things as transparent to the programmers as possible

      That sounds fine; particularly after seeing your intended interface I withdraw all thoughts about using an OO approach. :) One shouldn't have to instantiate an object just to die.

      Anyway, though, would you mind posting some of your code? Is it possible that you have your functions in @EXPORT_OK and that you're not importing them explicitly? Just an idea.

        I'll see what I can do about extracting what I think are the relevant bits :)

        here is the gist of

        package Error; use Exporter (); @ISA = qw(Exporter); @EXPORT = qw( csWarn csDie ); use lib 'f:/projects/site/bin/config/'; use strict; use utf8; use csLogsConfig; #================================================== # Replacements for User-called warn and die #-------------------------------------------------- # to be used instead of warn() in application code sub csWarn { takes error class/severity and diagnostics info as parameters . . . } #-------------------------------------------------- # to be used instead of die() in application code sub csDie { very similar to csWarn, except that it exit()'s on completion . . . } #================================================== # Private Functions . . . #================================================== #Overload SIGWARN and SIGDIE BEGIN { sub csWarnHandle { much like csWarn except with default parameters } sub csDieHandle { much like csDie except with default parameters } $SIG{__WARN__} = \&csWarnHandle; $SIG{__DIE__} = \&csDieHandle; } #---- 1;

        here is pieces of csDB, which is a wrapper for the DBI

        package csDB; use Exporter (); @ISA = qw(Exporter); @EXPORT = qw(QueryDB $dbh); use lib 'f:/projects/site/bin/config/'; use strict; use DBI; use csConfig; use csLogs::Error; our $dbh; #===================================================================== +===================================== # Database Connection BEGIN { $dbh = DBI->connect(csDBAuth()) or csDie( class=>csErrorClass('SERVER_DATABASE'),severity=>csErrorSca +le('CRITICAL'), message=>'Could not connect to database server', debug=>$DBI::errstr ); } END { $dbh->disconnect(); } #===================================================================== +===================================== sub QueryDB { $dbh->{RaiseError}=0; $dbh->{PrintError}=0; my $qstring = $_[0]; my $dbquery= $dbh->prepare("$qstring") or csDie( class=>csErrorClass('CODE_DATABASE'),severity=>csErrorScale +('SEVERE'), message=>'Database Query Preparation Error', debug=>$DBI::errstr ); $dbquery->execute or csDie( class=>csErrorClass('CODE_DATABASE'),severity=>csErrorScale +('SEVERE'), message=>'Database Query Execution Error', debug=>$DBI::errstr ); $dbh->{RaiseError}=1; $dbh->{PrintError}=1; return ($dbquery); } #===================================================================== +===================================== other handy functions . . .

        and finally the script that i was testing upon discovering this issue, csErrorLog_Parser

        use lib 'f:/projects/site/bin/config/'; use strict; use utf8; use csConfig; use csLogsConfig; use csLogs::Error; use csLogs::Event; use csDB; if (-f &csErrorLog) # file exists and needs to be processed { my $tmp_logfile = csPath('logs').'/errors'.time().'.log'; my $LOG = undef; #--------------------------------- # renaming the file assures that the processing # this script does doesn't interfere with normal logging. rename(&csErrorLog,$tmp_logfile) or csDie( class=>csErrorClass('CO +DE_FILE_IO'),severity=>csErrorScale('MODERATE'), message=>'Error Log Parser + could not rename log to a temporary file.', debug=>"original filename: + '".&csErrorLog."'\nnew filename: '$tmp_logfile'" ); open($LOG,$tmp_logfile) or csDie( class=>csErrorClass('CODE_FILE_I +O'),severity=>csErrorScale('MODERATE'), message=>'Error Log Parser + could not open the renamed log file.', debug=>"filename: '$tmp_lo +gfile'" ); #--------------------------------- # process the entries my $entries = process_errorlog($LOG); Event::NoteEvent( class=>csEventClass('TOOL_ACTION'),importance=>c +sEventScale('LOW'), message=>'Error Log Parser successfully processed the e +rror log', info=>"$entries log entries processed from '".&csErrorL +og."'" ); #--------------------------------- # with processing completed dispose of the temp log file. close($LOG); unlink($tmp_logfile) or csDie( class=>csErrorClass('CODE_FILE_IO') +,severity=>csErrorScale('MODERATE'), message=>'Error Log Parser + could remove the renamed log file.', debug=>"filename: '$tmp_lo +gfile'" ); } else # file doesn't exist and no processing was necessary { Event::NoteEvent( class=>csEventClass('TOOL_ACTION'),importance=>c +sEventScale('LOW'), message=>"Error Log Parser ran but '".&csErrorLog."' wa +s not found", info=>"parser assumes no errors have been logged since +the parser was last run." ); } #exit #===================================================================== +=========================== sub process_errorlog { my $LOG = $_[0]; my $entries = 0; # track num of entries processed. my $state = 'none'; #state of the parser; my ( $time, $class, $severity, $message, $debug ) = (); #--------------------------------- # walk the file while( <$LOG> ) { with regex's and such.... } if( $time ) { ....temporary inelegance to flush out the last record.... } return $entries; }