如何从另一个 perl 文件添加库? [英] How to add a library from another perl file?

查看:39
本文介绍了如何从另一个 perl 文件添加库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个库导入到我的 perl 脚本中.以下是我的尝试:

I want to import a library into my perl script. Below is my attempt:

function.pl

#!/usr/bin/perl
package main; 
use strict;
use warnings;
use 5.005;
use Term::ANSIColor qw(:constants); 
use LWP::Simple;    
use diagnostics;
use File::Spec;
use Getopt::Long;
use File::Basename;
use Cwd 'abs_path';

sub myfunction {
    print RED, " Deleting...", RESET;
    system("rm –f /file_location/*.");
    print "deleted.\n";      
}

我想在这个新的 perl 脚本中导入 function.pl.

I want to import function.pl in this new perl script.

#!/usr/bin/perl    
package main; 

myfunction;
myfunciton2;

推荐答案

如果您只想要一个包含多个实用程序子例程的容器,那么您应该使用 Exporter

If you just want a container for a number of utility subroutines then you should create a library module using Exporter

将包和模块文件命名为 main 以外的其他名称,这是主程序使用的默认包.在下面的代码中,我编写了包含 package Functions 的模块文件 Functions.pm.名称​​必须匹配

Name your package and your module file something other than main, which is the default package used by your main program. In the code below I have written module file Functions.pm which contains package Functions. The names must match

package Functions;

use strict;
use warnings;

use Exporter 'import';
our @EXPORT_OK = qw/ my_function /;

use Term::ANSIColor qw(:constants); 


sub my_function {
    print RED, " Deleting...", RESET;
    system("rm –f /file_location/*.");
    print "deleted.\n";      
}

1;

程序.pl

#!/usr/bin/perl    

use strict;
use warnings 'all';

use Functions qw/ my_function /;

my_function();

这篇关于如何从另一个 perl 文件添加库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆