Zagorax
Zagorax

Reputation: 11890

Function exported but the script search it in the wrong package

I have a perl package ServerSocket.pm written as following:

package Functions::ServerSocket;

use strict;
use warnings;
use ZeroMQ qw/:all/;
use Functions::Testd qw(get_interface_address);

# Next lines are needed to export subroutines
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw(start_socket receive_msg send_msg end_msg close_socket term_ctxt send_ip);

{....................}

# Send a message
sub send_msg {
    # Retrieve message
    my $msg = shift;

    # If $sender is not undef, sending the first part of output with socket identity
    if (defined($sender)){
        select_recipient();
    }

    ZeroMQ::Raw::zmq_send($socket, $msg, ZMQ_SNDMORE);
    chomp($msg);
    print "MSG: \"$msg\" sent.\n";
}

{................................}

And I use this package inside another one called Testd.pm: package Functions::Testd;

use strict;
use warnings;
use Functions::ServerSocket qw(send_msg close_socket term_ctxt end_msg);
use Scalar::Util qw(looks_like_number);
use Socket;
require 'sys/ioctl.ph';

{...............}

sub stop_daemon {   
    # Killing all remaining process
    killing_child();

    # Printing to the FIFO the last log. 
    send_msg("Daemon stopped.\n");
    send_msg("DAEMON_STOPPED\n");
    end_msg();

    # Removing the socket. Do it only when you're sure you don't have any more output to send.
    remove_socket();

    print "Daemon stopped.\n";
    STDOUT->flush();
    exit 0;
}

{..............}

It seems all ok to me and I used this method hundreds of times. But this time I get this error:

Undefined subroutine &Functions::Testd::send_msg called at Functions/Testd.pm line 80.

The only solution I've found is to write the subroutine with its full path, i.e. &Functions::ServerSocket::send_msg. Why is this happening? The same error happen with all functions exported from ServerSocket to Testd. I'm importing ServerSocket function also in other packages and script and everything works fine. Only Testd seems unable to import them.

I can post the whole packages, if you need it.

As someone asked, here is the output of M in the debugger:

'/usr/lib/perl/5.14/auto/POSIX/autosplit.ix' => '/usr/lib/perl/5.14/auto/POSIX/autosplit.ix'
'/usr/lib/perl/5.14/auto/POSIX/load_imports.al' => '/usr/lib/perl/5.14/auto/POSIX/load_imports.al'
'AutoLoader.pm' => '5.71 from /usr/share/perl/5.14/AutoLoader.pm'
'B.pm' => '1.29 from /usr/lib/perl/5.14/B.pm'
'Carp.pm' => '1.20 from /usr/share/perl/5.14/Carp.pm'
'Class/Struct.pm' => '0.63 from /usr/share/perl/5.14/Class/Struct.pm'
'Config.pm' => '/usr/lib/perl/5.14/Config.pm'
'Config_git.pl' => '/usr/lib/perl/5.14/Config_git.pl'
'Config_heavy.pl' => '/usr/lib/perl/5.14/Config_heavy.pl'
'Cwd.pm' => '3.36 from /usr/lib/perl/5.14/Cwd.pm'
'DynaLoader.pm' => '1.13 from /usr/lib/perl/5.14/DynaLoader.pm'
'Errno.pm' => '1.13 from /usr/lib/perl/5.14/Errno.pm'
'Exporter.pm' => '5.64_03 from /usr/share/perl/5.14/Exporter.pm'
'Exporter/Heavy.pm' => '5.64_03 from /usr/share/perl/5.14/Exporter/Heavy.pm'
'Fcntl.pm' => '1.11 from /usr/lib/perl/5.14/Fcntl.pm'
'File/Basename.pm' => '2.82 from /usr/share/perl/5.14/File/Basename.pm'
'File/Find.pm' => '1.19 from /usr/share/perl/5.14/File/Find.pm'
'File/Spec.pm' => '3.33 from /usr/lib/perl/5.14/File/Spec.pm'
'File/Spec/Unix.pm' => '3.33 from /usr/lib/perl/5.14/File/Spec/Unix.pm'
'File/stat.pm' => '1.05 from /usr/share/perl/5.14/File/stat.pm'
'FindBin.pm' => '1.50 from /usr/share/perl/5.14/FindBin.pm'
'Functions/Help.pm' => 'Functions/Help.pm'
'Functions/Launcher.pm' => 'Functions/Launcher.pm'
'Functions/ServerSocket.pm' => 'Functions/ServerSocket.pm'
'Functions/Setup.pm' => 'Functions/Setup.pm'
'Functions/Shell.pm' => 'Functions/Shell.pm'
'Functions/ShellSocket.pm' => 'Functions/ShellSocket.pm'
'Functions/Testd.pm' => 'Functions/Testd.pm'
'Getopt/Long.pm' => '2.38 from /usr/share/perl/5.14/Getopt/Long.pm'
'IO.pm' => '1.25_04 from /usr/lib/perl/5.14/IO.pm'
'IO/Dir.pm' => '1.08 from /usr/lib/perl/5.14/IO/Dir.pm'
'IO/File.pm' => '1.15 from /usr/lib/perl/5.14/IO/File.pm'
'IO/Handle.pm' => '1.31 from /usr/lib/perl/5.14/IO/Handle.pm'
'IO/Pipe.pm' => '1.14 from /usr/lib/perl/5.14/IO/Pipe.pm'
'IO/Seekable.pm' => '1.1 from /usr/lib/perl/5.14/IO/Seekable.pm'
'IO/Socket.pm' => '1.32 from /usr/lib/perl/5.14/IO/Socket.pm'
'IO/Socket/INET.pm' => '1.31 from /usr/lib/perl/5.14/IO/Socket/INET.pm'
'IO/Socket/UNIX.pm' => '1.23 from /usr/lib/perl/5.14/IO/Socket/UNIX.pm'
'JSON.pm' => '2.53 from JSON.pm'
'JSON/PP.pm' => '2.27200 from /usr/share/perl/5.14/JSON/PP.pm'
'JSON/backportPP.pm' => 'JSON/backportPP.pm'
'List/Util.pm' => '1.23 from /usr/lib/perl/5.14/List/Util.pm'
'POSIX.pm' => '1.24 from /usr/lib/perl/5.14/POSIX.pm'
'Proc/Daemon.pm' => '0.14 from Proc/Daemon.pm'
'Proc/Spawn.pm' => '1.03 from Proc/Spawn.pm'
'Scalar/Util.pm' => '1.23 from /usr/lib/perl/5.14/Scalar/Util.pm'
'SelectSaver.pm' => '1.02 from /usr/share/perl/5.14/SelectSaver.pm'
'Socket.pm' => '2.005 from /usr/local/lib/perl/5.14.2/Socket.pm'
'Symbol.pm' => '1.07 from /usr/share/perl/5.14/Symbol.pm'
'Term/ANSIColor.pm' => '3.00 from /usr/share/perl/5.14/Term/ANSIColor.pm'
'Term/Cap.pm' => '1.12 from /usr/share/perl/5.14/Term/Cap.pm'
'Term/ReadLine.pm' => '1.07 from /usr/share/perl/5.14/Term/ReadLine.pm'
'Tie/Hash.pm' => '1.04 from /usr/share/perl/5.14/Tie/Hash.pm'
'Time/HiRes.pm' => '1.972101 from /usr/lib/perl/5.14/Time/HiRes.pm'
'XSLoader.pm' => '0.13 from /usr/share/perl/5.14/XSLoader.pm'
'ZeroMQ.pm' => '0.21 from /usr/local/lib/perl/5.14.2/ZeroMQ.pm'
'ZeroMQ/Constants.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Constants.pm'
'ZeroMQ/Context.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Context.pm'
'ZeroMQ/Message.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Message.pm'
'ZeroMQ/Poller.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Poller.pm'
'ZeroMQ/Raw.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Raw.pm'
'ZeroMQ/Socket.pm' => '/usr/local/lib/perl/5.14.2/ZeroMQ/Socket.pm'
'_h2ph_pre.ph' => '/usr/lib/perl/5.14/_h2ph_pre.ph'
'asm-generic/ioctl.ph' => '/usr/lib/perl/5.14/asm-generic/ioctl.ph'
'asm-generic/ioctls.ph' => '/usr/lib/perl/5.14/asm-generic/ioctls.ph'
'asm/ioctl.ph' => '/usr/lib/perl/5.14/asm/ioctl.ph'
'asm/ioctls.ph' => '/usr/lib/perl/5.14/asm/ioctls.ph'
'base.pm' => '2.16 from /usr/share/perl/5.14/base.pm'
'bits/ioctl-types.ph' => '/usr/lib/perl/5.14/bits/ioctl-types.ph'
'bits/ioctls.ph' => '/usr/lib/perl/5.14/bits/ioctls.ph'
'bits/predefs.ph' => '/usr/lib/perl/5.14/bits/predefs.ph'
'bits/wordsize.ph' => '/usr/lib/perl/5.14/bits/wordsize.ph'
'bytes.pm' => '1.04 from /usr/share/perl/5.14/bytes.pm'
'constant.pm' => '1.21 from /usr/share/perl/5.14/constant.pm'
'features.ph' => '/usr/lib/perl/5.14/features.ph'
'gnu/stubs-64.ph' => '/usr/lib/perl/5.14/gnu/stubs-64.ph'
'gnu/stubs.ph' => '/usr/lib/perl/5.14/gnu/stubs.ph'
'linux/ioctl.ph' => '/usr/lib/perl/5.14/linux/ioctl.ph'
'overload.pm' => '1.13 from /usr/share/perl/5.14/overload.pm'
'perl5db.pl' => '1.33 from /usr/share/perl/5.14/perl5db.pl'
'strict.pm' => '1.04 from /usr/share/perl/5.14/strict.pm'
'sys/cdefs.ph' => '/usr/lib/perl/5.14/sys/cdefs.ph'
'sys/ioctl.ph' => '/usr/lib/perl/5.14/sys/ioctl.ph'
'sys/ttydefaults.ph' => '/usr/lib/perl/5.14/sys/ttydefaults.ph'
'threads.pm' => '1.83 from /usr/lib/perl/5.14/threads.pm'
'vars.pm' => '1.02 from /usr/share/perl/5.14/vars.pm'
'warnings.pm' => '1.12 from /usr/share/perl/5.14/warnings.pm'
'warnings/register.pm' => '1.02 from /usr/share/perl/5.14/warnings/register.pm'

Upvotes: 2

Views: 691

Answers (1)

h4ck3rm1k3
h4ck3rm1k3

Reputation: 2100

Works for me, please reduce the example and post a full one.

also use perl -d test.pl and post the output of : M (http://obsidianrook.com/devnotes/talks/perl_debugger/)

DB<1> M
 'example1.pm' => 'example1.pm'
 'example2.pm' => 'example2.pm'

example1.pm

package example1;
use strict;
use warnings;
use example2 qw(get_interface_address);
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw( send_msg );
# Send a message
sub send_msg {
    my $msg = shift;
    warn "MSG: \"$msg\" sent.\n";
}

example2.pm

package example2;
use strict;
use warnings;
use example1 qw(send_msg );
use base 'Exporter';
use vars qw/ @EXPORT_OK /;
@EXPORT_OK = qw( test );
sub test  {   
    send_msg("DAEMON_STOPPED\n");
    exit 0;
}
1;

test1.pl

use strict;
use warnings;
use example2 qw(test);
test;

output

perl test1.pl
MSG: "DAEMON_STOPPED
" sent.

Update1

I have taken your code and done some small modifications to remote the circular include.

It is hosted here : https://gist.github.com/3391956

Installed like this :

apt-get install libzmq1  libzmq-dev
apt-get install uuid-dev libuuid-perl
cpan ZeroMQ

Here is the output,

mdupont@localhost:~/experiments/stackoverflow/perl/gist-3391956$ perl test.pl
ERROR: User name does not exist.
********* simple selection *********  ********* selection by list *********
-A all processes                      -C by command name
-N negate selection                   -G by real group ID (supports names)
-a all w/ tty except session leaders  -U by real user ID (supports names)
-d all except session leaders         -g by session OR by effective group name
-e all processes                      -p by process ID
T  all processes on this terminal     -s processes in the sessions given
a  all w/ tty, including other users  -t by tty
g  OBSOLETE -- DO NOT USE             -u by effective user ID (supports names)
r  only running processes             U  processes for specified users
x  processes w/o controlling ttys     t  by tty
*********** output format **********  *********** long options ***********
-o,o user-defined  -f full            --Group --User --pid --cols --ppid
-j,j job control   s  signal          --group --user --sid --rows --info
-O,O preloaded -o  v  virtual memory  --cumulative --format --deselect
-l,l long          u  user-oriented   --sort --tty --forest --version
-F   extra full    X  registers       --heading --no-heading --context
                        ********* misc options *********
-V,V  show version      L  list format codes  f  ASCII art forest
-m,m,-L,-T,H  threads   S  children in sum    -y change -l format
-M,Z  security data     c  true command name  -c scheduling class
-w,w  wide output       n  numeric WCHAN,UID  -H process hierarchy
Argument is not an object at Functions/ServerSocket.pm line 84.

Still I dont see the problem.

Upvotes: 1

Related Questions