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

I am working on TeamSIte software, which has its own proprietory Perl modules

When I have to use a subroutine from TeamSite::Config module, I have to use handle for that. I have to declare handle as "$handle=> TeamSite::Config -> new();"
But when I have to use "copy" subroutine from File::Copy, I can use it directly.

Why is that ? Why do I not need to use handle to use copy subroutine ?
  • Comment on Use of Handle to use subroutines in a module

Replies are listed 'Best First'.
Re: Use of Handle to use subroutines in a module
by davido (Cardinal) on Aug 08, 2011 at 06:25 UTC

    Your syntax is wrong on the first example. It's not a => operator, it's my $handle = TeamSite::Config->new();

    And that is one of the most common ways to instantiate an object using Perl's object system. The second example you gave, File::Copy, is not an object oriented module. Instead, it internally invokes the Exporter module to export the copy() subroutine into your main package namespace so that you can invoke it almost as if it were a built-in function.

    So the first is an example of an Object Oriented module, and the second is a Module with a functional interface.

    I would say to pick up a copy of Intermediate Perl, published by O'Reilly. It covers modules, and an introduction to Object Oriented programming. But it may be too big a bite to chew on if you're really new to Perl. If that's the case, start with Learning Perl (the Llama book), and then move on to Intermediate Perl.

    There is also perlmod, and perltoot; Perl documentation that is free and explains modules and OO programming with Perl.


    Dave

      How do I know which module is Object Oriented Perl module and which one is not ?

        You type: perldoc Module::Name, and look at the SYNOPSIS section in the documentation. If you see an object being instantiated it's an OO module. If you see bare functions being used, it's not OO.... unless you have one of those modules that does both, such as CGI. For those, you'll have to actually read a little further in the documentation.


        Dave

Re: Use of Handle to use subroutines in a module
by kcott (Archbishop) on Aug 08, 2011 at 07:59 UTC

    The short answer is that TeamSite::Config is an object-oriented module whereas File::Copy is a functional module.

    With your proprietory module, $handle becomes an object of class TeamSite::Config; you then call methods like this: $handle->method_name().

    Here's some documentation that may be of help: search for Perl OO on the perl manpage. Update: also perlobj - Perl objects.

    The details regarding the File::Copy side of things can be found in the documentation for the use function and the Exporter module. Here's a few notes regarding your specific case - this is an oversimplification so do read the doco for full details.

    Some modules export functions automatically. If your code is written as:

    use File::Copy;

    Taking a quick peek at File::Copy's source code:

    @EXPORT = qw(copy move); @EXPORT_OK = qw(cp mv);

    You'll find move and copy are exported automatically (@EXPORT); while cp and mv are optional (@EXPORT_OK). You could choose to use the abbreviated names with: use File::Copy qw{cp mv}; or decide to disallow any exports with: use File::Copy ();.

    Documentation for @EXPORT, @EXPORT_OK and %EXPORT_TAGS is often lacking; I typically check the source rather than rely on what the documentation is telling me.

    Finally, just because TeamSite::Config is an object-oriented module does not mean it cannot also export functions or data. Check your in-house doco.

    -- Ken