Why should the function name be indicated in the usage statement?

In perlsometimes it is necessary to specify the function name in the instruction use.

For instance:

use Data::DPath ('dpath');

will work but

use Data::DPath;

will not be.

Other modules do not need function names, for example:

use WWW::Mechanize;

Why?

+4
source share
2 answers

Each module selects which functions it exports by default. Some people prefer to export no default features at all, you should ask for them. There are several good reasons for this, and one is bad.

If you are a class like WWW :: Mechanize , you do not need to export any functions. All this is a class or object method. my $mech = WWW::Mechanize->new.

, strict, , .

. Test::Deep, ...

array_each arrayelementsonly arraylength arraylengthonly bag blessed bool cmp_bag cmp_deeply cmp_methods cmp_set code eq_deeply hash hash_each hashkeys hashkeysonly Isa isa listmethods noclass none noneof num obj_isa re reftype regexpmatches regexponly regexpref regexprefonly scalarrefonly scalref set small under subbag subhashof subset of superbagof superhashof supersetof useclass

, . , .

$ cat ~/tmp/test.plx
use Test::Deep;
use List::Util qw(all);

$ perl -w ~/tmp/test.plx
Subroutine main::all redefined at /Users/schwern/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2/Exporter.pm line 66.
 at /Users/schwern/tmp/test.plx line 2.
Prototype mismatch: sub main::all: none vs (&@) at /Users/schwern/perl5/perlbrew/perls/perl-5.20.2/lib/5.20.2/Exporter.pm line 66.
 at /Users/schwern/tmp/test.plx line 2.

. , ...

!

!

. , @EXPORT_OK @EXPORT , .

, . Data::DPath - . , dpath(), . .

use Some::Module ();.

+9

, , , , script . :

# in some script
use SomeModule;
# ...
SomeModule::some_function(...);

use SomeModule ('some_function');
# ...
some_function(...);

, - , , , , my $obj = SomeModule->new(), .

EXPORT_OK, , , " ", "", EXPORT .

@EXPORT. Exporter docs .

MCVE , Funcs.pm, EXPORT EXPORT_OK. , package Funcs; , @JonathanLeffler . , - . Perl , TMTOWTDI , / , .

. - :

use WWW::Mechanize;
my $mech = new WWW::Mechanize;
$mech->get("http://www.google.com");

WWW::Mechanize , get, . , . , , . .

+1

Source: https://habr.com/ru/post/1650733/


All Articles