Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

perl beginner question

by Anonymous Monk
on Jun 02, 2009 at 10:55 UTC ( [id://767550]=perlquestion: print w/replies, xml ) Need Help??

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

How to use a user defined function inside a .pm file in other .pl file with in same directory as well as different directory?

Replies are listed 'Best First'.
Re: perl beginner question
by vinoth.ree (Monsignor) on Jun 02, 2009 at 11:19 UTC

    Within the same directory

    Create .pm file and use use .pm and create object for that class and using that object access the functions in .pm into the .pl file, it is oops method!

    See simple module tutorial in perlmonks

    Simple Module Tutorial

    See

    Module Creation

    Other directory

    unshift the .pm directory path to the @INC array.

    See Objects in Perl

    Vinoth,G

      You don't need to create an object or class to import functions from another file. See require, or even do.

      Thanks vinothji..
Re: perl beginner question
by targetsmart (Curate) on Jun 02, 2009 at 11:22 UTC
    you have to use use modulename inside your .pl file.
    if the .pm file is in the other directory, mention that path in the @INC variable, or use 'use lib' pragma, or -I option to the interpreter.
    you can call that routine like classname::routinename as plain
    with OO you can call classname->routinename
    see my earlier post Re^3: A Class inside a Perl script?

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
      thanks vivek

        Let's say you have a package: test.pm

        package test; sub mysub { return 'babayaga'; } 1;

        Then in your .pl file

        #!/usr/bin/perl use strict; use test; print test::mysub();
        If you want to use directly mysub() read this one here: Exporter
Re: perl beginner question
by eye (Chaplain) on Jun 02, 2009 at 14:24 UTC
    Several people have already made good suggestions, but this is one of those things with lots of answers.

    When the .pm file will not be in the same directory as the main script, I'm fond of using the lib pragma.

    When the .pm file will be in the same directory as the main script, it is sometimes easier to skip the package declaration in the module. This will place everything from the module in the "main" package namespace. This is not a good idea for code that will be used in a variety of projects, but may be useful when intended for a group of related scripts that share code.

      When the .pm file will not be in the same directory as the main script, I'm fond of using the lib pragma.

      Even when it is in the same directory.

      $ dir test/ total 8 -rw------- 1 eric users 21 2009-06-02 10:53 Module.pm -rwx------ 1 eric users 29 2009-06-02 10:54 script.pl* $ cat test/script.pl #!/usr/bin/perl use Module; $ cat test/Module.pm package Module; 1; $ test/script.pl Can't locate Module.pm in @INC (@INC contains: /home/eric/lib/perl5/i4 +86-linux-gnu-thread-multi /home/eric/lib/perl5 /etc/perl /usr/local/l +ib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/p +erl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl . +) at test/script.pl line 2. BEGIN failed--compilation aborted at test/script.pl line 2.

      It's bad to assume the working directory is set to the directory in which the script resides. Fix:

      use Cwd qw( realpath ); use File::Basename qw( dirname ); use lib dirname(realpath($0));

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://767550]
Approved by Corion
Front-paged by vinoth.ree
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-03-19 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found