Answer the question
In order to leave comments, you need to log in
How to pass the value of a variable to a module in Perl?
Hello!
How can perl pass the value of a variable from a main module to a plug-in?
I tried this (you need to pass the value of the $ip variable to the getlib module):
main.pl:
use getlib;
use strict;
...
while (@row = $sth -> fetchrow_array())
{
$ip = $row[0];
foreach $ip(@row)
{
print "$ip - OK!, then $ip to continue...\n";
getconf();
}
}
getlib:
package getlib;
use Exporter 'import';
our @EXPORT_OK = /getconf/;
sub getconf {
...
$telnet -> open($::ip); #connect to switch
...
}
1;
Answer the question
In order to leave comments, you need to log in
OOP - style
# main.pl
use strict;
use warnings;
use utf8;
use Getlib;
my $glib = Getlib->new( ip => '127.0.0.1' );
my $res = $glib->getconf( );
# Getlib.pm
use strict;
use warnings;
package Getlib;
sub new {
my ( $class, %self ) = @_;
bless \%self, $class;
return \%self;
}
sub getconf {
my $self = shift;
print $self->{ip}, "\n";
return "aaaa";
}
1;
# perl main.pl
127.0.0.1
# main.pl
use strict;
use warnings;
use utf8;
require "Getlib.pm";
my $glib = Getlib::getconf( '127.0.0.1' );
# Getlib.pm
use strict;
use warnings;
package Getlib;
sub getconf {
my $ip = shift;
print $ip, "\n";
return "aaaa";
}
1;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question