R
R
ReD2018-02-21 09:32:33
Perl
ReD, 2018-02-21 09:32:33

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:

getlib.pm:
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

1 answer(s)
P
pcdesign, 2018-02-21
@trinitr0

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;

And the names of packages and package files are usually written with a capital letter, and those included in the perl distribution with a small letter. In general, in the TMTOWTDI pearl, and there are many more options to do this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question