如何将一堆 perl 模块加载到 perl 脚本中 [英] How to load a bunch of perl modules into a perl script

查看:55
本文介绍了如何将一堆 perl 模块加载到 perl 脚本中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

在我制作/开发的每个脚本中,我总是调用 perl 库和子例程,例如:

In every script I make/develop I always call perl libraries and sub routines like:

#! /directory/bin/perl
system('source /directory/.cshrc&');

use Net::Domain qw(hostname hostfqdn hostdomain);
use Time::Local;
use Time::Piece;
use Switch;
use Exporter;
#use strict;

use Data::Dumper qw(Dumper);

use Time::Local;                                        
use Time::Piece; 
use Time::Seconds();                                       

use Tk;
use Tk::BrowseEntry; 
use Tk::Balloon;                                   
use Tk::widgets qw(Checkbutton BrowseEntry);            
use Tk::NoteBook;                                       
use Tk::Pane;                                           

use DBI;
use DBD::Oracle;
$ORACLE_HOME = "/lolDirectory/10.2.0/elinux";    
$ENV{ORACLE_HOME}=$ORACLE_HOME; 

###############
# SUBROUTINES #
###############

&ownerChecker;
&processChecker;    

<小时>

我希望我可以将所有这些放到一个文件中并将其加载到 perl 脚本中,同时运行它,就好像它是 perl 脚本本身的一部分一样:


I wish i can put all those to a file and load it to a perl script, in the same time running it as if it is part of the perl script itself like:

#! /directory/bin/perl

# load the content of the file and run it as a part of the script

<小时>

这可能吗?如果可能?如果可能的话,从调用库到调用检查器脚本可能是非常通用和标准的.


Is this possible? If it is possible? If it is possible, from calling libs to calling checker scripts could be very generic and standard.

推荐答案

创建一个加载其他标准"模块的模块是诸如 perl5iModern::Perl.

Creating one module that loads other "standard" modules is the motivation for things like perl5i and Modern::Perl.

具有词法效果的 Pragma 模块,例如 strict、warnings 和 autodie,只需在模块的导入例程中加载即可.需要告诉导出函数的模块将它们的模块导出到别处,这可以通过 Import::Into 来完成.最后,只需要加载类.

Pragma modules with lexical effect such as strict, warnings and autodie simply need to be loaded in your module's import routine. Modules which export functions need to be told to export their modules elsewhere, which can be done with Import::Into. Finally, classes simply need to be loaded.

由于 use 发生在编译时,您需要在运行时 require 模块并调用其 import 方法.

Since use happens at compile time, you need to do the equivalent at runtime which is requireing the module and calling its import method.

这是一个打开严格和警告、加载 Time::Local 和加载 Time::Piece 以及激活 say 和 switch 功能的示例.

Here's an example of turning on strict and warnings, loading Time::Local and loading Time::Piece, and activating the say and switch features.

package My::Perl;

use strict;
use warnings;
use Import::Into;

sub import {
    # import is called as a class method
    my $class = shift;

    # The class which 'use'd this module
    my $caller = caller;

    # same as "use strict" but happens when import() is called.
    require strict;
    "strict"->import;

    # use warnings;
    require warnings;
    "warnings"->import;

    # use Time::Local;
    # use Time::Piece;
    Time::Local->import::into($caller);
    Time::Piece->import::into($caller);

    # use feature qw(say switch);
    require feature;
    feature->import(qw(say switch));
}

1;

现在你只需要加载那个模块.

Now you just have to load that one module.

use My::Perl;

say localtime->year;

别太疯狂了,你希望这些是通用的.如果您不打算使用它们,加载 DBI 和 Tk 是很愚蠢的.如果您想加载一堆 Tk 模块,请创建一个单独的 My::Tk 模块来执行此操作.出于同样的原因,我不会让模块执行任何代码.

Don't go too crazy, you want these to be generically useful. It's silly to load DBI and Tk if you're not going to use them. If you want to load a bunch of Tk modules, make a separate My::Tk module to do that. And I wouldn't have the modules execute any code for the same reason.

这篇关于如何将一堆 perl 模块加载到 perl 脚本中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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