Perl CLI フレームワークを使用してスクリプトを記述しています。ここで、制御モジュールからサブコマンド モジュールに変数を渡したいと思います。制御モジュールで変数をグローバル変数として設定しようとしましたが、サブコマンド モジュールはまだ変数を取得できません。同じモジュール内でグローバル変数を共有することさえできません。スクリプトを実行すると、いくつかのエラー メッセージが表示されます。
[root@old]# perl pc --ip=dsfa --device dsfasdf on Power/Control.pm 行 63 の連結 (.) または文字列で初期化されていない値 $Power::Control::data が使用されています。
globla データは、連結 (.) または Power/Control.pm 75 行目の文字列で初期化されていない値 $Power::Control::data を使用しています。
親からのデータは
デバイス名は dsfasdf です
IP アドレスは dsfa です。
これは上のコマンドです
スクリプト pc は次のとおりです。
#! /usr/bin/perl
use strict;
use warnings;
use Power::Control;
use lib 'lib';
# ---- EXECUTION ----
Power::Control->run(); # Launch command
Power/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|d=s@' => 'device name' ],
#}
sub run {
print "\n This is the command on\n";
}
1;