Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Calling subroutine in a package from other perl sub.

by Anonymous Monk
on Jan 16, 2001 at 11:05 UTC ( [id://52182]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I've a doubt. How can i call a subroutine in one perl file from another perl file?. I've two perl files namely One.pl and Two.pl Please have a look at the below code.
#file name: One.pl package MyPerl; sub Main_Fn { # Here i would like to call the subroutine # ProcessData()in file Two.pl } 1; #file name: Two.pl package Second_Pak; sub ProcessData { # doing some processing here. } 1;
If we make the second file (Two.pl) as a perl module, then we can invoke the sub ProcessData()as below
#file name One.pl use Two; package MyPerl; sub Main_Fn { Two::ProcessData(); } 1; #file name Two.pm package Two; sub ProcessData { } 1;
My question is here. Is there any way to call ProcessData without making the second file as perl module. Thanks,

Replies are listed 'Best First'.
Re: Calling subroutine in a package from other perl sub.
by zzspectrez (Hermit) on Jan 16, 2001 at 11:22 UTC

    Here is an example.

    test1.pl
    sub hello_world { print "Hello World!\n"; } 1;
    test2.pl
    #!/usr/bin/perl -w use strict; require 'test1.pl'; &hello_world;
    Output
    Hello World!

    I havent ever used this practice in my own code. I believe it is better to use as a module. Otherwise if Im not wrong, they will be in the same package and you are more likely to clobber your data in one file with data in the other file.

    You could ofcourse declare them in seperate packages. But then you might as well make it a module.

    zzSPECTREz

      It is a dangerous style to call functions with the
      &hello_world;
      notation because (as perlsub mentions) you get an implicit argument list passed that you probably did not intend.

      Also note that just calling something a module doesn't give you any protection if it doesn't use a package statement internally. When loading stuff from a file that doesn't use package you can always do it yourself by declaring a package and then using do to load it.

      # Loads a file into a package. sub load_lib { my $file = shift; my $pkg = shift; my $ret = eval "package $pkg; do '$file';"; if (not defined($ret)) { $@ and confess("Cannot parse '$file': $@"); $! and confess("Cannot load '$file': $!"); warn("Loading '$file' did not return a defined value"); } $ret; }
      Note that the semantics here are slightly more generous than Perl's requirements of a module. I only warn if you don't get a defined value back. But the reason that I do this is that if it doesn't return something defined I cannot tell the difference between a system error caused executing the file and a system error caused by trying to read the file.

      If that restriction bothers you, you can explicitly read the file's contents into a string with the package prepended and do your own eval of that. You may wish to look at the advice at the end of perlsyn on how to control the output of the error message though. (Put the package before the preprocessor message. There is a bug in 5.005 that causes a preprocessor message on the first line of an eval to be ignored. Besides which you need to do this to make the line-number you get be reasonable. :-)

      Note that none of these solutions will help much if you have a script that does an exit somewhere. There are ways to override that, but if you are getting to this kind of detail, you probably wanted to make stuff into a module. :-)

        I never quite understood why this is considered dangerous style. This is the old style of calling subroutines. If you do not predeclare you subs then you either have to &sub or sub() to not get a bareword error with use strict

        When you call a subroutine like &sub I understand that you are passing the value of @_ to the sub or the equivalent of sub(@_). If you call sub(), you instead are passing a null list. But exactly how is passing @_ dangerous if the sub is not using any passed variables?

        I think not knowing the following is more dangerous.

        #!/usr/bin/perl -w use strict; my @test = ("one","two","three"); my $x = "one"; my $y = "two"; my $z = "three"; test1($x,$y,$z); print "$x\n$y\n$z\n\n"; test2($x,$y,$z); print "$x\n$y\n$z\n"; test1(@test); print join "\n", @test, "\n"; test2(@test); print join "\n", @test, "\n"; sub test1 { my $x1 = shift; my $y1 = shift; my $z1 = shift; $_[0] = "test1: 1"; $_[1] = "test1: 2"; $_[2] = "test1: 3"; } sub test2 { my ($x2,$y2,$z2) = @_; $_[0] = "test2: 1"; $_[1] = "test2: 2"; $_[2] = "test2: 3"; }
        Output:
        one two three test2: 1 test2: 2 test2: 3 one two three test2: 1 test2: 2 test2: 3

        Someone mind explaining this? In more detail than " '@_' is a local array, but its elements are aliases for the actual scalar paramters." What really gets me is how useing shift changes the behavior! Why?

        zzSPECTREz

      and if you use do rather than require, you don't even need the first file to return true.

      But I agree with zzSPECTREz, it's probably best to make it a module and import whatever you need.

Re: Calling subroutine in a package from other perl sub.
by chipmunk (Parson) on Jan 16, 2001 at 19:10 UTC
    Given Two.pl as written, One.pl would look like this:
    package MyPerl; sub Main_Fn { Second_Pak::ProcessData(); } 1;
    You can access variables and subroutines in another package simply by prepending the package name.

    Alternatively, you could switch into the other package inside the block:

    package MyPerl; sub Main_Fn { package Second_Pak; ProcessData(); } 1;
    That can be useful if you want to access a bunch of stuff from the other package.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://52182]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found