in reply to Re: exporter and multiple namespaces
in thread exporter and multiple namespaces

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

Replies are listed 'Best First'.
Re: Re: Re: exporter and multiple namespaces
by btrott (Parson) on May 01, 2001 at 23:20 UTC
    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 csLogs::Error.pm

      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> ) { ....fun with regex's and such.... } if( $time ) { ....temporary inelegance to flush out the last record.... } return $entries; }
        Your original error message was that the function csErrorClass couldn't be found in the csDB package. And that makes sense from the above code, because that function is not defined anywhere, nor is it exported from csLogs::Error. Could that be your problem?

        Do you define this function in csLogs::Error, and if so, why are you not exporting it?

        @EXPORT = qw( csWarn csDie csErrorClass );