2

我有一个 perl 包ServerSocket.pm,如下所示:

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";
}

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

我在另一个名为的包中使用了这个包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;
}

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

对我来说一切都很好,我使用了这种方法数百次。但是这次我得到了这个错误:

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

我找到的唯一解决方案是编写带有完整路径的子例程,即&Functions::ServerSocket::send_msg. 为什么会这样?从 ServerSocket 导出到 Testd 的所有函数都会发生同样的错误。我也在其他包和脚本中导入 ServerSocket 函数,一切正常。只有 Testd 似乎无法导入它们。

如果您需要,我可以发布整个包裹。

正如有人问的那样,这是调试器中 M 的输出:

'/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'
4

1 回答 1

1

对我有用,请减少示例并发布完整示例。

还使用 perl -d test.pl 并发布以下输出:M(http://obsidianrook.com/devnotes/talks/perl_debugger/

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

例子1.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";
}

例子2.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;

测试1.pl

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

输出

perl test1.pl
MSG: "DAEMON_STOPPED
" sent.

更新1

我已经获取了您的代码并对远程循环包含进行了一些小的修改。

它托管在这里:https ://gist.github.com/3391956

像这样安装:

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

这是输出,

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.

我仍然没有看到问题。

于 2012-08-18T19:05:16.410 回答