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

Hi Monks!
I have this simple module to log things in my code, I just can't understand why when I try to call the subroutine "data_log" from my module in my main script it gives this error:
Software error: Undefined subroutine &main::data_log called at my_script.pl
As far as I know I am exporting it and declaring it as you can see:
main.pl
#!/usr/bin/perl use strict; use warnings; use CGI qw(-oldstyle_urls :standard); use Time::Zone; use Date::Format; use Date::Parse; use CGI::Carp qw(fatalsToBrowser); use CGI qw(escapeHTML); use File::Basename; use Data::Dumper; use Digest::MD5 'md5_hex'; use Date::Calc qw( Today Today_and_Now Date_to_Days Add_Delta_Days ); use CGI::Session ( '-ip_match' ); use POSIX; use JSON; use HTML::Template; use lib '../MyLib'; # Add private library to path use Logger qw(data_log); # Load source at runtime my $q = new CGI; $| = 1; BEGIN { # Set log file my $log = 'log.log'; use CGI::Carp qw(carpout); # Send all warnings to the log_file open STDERR, '>>', $log; } # Load Main Template my $main_templ = HTML::Template->new(filename => '../../temp/main.tmpl +'); $ENV{'TZ'} = 'America/New_York'; #Logging to a file data_log('logger_error.txt', $ENV{'TZ'}); ...

Here is the module:

#!/usr/bin/perl # # Logg.pm # # package MyLib::Logger; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw( data_log ); # Exported, if explicitly requested our $VERSION = '0.01'; sub data_log { my ($log_file, @data) = @_; my $log_fh; # Log data to file my $timestamp = '[' . localtime() . '] '; if ($log_file && ($log_file !~ /^STDOUT$/i)) { # Allow tee-ing of logfile to STDOUT and a file ('|tee -a log_file +.txt') if ($log_file !~ /^\|/) { $log_file = ">>${log_file}"; } open $log_fh, $log_file or warn "Can't open $log_file: $!"; print $log_fh $timestamp, @data, "\n"; close $log_fh; } else { print $timestamp, @data, "\n"; } return; } 1; # End of Logg.pm

Thanks for looking!

Replies are listed 'Best First'.
Re: Export not exporting from module
by chromatic (Archbishop) on Feb 21, 2014 at 18:35 UTC

    You refer to the module with three different names, Logger, Logg, and MyLib::Logger. If you're not getting an error about a failed use of Logger, then Perl's loading it from a file named MyLib/Logger.pm, but MyLib/ is in @INC.

    Because that's how use loads it, Perl will try to call Logger->import( 'data_log' );. There's no failure if this import fails to exist in the package. Your package is MyLib::Logger, which does have an import but which doesn't get called, because its name doesn't match the name by which Perl loaded the file.

    Short answer: change package MyLib::Logger; to package Logger;. (Also change the comments for the sake of other people reading the code.)


    Improve your skills with Modern Perl: the free book.

Re: Export not exporting from module
by toolic (Bishop) on Feb 21, 2014 at 18:32 UTC
    use lib '..'; # Add private library to path use MyLib::Logger qw(data_log); # Load source at runtime