从命令行调用 perl 子程序

从命令行调用 perl 子程序

问题描述:

好的,所以我想知道如何从命令行调用 perl 子例程.所以如果我的程序被称为测试,而子程序被称为字段,我想从命令行调用它.

Ok so i was wondering how i would go about calling a perl subroutine from the command line. So if my program is Called test, and the subroutine is called fields i would like to call it from the command line like.

测试字段

使用调度表.

#!/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