Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to share the global variables between two modules by using Perl CLI Framework?
    text
    copied!<p>I am using Perl CLI Framework to write some script. Now I want to pass the variable from the control module to the sub-command module. I have tried to set the variable as the global variable in the control module but the sub-command module still cannot get the variable. It is even not able to share the global variable in the same module. There are some error messages when executing the script:</p> <p>[root@old]# perl pc --ip=dsfa --device dsfasdf on Use of uninitialized value $Power::Control::data in concatenation (.) or string at Power/Control.pm line 63.</p> <p>The globla data is Use of uninitialized value $Power::Control::data in concatenation (.) or string at Power/Control.pm line 75.</p> <p>The data from parent is </p> <p>The device name is dsfasdf</p> <p>The ip address is dsfa</p> <p>This is the command on</p> <p>Here is the script pc:</p> <pre><code>#! /usr/bin/perl use strict; use warnings; use Power::Control; use lib 'lib'; # ---- EXECUTION ---- Power::Control-&gt;run(); # Launch command </code></pre> <p>Here is the Power/Control.pm:</p> <pre><code>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' =&gt; 'device name' ], [ 'ip=s' =&gt; 'ip address' ], [ 'user|u=s' =&gt; 'user name' ], [ 'password|p=s' =&gt; 'password' ], [ 'interval|i=s' =&gt; 'interval' ], [ 'brand|b=s' =&gt; 'brand' ], [ 'community|c=s' =&gt; 'community' ], [ 'version|v=s' =&gt; 'version' ], } sub command_map { on =&gt; 'Power::Control::Command::On', off =&gt; 'Power::Control::Command::Off', reboot =&gt; 'Power::Control::Command::Reboot', version =&gt; 'Power::Control::Command::Version', status =&gt; 'Power::Control::Command::Status', sysstat =&gt; 'Power::Control::Command::Sysstat', } sub command_alias { r =&gt; 'reboot', v =&gt; 'version', st =&gt; 'status', sys =&gt; 'sysstat', } our $opts; our $self; our $data; sub init { ($self, $opts) = @_; $data = $opts-&gt;{'ip'}; print "\n The device name is $opts-&gt;{'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=&lt;device name&gt;: Power on the device } } #sub option_spec { # [ 'device|d=s@' =&gt; 'device name' ], #} sub run { print "\n This is the command on\n"; } 1; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload