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)
?
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With