tags:

views:

109

answers:

4

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

+1  A: 

Use the require and/or use functions.

AJ
+2  A: 

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
You can also turn your module as modulinos, a perl module that can be called as a script - See this link for more infohttp://www252.pair.com/comdog/mastering_perl/Chapters/18.modulinos.html
ccheneson
@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
A: 

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
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
@Evan Carroll: I guess you are wrong: This example actually works. You don't need package qualification `B::` here.
Curd
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
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
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
+3  A: 

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.

Evan Carroll