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!

In reply to Export not exporting from module by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.