ballstar has asked for the wisdom of the Perl Monks concerning the following question:

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:
[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. The globla data is Use of uninitialized value $Power::Control::data in concatenation (.) +or string at Power/Control.pm line 75. The data from parent is The device name is dsfasdf The ip address is dsfa This is the command on
Here is the script pc:
#! /usr/bin/perl use strict; use warnings; use Power::Control; use lib 'lib'; # ---- EXECUTION ---- Power::Control->run(); # Launch command
Here is the 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;

Replies are listed 'Best First'.
Re: How to share the global variables between two modules by using Perl CLI Framework?
by Preceptor (Deacon) on Jun 12, 2013 at 19:39 UTC
    At risk of missing the point of the question you're asking - have you had a look at the module 'Exporter'? It's ... well, it's basically geared up to exporting (and importing) variables from modules.

    Anyway, that aside - the reason you get those to errors, is because the module 'runs' when you import it. The print statements on like 63 and 75 aren't encapsulated within a subroutine, so they run at the same time as you import the module - which is before the 'init' subroutine is called.

    You will get the same error with a blank script that just has the 'use' lines in it, I think.

    #!/usr/bin/perl use strict; use warnings; package testfish; sub sub_to_do_something { print "Doing something \n"; } print "FISH\n"; 1;
    And script:
    #!/usr/bin/perl use strict; use warnings; use testfish; print "Not done anything with the module yet\n"; &testfish::sub_to_do_something();
    Gives an output of:
    FISH Not done anything with the module yet Doing something
    Which is more or less the same as you're doing with that 'print' line in your modules. Init hasn't been called yet. (I assume it's something that CLI::Framework requires and uses later).
Re: How to share the global variables between two modules by using Perl CLI Framework?
by Anonymous Monk on Jun 13, 2013 at 00:19 UTC