Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of qw(:const) with use statement

Tags:

perl

Sorry for a very basic question, but I'm beginner level at Perl and could not find a suitable explanation on SO (or anywhere else!) for this question. I understand I can write a few examples and try to decipher this but I can really use some knowledge from the experts.

I'm going through some code where the developer has sourced libraries using:

use libExample qw(:const)

Now from what I understand this means sourcing the constants from libExample but would really like to know how this works.

  1. Why can't I simply say: use libExample qw(const) (Trying to understand relevance of :)

  2. Is there something we can/should write in libExample.pm itself to make other developers utilizing this library to mention such options in place of const that is.

Thanks!

like image 520
Jagdish Rapata Avatar asked Oct 20 '16 12:10

Jagdish Rapata


2 Answers

The syntax use Foo qw(:const) is using the EXPORT_TAGS feature in Exporter.

When you set up your library module, you usually have a bunch of functions or class variables. Then you configure Exporter by telling it what to export by default

package Foo;
use Exporter;

our @EXPORT = qw( frobnicate );

sub frobnicate { ... }

or when they are asked for.

OUR @EXPORT_OK = qw( frobnicate barnicate );

sub barnicate { ... }

But you can also tell it to group things together, so the user of your library does not need to list all the methods. Consider this example.

package Foo;
use Exporter;

our @EXPORT_OK qw(monday tuesday wednesday thursday friday saturday sunday);

sub monday { ... }
sub tuesday { ... }
sub wednesday { ... }
sub thursday { ... }
sub friday { ... }
sub saturday { ... }
sub sunday { ... }

Now if I wanted all the working days, I'd have to do this:

use Foo qw(monday tuesday wednesday thursday friday);

That's one long line. Instead, it would be super useful if those could be grouped. Well, they can be. If you do this instead in your library:

package Foo;
use Exporter;

our %EXPORT_TAGS = ( 
    working_days => [ 
      qw(monday tuesday wednesday thursday friday)
    ],
    weekend_days => [
      qw(saturday sunday)
    ]
);

# ...

We can then use it with one tag instead of five function names:

use Foo qw(:working_days);

Note that this is equivalent:

use Foo ':working_days';
like image 159
simbabque Avatar answered Nov 12 '22 23:11

simbabque


use libExample qw(:const)

will pick all names in $EXPORT_TAGS{const} anonymous array and will import them to current namespace.

Whereas

use libExample qw(const)

will pick const and will import it in current namespace.

There are also other variants:

[!]name         This name only
[!]:DEFAULT     All names in @EXPORT
[!]:tag         All names in $EXPORT_TAGS{tag} anonymous array
[!]/pattern/    All names in @EXPORT and @EXPORT_OK which match

Please go through Exporter documentation for more details on the topic.

like image 20
Chankey Pathak Avatar answered Nov 13 '22 00:11

Chankey Pathak