Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a Perl module with empty parentheses

Tags:

perl

I'm trying to learn Perl and understand something about use and modules.

(Assume use strict; use warnings;)

I understand that use File::Find; loads all the subroutines of the module.

I understand that use File::Find qw(find); loads only the find subroutine of the module (although other subroutines my be used via File::Find::finddepth).

So what does File::Find (); do? Specifically, why the empty parens?

like image 641
steve-er-rino Avatar asked Nov 15 '12 15:11

steve-er-rino


2 Answers

tl;dr : It says not to export anything instead of the default.

long version:

File::Find has our @EXPORT = qw(find finddepth);, so those subs are exported by default. If we just use the module and then try to call find it errors because I didn't pass it the right arguments to find but find does exist.

quentin@workstation:~ # perl
use File::Find;
find();

no &wanted subroutine given at /Users/david/perl5/perlbrew/perls/perl-5.16.1/lib/5.16.1/File/Find.pm line 1064.

Passing a list in the use statement overrides the defaults and exports only the subs that you ask for. An empty list means that none will be exported and it will error because find doesn't exist. Such:

quentin@workstation:~ # perl
use File::Find ();
find();

Undefined subroutine &main::find called at - line 2.
like image 129
Quentin Avatar answered Nov 16 '22 18:11

Quentin


So what does File::Find (); do? Specifically, why the empty parens?

In short, you require-ing this module and calling File::Find::import to import functions (like find and finddepth in your example). So empty brackets mean you dont want import anything, and implicitly forbid to import any default symbols.

like image 1
PSIAlt Avatar answered Nov 16 '22 17:11

PSIAlt