如何调用在同一文件中定义的Perl程序包? [英] How can I call a Perl package I define in the same file?

查看:70
本文介绍了如何调用在同一文件中定义的Perl程序包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一些模块,并在同一文件中全部使用它们.不,我不能更改要求.

I need to define some modules and use them all in the same file. No, I can't change the requirement.

我想做以下事情:

{
    package FooObj;

    sub new { ... }

    sub add_data { ... }
}

{
    package BarObj;

    use FooObj;

    sub new { 
        ... 
        # BarObj "has a" FooObj
        my $self = ( myFoo => FooObj->new() );
        ...
    }

    sub some_method { ... }
}

my $bar = BarObj->new();

但是,这导致出现消息:

However, this results in the message:

在@INC中找不到FooObj.pm ...
开始失败...

Can't locate FooObj.pm in @INC ...
BEGIN failed...

我如何使它工作?

推荐答案

拖放use.认真地.

use告诉perl从另一个文件中读取代码,这是不需要的,因为代码位于同一文件中.

use tells perl to read in the code from another file, which you don't need to do because the code is in the same file.

这篇关于如何调用在同一文件中定义的Perl程序包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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