开发者

Accessing perl subroutine from different perl script

开发者 https://www.devze.com 2023-03-09 03:31 出处:网络
I had 1 perl script in which we write couple of subroutines. Example: # Tr开发者_JAVA技巧y_1.pl main();

I had 1 perl script in which we write couple of subroutines. Example:

# Tr开发者_JAVA技巧y_1.pl

main();

sub main{
---
---
 check();
}

check {
--
--}

Now, i wrote another script Try_2.pl, in which i want to call check subroutine of perl script Try_1.pl.


It sounds like you want to create a module. Try_1.pm (Edit: note extension) should have the following form:

package Try_1;
use base 'Exporter';
our @EXPORT = qw(check);

sub check {
}

1;

And then Try_2.pl needs to get that code:

use Try_1 qw(check);

That what you're looking for?


If you are not using modules (extension .pm) but instead use libraries (extension .pl):

require 'Try_1.pl';
check();

Make sure that both files Try_1.pl and Try_2.pl are in the same directory.


You may need this

test1.pl:

use Routines;
{    
    my $hello = "hello123";
    hello( $hello );    
    # ...
}

test2.pl:

package Routines;
sub hello 
{
    my $hello = shift;
    print "$hello\n";
}
1;


"run"'s answer tested work, but need to call like "Try_1::check()". Otherwise show error "Undefined subroutine &main::check()".

0

精彩评论

暂无评论...
验证码 换一张
取 消