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

Hello I am trying to export a variable from a Module called Routines I have created. I am able to run functions from the same module in a script but not able to use variables I have in the Module in the same script.... any ideas ?? see code below I am trying to print the $command_showconfig variable in the script using the Routines module. I have included the module and the script below;
package Routines; use strict; use warnings; use Net::SSH::Expect; use Expect; use Carp; use IO::Pty; use HTTP::Request; use LWP::UserAgent; BEGIN { use Exporter(); use vars qw ($VERSION @ISA @EXPORT); $VERSION=1.00; @EXPORT = qw(%args &clear_config &load_config &show_config $c +ommand_showconfig); @ISA = qw(Exporter); } ###### show config #### sub show_config { my $ssh2 = Net::SSH::Expect->new ( host => "item201", user => 'root', password => 'admin', raw_pty => 1, timeout => 1 ); my $login_output2 = $ssh2->login(); if ($login_output2 !~ /Blah/) { die "Login has failed. Login output was $login_output2"; } $ssh2->exec("stty raw -echo"); my $command_suconfig = $ssh2->exec('su config'); sleep(2); my $command_showconfig = $ssh2->exec('show config'); print "\n############ SHOW CONFIG ###########\n"; print "\n$command_showconfig\n"; #Issue the commands from the command file } SECTION of SCRIPT USING MODULE (The function, &show_config works) #!/usr/bin/perl #$Expect::Exp_Internal = 1; #$Expect::Debug = 1; #use strict; #use warnings; #use diagnostics; use Routines; use Net::SSH::Expect; use Expect; &show_config; @CONFIG=undef(); push (@CONFIG,$command_showconfig; print "$command_showconfig\n"; print @CONFIG; # END OF CODE SEGMENT

Replies are listed 'Best First'.
Re: cannot export from Module
by ELISHEVA (Prior) on Aug 13, 2009 at 16:46 UTC

    Variables declared with my aren't visible outside the module. Use our to declare variables if you want your scripts to see the value of a variable defined within a module.

    Best, beth

Re: cannot export from Module
by Marshall (Canon) on Aug 14, 2009 at 16:29 UTC
    I see a number of problems.

    1) FIRST: A Perl module should be set up like a Perl executable module. So that you can have a test() target that does a "sanity check" for debugging, etc. That means that that this module should start like any other Perl module. You are Windows user, but this is the standard preamble that will allow your programs to work on Unix. This is completely compatible with Windows and even the -w option on the first line is recognized as "use warnings" although the script path is ignored.

    #!/usr/bin/perl -w use strict;
    2. Use of BEGIN {} is not needed here.

    3. use of address of sub_name is not needed: &load_config, just load_config is better.

    4. use of @EXPORT (%args) makes no sense. You have no %args to export, but this would not be an error.

    There are vars that are exported by default and some that can be exported upon "request" by caller. All subroutine names are "eligible for export". A scalar can only be exported if it has "our" declaration (package scope) - you don't have any so this is not an issue here.

    I think you will have much better luck with this preamble:

    #!/usr/bin/perl -w use strict; package Routines; #File is Routines.pm IMPORTANT! use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.0; our @ISA = qw(Exporter); our @EXPORT = qw( clear_config load_config show_config command_showconfig ); our @EXPORT_OK = qw();
    Calling program does a
    use Routines;

    I did not do any testing of you code so I don't know if it works or not.