Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Using only one subroutine from module file

by isha (Sexton)
on Jun 27, 2007 at 09:15 UTC ( [id://623562]=perlquestion: print w/replies, xml ) Need Help??

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

hi, I have a module names Sheets.pm, The module contains so many methods(subroutines). Now I only want to use the subroutine named "make_files" from the module file into my script. How can i use only one method from a module? I tried it with use Sheets; but this will allow me to use all the methods from the module.... I only want to use one method from the module..... Please reply me soon.
  • Comment on Using only one subroutine from module file

Replies are listed 'Best First'.
Re: Using only one subroutine from module file
by GrandFather (Saint) on Jun 27, 2007 at 09:36 UTC
    use Streets qw(); # Hide everything ... Streets::make_files (...); # Access sub explicitly

    Including files in the Tutorials section provides a little more detail and links to further documentation.


    DWIM is Perl's answer to Gödel
Re: Using only one subroutine from module file
by tirwhan (Abbot) on Jun 27, 2007 at 09:42 UTC

    For importing only a limited number of subroutines, add a list of the subroutine names to your use statement.

    use Sheets qw(make_files);

    See perldoc perlmod and perldoc Exporter for more on this.


    All dogma is stupid.
Re: Using only one subroutine from module file
by shmem (Chancellor) on Jun 27, 2007 at 10:15 UTC
    In addition to the good advice given above - if you don't want to load the entire Module of which you use only one function, consider using AutoLoader and AutoSplit.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Using only one subroutine from module file
by ferreira (Chaplain) on Jun 27, 2007 at 20:01 UTC

    First, it is important that you remember the difference between methods (sort of "subs called on an object or class") and subroutines. For methods, you just need to say

    require Sheets; Sheets->make_files;
    or invoke some kind of constructor (creating an object) and using a piece of code like this:
    require Sheets; Sheets->new->make_files;
    There is no need for doing more to have access to a method (no need for exporting, etc.)

    If the issue is with subroutines, the following may be valid. If you don't want to change Sheets.pm or is not under your control, the following may do the trick you want:

    # load Sheets require Sheets; # making it known to the current package # alias the function that matters *make_files = \&Sheets::make_files; # use it make_files() # call cleanly Sheets::make_files
    (and you can discard the alias as well and use directly as Sheets::make_files, which is not that bad if used in a limited scope).

    The suggestion of the paragraph above "works" independent of the exporter module used or the idiosyncrasies of the provided import function. Well, works if there is not too much magic behind the panes.

Re: Using only one subroutine from module file
by jesuashok (Curate) on Jun 27, 2007 at 09:39 UTC

    Warm welcome to isha,

    # file Test.pm package Test; use strict; use warnings; our (@EXPORT_OK); use base qw(Exporter); @EXPORT_OK = qw(make_file); sub make_file { print qq{Hello from the make_file sub\n}; } sub delete_file { print qq{Hello from the delete_file sub\n}; } 1;
    #main.pl #!/usr/bin/perl use Test qw(make_file); make_file();
    Example to call only one method from a module.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-26 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found