Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Use, Exporter, I'm Dizzy.

by ikegami (Patriarch)
on Feb 07, 2007 at 17:20 UTC ( [id://598824]=note: print w/replies, xml ) Need Help??


in reply to Use, Exporter, I'm Dizzy.

You have three major problems.

  • Perl comes with a module called test. You aren't loading the module you think you are loading until you added use lib '.';, which causes your module to be found before the one that comes with Perl.

  • use test (); means import nothing

    use test; means import everything the module exports by default. With module using Exporter, that would be the items listed in @EXPORT. In the case of your module, that means import nothing.

  • Modules loaded using require (including those loaded using use) must have a package statement for them to work correctly when loaded from two different namespaces (such as from the main script and from a module).

    You'll notice by adding the package statement that your module will need to use Exporter (or similar) to export testroutine to the caller.

#!/usr/bin/perl use warnings; use strict; #use MyTest; # Imports what's in the module's @EXPORT #use MyTest ( ); # Won't work. #use MyTest qw( ); # Another way of writing the same thing. use MyTest qw( testroutine ); # Imports testroutine and nothing else. my $output = testroutine(); print $output, "\n";
# MyTest.pm use strict; use warnings; package MyTest; BEGIN { our @EXPORT = qw( ); # Export nothing by default. our @EXPORT_OK = qw( testroutine ); require Exporter; *import = \&Exporter::import; } # Put other "use" statements here, if any. sub testroutine { return "This is a test"; } 1;

Replies are listed 'Best First'.
Re^2: Use, Exporter, I'm Dizzy.
by logie17 (Friar) on Feb 07, 2007 at 17:30 UTC
    Thank you, that answers all my questions. So if package is omitted from my module it just treats the subroutine as if it's in the Main:: namespace?
    Thanks
    s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

      Not quite. It uses the currently "active" namespace.

      package Foo::Bar; use MyTest;
      would use Foo::Bar.
Re^2: Use, Exporter, I'm Dizzy.
by caelifer (Scribe) on Feb 07, 2007 at 17:52 UTC
    Just a question to your answer - is there something wrong with this form?
    package MyTest; use strict; use warnings; use base qw(Exporter); our @EXPORT_OK = qw(testroutine); # Rest of the code here ...

    In other words, does BEGIN form have any advantage over use base?

    -BR
      • You pretend there's an is-a relationship where there isn't one.
      • That won't work if two modules include each other.

      Newer versions of Exporter recognize the first point and allow you to do

      package MyTest; use strict; use warnings; BEGIN { our @EXPORT_OK = qw( testroutine ); } use Exporter qw( import ); # Rest of the code here ...

      The BEGIN is there to address the second point.

        Thanks. Good insight!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found