For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > May 2007 > Module question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Module question
Robert Hicks

2007-05-17, 9:59 pm

Is @EXPORT_OK for those items you want to import by "name" and
%EXPORT_TAGS for those items you want to group like :all or :select?

What is @EXPORT used for then if that is the case above?

Robert
Jeff Pang

2007-05-17, 9:59 pm

Robert Hicks 写道:
> Is @EXPORT_OK for those items you want to import by "name" and
> %EXPORT_TAGS for those items you want to group like :all or :select?
>


Almost right.

> What is @EXPORT used for then if that is the case above?
>


@EXPORT was used for exporting symbol (methods or variables) by default.
When you say,
use base 'Exporter';
our @EXPORT = qw/&mysub/;
...
then mysub() would be exported into caller's name space defaultly.

--
http://home.arcor.de/jeffpang/
Paul Lalli

2007-05-17, 9:59 pm

On May 17, 11:05 am, sigz...@gmail.com (Robert Hicks) wrote:
> Is @EXPORT_OK for those items you want to import by "name" and
> %EXPORT_TAGS for those items you want to group like :all or :select?
>
> What is @EXPORT used for then if that is the case above?


@EXPORT is things that are exported by default when the user of your
module does not give any import list.

use Foo;
would import into the current namespace everything contained in
@EXPORT.

@EXPORT_OK is things that the user of your module is allowed to
import, but are not imported by default. (Anything that's in @EXPORT
also behaves as though it's also in @EXPORT_OK)

use Foo qw/bar baz/;
would import into the current namespace 'bar' and 'baz' if and only if
'bar' and 'baz' are in at least one of @EXPORT or @EXPORT_OK. If not,
Perl would error out with "'baz' is not exported by Foo".

%EXPORT_TAGS is for grouping things that can be exported (ie, are in
@EXPORT and/or @EXPORT_OK).

use Foo ":group1";
would import into the current namespace all items that are in
@{$EXPORT_TAGS{group1}} (provided that each of those items is also in
@EXPORT and/or @EXPORT_OK).

Paul Lalli

David Moreno Garza

2007-05-24, 9:58 pm

Jeff Pang wrote:
> @EXPORT was used for exporting symbol (methods or variables) by default.
> When you say,
> use base 'Exporter';
> our @EXPORT = qw/&mysub/;
> ..
> then mysub() would be exported into caller's name space defaultly.


Would that also work using:

our @EXPORT = qw/mysub/; ?

Or is the Perl 4 way to call subroutines also needed?

--
David Moreno Garza <damog@ciencias.unam.mx> | http://www.damog.net/
<URL:http://pub.tsn.dk/how-to-quote.php>

Jeff Pang

2007-05-24, 9:58 pm

David Moreno Garza 写道:
> Jeff Pang wrote:
>
> Would that also work using:
>
> our @EXPORT = qw/mysub/; ?
>


'&' is not needed here. qw/mysub/ works fine.

--
http://home.arcor.de/jeffpang/
Paul Lalli

2007-05-24, 9:58 pm

On May 24, 12:44 pm, d...@ciencias.unam.mx (David Moreno Garza) wrote:
> Jeff Pang wrote:
>
> Would that also work using:
>
> our @EXPORT = qw/mysub/; ?
>
> Or is the Perl 4 way to call subroutines also needed?


That's not calling a sub. The & in this case is used because it's the
real name of the sub. Just as all scalars start with $, arrays start
with @, and hashes start with %, all subroutines start with &. It's
just that the & is usually not needed. This is one of those times
it's not. Completely optional.

Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com