Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the use of qw(:ALL) in perl

Tags:

perl

I know about qw() function but I have seen at many places the use of qw(:ALL).

What is the advantage of using this and where can I find examples of the use of qw(:ALL) ?

like image 796
user3540276 Avatar asked Apr 25 '14 11:04

user3540276


2 Answers

qw(:ALL) means exactly the same as (":ALL"). It's simply a one item list consisting of a single four character string — colon, capital A, capital L, capital L. Nothing exciting.

A lot of Perl modules offer functions that can be imported into your namespace. For example, the Carp module offers functions like croak and confess. Many such modules allow you to specify a list of functions you want to import:

use Carp "confess", "croak", "cluck";
use Carp qw( confess croak cluck );      # this means the same, but looks cleaner

Some modules allow you to specify something like ":ALL" or ":all" or "-all" in that list to indicate you wish to import all the functions they have to offer. File::Spec::Functions is an example of a module that does this:

use File::Spec::Functions ":ALL";
use File::Spec::Functions qw( :ALL );    # means the same again

The reason for the colon is to make it clearer that you're not requesting to import a function called ALL (such a function could exist - indeed, List::MoreUtils provides a function called all). There's no technical reason for it; just convention. It would be perfectly possible to write a module so that:

use Foo::Bar "ALL";

... imported all the functions from Foo::Bar. But people don't do that because tradition.

like image 141
tobyink Avatar answered Oct 20 '22 20:10

tobyink


Some modules use :ALL as a parameter to their import method to export all possible functions. See File::Spec::Functions for an example.

use File::Spec::Functions qw(:ALL);
print tmpdir();
like image 40
choroba Avatar answered Oct 20 '22 20:10

choroba