在 Perl 中调用具有不同属性的函数

发布于 2024-10-13 07:34:35 字数 1538 浏览 9 评论 0原文

我编写了一个 Perl 脚本,它将启动 SNMP 会话并提取数据/计数器及其值到 csv 文件。有7个perl脚本;顶部有不同的属性/定义/变量..但引擎是相同的。

此时,除了定义的变量之外,那 7 个 perl 脚本都是多余的。有没有办法将执行 perl 脚本保留为属性/执行文件并将引擎保留在另一个文件中?此属性/执行 perl 脚本将调用引擎(使用其自己的脚本中定义的属性)。

简而言之,我想在自己的脚本中使用变量(也作为执行),但从统一的“引擎”调用特定的函数。

检索_mibs1.pl检索_mibs2.pl 检索_mibs3.pl 检索_mibs4.pl 检索_mibs5.pl 检索_mibs6.pl 检索_mibs7.pl

检索_mibs1.pl

#!/usr/local/bin/perl

use Net::SNMP;

##DEFINITION START

my @Servers = (
  'server1',
  'server2',
);

my $PORT = 161;

my $COMMUNITY = 'secret';

my $BASEOID = '1.2.3.4.5.6.7.8';

my $COUNTERS = [
  [11,'TotalIncomingFromPPH'],
  [12,'TotalFailedIncomingFromPPH'],
];

##ENGINE START
sub main {
  my $stamp = gmtime();
  my @oids = ();
  foreach my $counter (@$COUNTERS) {
    push @oids,("$BASEOID.$$counter[0].0");
  }
  foreach my $server (@Servers) {
    print "$stamp$SEPARATOR$server";
    my ($session,$error) = Net::SNMP->session(-version => 1,-hostname => $server,-port => $PORT,-community => $COMMUNITY);
    if ($session) {
      my $result = $session->get_request(-varbindlist => \@oids);
      if (defined $result) {
        foreach my $oid (@oids) {
          print $SEPARATOR,$result->{$oid};
        }
      } else {
        print STDERR "$stamp Request error: ",$session->error,"\n";
        print "$SEPARATOR-1" x scalar(@oids);
      }
    } else {
      print STDERR "$stamp Session error: $error\n";
      print "$SEPARATOR-1" x scalar(@oids);
    }
    print "\n";
  }
}
main();

I have written a Perl script that would start a SNMP session and extracting the data/counters and it's value to a csv file. There are 7 perl scripts; different properties/definition/variables on the top.. but the engine is the same.

At this point, those 7 perl scripts are redundant except for the defined variables. Is there a way to keep the execution perl script as a properties/execution file and keep the engine in a another file? This properties/execution perl script will call the engine (using the properties defined in it's own script).

So in short, I want to use the variables in their own script (as an execution as well), but calls a specific function from a unified "engine".

i.e.

retrieve_mibs1.pl retrieve_mibs2.pl
retrieve_mibs3.pl
retrieve_mibs4.pl
retrieve_mibs5.pl
retrieve_mibs6.pl
retrieve_mibs7.pl

retrieve_mibs1.pl

#!/usr/local/bin/perl

use Net::SNMP;

##DEFINITION START

my @Servers = (
  'server1',
  'server2',
);

my $PORT = 161;

my $COMMUNITY = 'secret';

my $BASEOID = '1.2.3.4.5.6.7.8';

my $COUNTERS = [
  [11,'TotalIncomingFromPPH'],
  [12,'TotalFailedIncomingFromPPH'],
];

##ENGINE START
sub main {
  my $stamp = gmtime();
  my @oids = ();
  foreach my $counter (@$COUNTERS) {
    push @oids,("$BASEOID.$counter[0].0");
  }
  foreach my $server (@Servers) {
    print "$stamp$SEPARATOR$server";
    my ($session,$error) = Net::SNMP->session(-version => 1,-hostname => $server,-port => $PORT,-community => $COMMUNITY);
    if ($session) {
      my $result = $session->get_request(-varbindlist => \@oids);
      if (defined $result) {
        foreach my $oid (@oids) {
          print $SEPARATOR,$result->{$oid};
        }
      } else {
        print STDERR "$stamp Request error: ",$session->error,"\n";
        print "$SEPARATOR-1" x scalar(@oids);
      }
    } else {
      print STDERR "$stamp Session error: $error\n";
      print "$SEPARATOR-1" x scalar(@oids);
    }
    print "\n";
  }
}
main();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2024-10-20 07:34:35

您可以使用 eval 来完成此操作:在一个文件中设置变量,然后打开引擎并评估其内容。

variables.pl(设置变量并调用引擎):

use warnings;
use strict;
use Carp;
use English '-no_match_vars';

require "engine.pl"; # so that we can call it's subs

# DEFINITION START
our $VAR1    = "Hello";
our $VAR2    = "World";

# CALL THE ENGINE
print "START ENGINE:\n";
engine(); # call engine
print "DONE\n";

engine.pl(实际工作内容):

sub engine{
    print "INSIDE ENGINE\n";
    print "Var1: $VAR1\n";
    print "Var2: $VAR2\n";
}
1;  # return a true value

其他替代方法是:

You could do it using eval: set up the variables in one file, then open the engine and eval it's content.

variables.pl (set up your variables and call the engine):

use warnings;
use strict;
use Carp;
use English '-no_match_vars';

require "engine.pl"; # so that we can call it's subs

# DEFINITION START
our $VAR1    = "Hello";
our $VAR2    = "World";

# CALL THE ENGINE
print "START ENGINE:\n";
engine(); # call engine
print "DONE\n";

engine.pl (the actual working stuff):

sub engine{
    print "INSIDE ENGINE\n";
    print "Var1: $VAR1\n";
    print "Var2: $VAR2\n";
}
1;  # return a true value

Other alternatives would be:

黄昏下泛黄的笔记 2024-10-20 07:34:35

我立即想到两个想法:

为您的公共代码构建一个 Perl 模块,然后要求或使用模块。 (区别主要在于您是否要运行 LynxLee::run_servers() 还是 run_servers() ——您是否希望该模块影响您当前的作用域。)

使用符号链接:创建这些符号链接:retrieve_mibs1.pl ->检索_mibs.pl 检索_mibs2.pl -> retrieve_mibs.pl 等,然后根据程序名称设置变量:

#!/usr/bin/perl -w

use File::Basename;

my $name = basename($0);

my @Servers, $PORT, $COMMUNITY, $BASEOID, $COUNTERS;

if($name ~= /retrieve_mibs1\.pl/) {
    @Servers = (
        'server1',
        'server2',
    );

    # ...
} elsif ($name ~= /retrieve_mibs2\.pl/) {
    @Servers = (
        'server3',
        'server4',
    );

    # ...
}

用程序名称索引到哈希中以检索参数会更清晰,但我不太擅长 Perl 引用。 :)

Two thoughts come to mind immediately:

Build a Perl module for your common code, and then require or use the module as your needs dictate. (The difference is mostly whether you want to run LynxLee::run_servers() or run_servers() -- do you want the module to influence your current scope or not.)

Use symbolic links: create these symlinks: retrieve_mibs1.pl -> retrieve_mibs.pl retrieve_mibs2.pl -> retrieve_mibs.pl, and so on, then set the variables based on the program name:

#!/usr/bin/perl -w

use File::Basename;

my $name = basename($0);

my @Servers, $PORT, $COMMUNITY, $BASEOID, $COUNTERS;

if($name ~= /retrieve_mibs1\.pl/) {
    @Servers = (
        'server1',
        'server2',
    );

    # ...
} elsif ($name ~= /retrieve_mibs2\.pl/) {
    @Servers = (
        'server3',
        'server4',
    );

    # ...
}

Indexing into a hash with the name of the program to retrieve the parameters would be much cleaner, but I'm not so good at Perl references. :)

一向肩并 2024-10-20 07:34:35

我不确定问题是什么,所以我猜测一下。除了一些变量之外,您在不同位置的代码每次都是相同的。这就是子程序的定义。


也许问题是您不知道如何在这些不同的脚本中包含通用代码。这相当简单:您在 perl 模块中编写该代码。这基本上是一个以 pm 而不是 pl 结尾的文件。当然,您必须处理很多事情,例如导出函数。 Perldoc 应该有很大帮助。

I'm not sure what the problem is so I'm guessing a little. You have code in various places that is the same each time save for some variables. This is the very definition of a subroutine.


Maybe the problem is that you don't know how to include the common code in those various scripts. This is fairly easy: You write that code in a perl module. This is basically a file ending in pm instead of pl. Of course you have to take care of a bunch of things such as exporting your functions. Perldoc should be of great help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文