0

Possible Duplicate:
How do I include functions from another file in my Perl script?

I have a perl file suppose a.pl and I want to use a function from perl file b.pl. How do i do it

Community
  • 1
  • 1
manugupt1
  • 2,337
  • 6
  • 31
  • 39
  • 3
    See similar: http://stackoverflow.com/questions/1712016/how-do-i-include-functions-from-another-file-in-my-perl-script/1712036#1712036 – martin clayton Feb 02 '10 at 22:07
  • 3
    See also: http://stackoverflow.com/questions/2180554/in-perl-is-it-better-to-use-a-module-than-to-require-a-file – Ether Feb 02 '10 at 23:42

4 Answers4

3

Turn b.pl into a module

  1. Call it something descriptive like MyBModule (B is reserved by core).
  2. rename the file to the something .pm like MyBModule.pm.
  3. Add a package at the top, like package MyBModule;
  4. Set a true return code on the package by making the last line 1;

You don't have to do anything else if you want to use your package name when calling the sub.

use MyBModule;
use strict;
use warnings;
MyBModule::sub1();

If you don't want to qualify it with the package name, read on...

Use Exporter.pm

Now configure Exporter.

  1. Add the use Exporter; statement at the top of your module.
  2. add a line our @EXPORT_OK = qw(sub1 sub2);

After you're done your module should look something like this

package MyBModule;
use strict;
use warnings;
use Exporter;
our @EXPORT_OK = qw(sub1 sub2);

sub sub1 { ... }
sub sub2 { ... }

Edit the caller

  1. Make sure the library is in @INC, or the module in the current directory. If not append the directory to PERL5LIB.
  2. Add a line like use MyBModule qw(sub1 sub2);

Read perldoc Exporter for more information

Your script should look like this afterward:

use strict;
use warnings;
use MyModuleB qw( sub1 sub2 );

It really isn't that hard, it takes about 15 seconds after you get used to doing it.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
2

The best/conventional way is to keep all your functions in a Perl module file (.pm): See perlmod. This would require you to convert b.pl to a package. You would then access your module file (MyFuncs.pm) from a.pl with:

use MyFuncs;
toolic
  • 57,801
  • 17
  • 75
  • 117
  • 1
    You can also turn your module as modulinos, a perl module that can be called as a script - See this link for more info http://www252.pair.com/comdog/mastering_perl/Chapters/18.modulinos.html – ccheneson Feb 02 '10 at 22:13
  • @ccheneson See also http://stackoverflow.com/questions/1131304/in-perl-how-can-i-find-out-if-my-file-is-being-used-as-a-module-or-run-as-a-scri – Sinan Ünür Feb 02 '10 at 22:32
1

Use the require and/or use functions.

AJ.
  • 27,586
  • 18
  • 84
  • 94
-1

Use the 'use' keyword to use functions from another module.

Example:

File a.pl:

use b;
f_in_b();

File b.pm:

sub f_in_b()
{
  print "f_in_b\n";
}

1;

Important: File b.pm must have the final 1;

Curd
  • 12,169
  • 3
  • 35
  • 49
  • It isn't nearly so simple. Firstly, **never** use lowercase module names unless you're writing a pragma to change the behavoir of perl, secondly, B.pm in this example would have to have a configured `sub import {}`, this can be set up with `Exporter`, or `Sub::Install` or something, but the magic doesn't happen without that. You can still get to `f_in_b()` without it, but you have to use package-qualification, `use b; B::f_in_b();` – Evan Carroll Feb 02 '10 at 22:15
  • @Evan Carroll: I guess you are wrong: This example actually works. You don't need package qualification `B::` here. – Curd Feb 02 '10 at 22:31
  • 2
    This is something I forgot, reason being no sane person would want this. Technically what is happening is you're polluting `package main;` which is a bad thing. If you had `f_in_b()`, in the `a.pl`, the one in `b.pm` would be overwritten. – Evan Carroll Feb 02 '10 at 23:04
  • 1
    If you make a file called *.pm, it should conform to the standards of a module, unless your intent is to sabotage the code maintenance efforts of your employer or client. Your code sets up a false expectation that b.pm will respect the `main` namespace. You managed to trip up a skilled Perl programmer with an insane and ill-considered move. Please read perldoc perlmod -- http://perldoc.perl.org/perlmod.html. – daotoad Feb 03 '10 at 16:18
  • Yes, I know, that this example is not the official way to define a perl module. Neither did I claim that this is the case nor was it the question of manugupt1. His question was how to use a function from another perl file; nothing more. And this is probably the most minimalistic answer. I don't understand this excitement about it. – Curd Feb 03 '10 at 23:02