Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: Exporter/@ISA confusion

by qhen (Acolyte)
on Jun 10, 2014 at 08:55 UTC ( [id://1089399]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Exporter/@ISA confusion
in thread Exporter/@ISA confusion

Thanks - that's what I needed to know. I've been trying to stick to the "modern" Perl way (ie, following recommendations by chromatic, conway, et al), and also trying to keep things simple and clean without having to resort to Moose, etc.

I've been using Exporter/@ISA and always wondered whether it was really needed as of Perl 5.8 (admittedly I have not spent the time to study the matter; time I often don't have) - the more syntactic cruft I can cut out, the better. I sometimes look at the relative elegance of Python and wish the same were true for Perl (which I love).

So, bottom line, I can simply exclude use Exporter; our @ISA = qw(Exporter); when creating a new class and/or inheriting from an existing, but still use:

our @EXPORT = qw(new serve); our @EXPORT_OK = qw(foo);
to allow optional/selective exporting of methods.

UPDATE:

Someone else commented:

You need to use use Exporter; in your module to get the @EXPORT array into the calling script/module...

So, in order to use @EXPORT/@EXPORT_OK, I still need to use Exporter.

Replies are listed 'Best First'.
Re^4: Exporter/@ISA confusion
by Corion (Patriarch) on Jun 10, 2014 at 09:17 UTC

    Yes, if you want to export subroutines, you need to use Exporter. The most convenient way to do so is to use

    use Exporter 'import';

    ... which does not need to modify @ISA at all, because all Exporter does is supply a subroutine named import.

    But in most cases, object methods do not need to be exported at all. So neither new nor serve should be exported, because they will always be called on an object (or a class).

      ok, that's probably what I needed to hear.

      I just confirmed this with a test:

      package Test::Class; use strict; use warnings; sub new { my ($className, $config) = @_; my $self = {}; bless $self, $className; $self->{config} = \%{$config}; return $self; } sub doSomething { my ($self, $what) = @_; print $self->{config}->{$what} . "\n";; } 1;

      and the caller:

      #!/usr/bin/env perl use strict; use warnings; use lib '.'; use Test::Class; my $t = Test::Class->new( { name => 'bob', } ); $t->doSomething('name');

      ...no need for noisy Exporter/@ISA nonsense. Nice and clean.

      /me raises tankard!

Re^4: Exporter/@ISA confusion
by perlfan (Vicar) on Jun 10, 2014 at 12:50 UTC
    This is where TIMTOWTDI. I prefer not exporting anything in my own modules. This forces the user of your module to use the full namespace at least once. Some module authors and users prefer the use Module qw(sub1 sub2) type of idiom, and while I don't mind it as a module user I don't like to enable main:: namespace pollution by providing that as something in my own modules.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2024-04-18 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found