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

Hello again prestigious monks. I am stuck and I hope you can help. I have a simple perl module that I use to write logging info into a text file. I call this module from scripts that run as daemons along the way to log useful tidbits of info. I am stuck on this error though. Its like my subroutine in the main script doesn't see the sub exported by loading the module. As soon as the sub routing dbConnect tries to run the dbug() it bombs. Any insight is appreciated.
# ./path.pl Undefined subroutine &dbug::dbug called at ./path.pl line 56.
Here is the main script:
#!/usr/bin/perl use lib qw(../lib/); use warnings; use strict; use Proc::Daemon; use Proc::PID::File; use File::Copy; use DBI; use XML::Simple; use POSIX; use dbug qw(dbug); my $path = "/opt/homeforscript"; my $hopper = "$path/hopper"; my $deliveryDir = "$path/hopper2"; my $log = "/var/log/script.log"; my $timestamp = POSIX::strftime("%m/%d/%Y %H:%M:%S", localtime); my $dbh; defined(my $pid = fork) or die "Cannot fork: $!\n"; exit if $pid; $dbh = dbConnect() or exit -1; dbug("Fork", "[$$ DB] Connected\n"); open (LOG, '>>', $log) or die("Cannot open Log"); print LOG "Log Opened\n"; for (;;) { #Open the hopper opendir (HOPPER, $hopper) or die $!; # And look for Files checkHopper(); sleep 1; } ############################ # Sub's # ############################ sub dbConnect { #print LOG "$timestamp - [Trying to Connect to DB]\n"; dbug("DB", "DBI CONNECTED"); print LOG "$timestamp - [DBI CONNECTED]\n"; eval { $dbh = DBI->connect('dbi:mysql:DBNAM','DBUSER','DBPASS +WD')or print LOG "Connection Error: $DBI::errstr\n"; }; return $dbh; } sub checkHopper { #Read a list of files in hopper if (is_folder_empty($hopper)) { } else { while (defined(my $file = readdir(HOPPER))) { #open (LOG, '>>', $log) or die("Cannot open Log"); if ($file !~ /^\./) { dbug("INCOMMING", "Found file named $file in i +ncomming hopper\n"); #print LOG "$timestamp - Found file named $fi +le in incomming hopper\n"; move("$hopper/$file", "$deliveryDir/$file") or + print LOG "cannot move $hopper/$file to $deliveryDir/$file\n"; dbug("PROCESSING", "Moved $file from hopper fo +r processing \n"); #print LOG "$timestamp - Moved $file from hopp +er for processing \n"; my $sql = "select * from transfers"; my $sth = $dbh->prepare($sql); $sth->execute or die dbug("DB", "SQL Error: $D +BI::errstr\n"); dbug("DB", "Queried the Database\n"); if (my @row = $sth->fetchrow_array) { dbug("QUERY", "@row\n"); } } } } } sub is_folder_empty { my $dirname = shift; opendir(my $dh, $dirname) or die "Not a directory"; return scalar(grep { $_ ne "." && $_ ne ".." } readdir($dh)) == 0; }
And here is the module:
#!/usr/bin/perl # $Id: dbug.pm $ package dbug; require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw(dbug); use strict; use warnings; use POSIX; my $timestamp = POSIX::strftime("%m/%d/%Y %H:%M:%S", localtime); sub pdebug { my $logDate = POSIX::strftime('%y%m%d', localtime(time)); my $component = shift; my $string = shift; my $logto = "/var/log/logfile.log.$logDate"; open (LOGGER, '>>', $logto) or die("Cannot open Log"); print LOGGER "$timestamp - [$component] $string"; close(LOGGER); }

Replies are listed 'Best First'.
Re: Subroutine not seeing export from module
by roboticus (Chancellor) on Feb 08, 2014 at 15:37 UTC

    perlguyjoe:

    It looks like your subroutine is named pdebug, not dbug. So perl isn't seeing that subroutine. If you change the name of the subroutine to dbug, then it looks like it could work.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      DOH! I knew it was something simple! .....Sometimes it just takes another set of eyes...I now humbly with tail between legs go back to my code. :) Thanks to you all.
Re: Subroutine not seeing export from module
by toolic (Bishop) on Feb 08, 2014 at 15:49 UTC
    As a side note, since you seem to always call POSIX::strftime, you can avoid cluttering your namespace by not importing anything else from POSIX:
    use POSIX qw();
Re: Subroutine not seeing export from module
by BrowserUk (Patriarch) on Feb 08, 2014 at 15:38 UTC

    You are exporting (and importing) the symbol dbug, but your module only contains a routine called pdebug().


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Subroutine not seeing export from module
by McA (Priest) on Feb 08, 2014 at 15:38 UTC

    Hi,

    In package dbug I can see only the definition of a subroutine pdebug and no subroutine dbug you want to export.

    Best regards
    McA