我正在使用Perl CLI框架來編寫一些腳本。現在我想將控制模塊中的變量傳遞給子命令模塊。我試圖將變量設置爲控制模塊中的全局變量,但子命令模塊仍然無法獲取變量。它甚至無法在同一模塊中共享全局變量。執行腳本時會出現一些錯誤消息:如何使用Perl CLI Framework共享兩個模塊之間的全局變量?
[root @ old]#perl pc --ip = dsfa --device dsfasdf on 使用未初始化的值$ Power :: Control :: data in concatenation(。)或字符串在電源/ Control.pm線63
的globla數據是 使用未初始化值$電源的::控制::數據在級聯(。)或字符串在電源/ Control.pm線75
來自父代的數據是
設備名稱是dsfasdf
個的IP地址是DSFA
這是
這裏的命令腳本PC:
#! /usr/bin/perl
use strict;
use warnings;
use Power::Control;
use lib 'lib';
# ---- EXECUTION ----
Power::Control->run(); # Launch command
下面是電源/ Control.pm:
package Power::Control;
use base qw(CLI::Framework);
use strict;
use warnings;
sub usage_text {
qq{
$0 [--verbose|v]:
OPTIONS:
--verbose -v: be vebose
ARGUMENTS (subcommands):
on: power on the device
off: power off the device
reboot: reboot the device
version: show PDU version
status: show PDU status
sysstat: show PDU sysstatus
}
}
sub option_spec {
[ 'device|d=s' => 'device name' ],
[ 'ip=s' => 'ip address' ],
[ 'user|u=s' => 'user name' ],
[ 'password|p=s' => 'password' ],
[ 'interval|i=s' => 'interval' ],
[ 'brand|b=s' => 'brand' ],
[ 'community|c=s' => 'community' ],
[ 'version|v=s' => 'version' ],
}
sub command_map {
on => 'Power::Control::Command::On',
off => 'Power::Control::Command::Off',
reboot => 'Power::Control::Command::Reboot',
version => 'Power::Control::Command::Version',
status => 'Power::Control::Command::Status',
sysstat => 'Power::Control::Command::Sysstat',
}
sub command_alias {
r => 'reboot',
v => 'version',
st => 'status',
sys => 'sysstat',
}
our $opts;
our $self;
our $data;
sub init {
($self, $opts) = @_;
$data = $opts->{'ip'};
print "\n The device name is $opts->{'device'}\n";
print "\n The ip address is $data\n";
}
print "\n The globla data is $data\n";
1;
# ---- COMMAND: On ----
package Power::Control::Command::On;
use base qw(CLI::Framework::Command);
use strict;
use warnings;
use Power::Control;
use Data::Dumper;
print "\n The data from parent is $data \n";
sub usage_text {
q{
on [--d=<device name>: Power on the device
}
}
#sub option_spec {
# [ 'device|[email protected]' => 'device name' ],
#}
sub run {
print "\n This is the command on\n";
}
1;
爲什麼不分開文件 - 每個文件一個模塊/函數?然後你的控制器模塊就可以進行system()調用。 –
這聽起來像你正在訪問某個地方的錯誤範圍內的東西,但誰知道肯定?您發佈的代碼已混淆,部分內容似乎已被雙重粘貼或粘貼在缺失的行上。你可以清理代碼,然後告訴我們腳本中有什麼「'''pc'''」?這是您獲得有用回覆的唯一方法。 –
我已更新我的帖子。添加了腳本電腦。 – user2476350