从命令行调用perl子例程

前端之家收集整理的这篇文章主要介绍了从命令行调用perl子例程前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,我想知道如何从命令行调用perl子例程.因此,如果我的程序被称为测试,并且子程序被称为字段,我想从命令行调用它.

测试领​​域

解决方法

使用调度表.
#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

sub fields {
  say 'this is fields';
}

sub another {
  say 'this is another subroutine';
}

my %functions = (
  fields  => \&fields,another => \&another,);

my $function = shift;

if (exists $functions{$function}) {
  $functions{$function}->();
} else {
  die "There is no function called $function available\n";
}

一些例子:

$./dispatch_tab fields
this is fields
$./dispatch_tab another
this is another subroutine
$./dispatch_tab xxx
There is no function called xxx available
原文链接:https://www.f2er.com/Perl/172819.html

猜你在找的Perl相关文章