Using qw (: const) using the operator

Sorry for the very simple question, but I am starting a level in Perl and cannot find a suitable explanation for SO (or anywhere else!) For this question. I understand that I can write some examples and try to decipher it, but I really can use some knowledge from experts.

I am looking at code in which the developer has libraries using:

use libExample qw(:const) 

Now I understand that this means finding constants from libExample, but I would really like to know how this works.

  • Why can't I just say: use libExample qw(const) (Trying to understand relevance :

  • Is there something we can / should write in libExample.pm so that other developers use this library to mention such parameters instead of const.

Thanks!

+6
source share
2 answers
 use libExample qw(:const) 

selects all the names in the anonymous array $EXPORT_TAGS{const} and imports them into the current namespace.

While

 use libExample qw(const) 

will select const and import it in the current namespace.

There are other options:

 [!]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 see the Exporter documentation for more details on this topic.

+6
source

The syntax use Foo qw(:const) uses the EXPORT_TAGS function in Exporter .

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

 package Foo; use Exporter; our @EXPORT = qw( frobnicate ); sub frobnicate { ... } 

or when they are asked.

 OUR @EXPORT_OK = qw( frobnicate barnicate ); sub barnicate { ... } 

But you can also tell him to group things together, so your library user 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 would have to do this:

 use Foo qw(monday tuesday wednesday thursday friday); 

This one is a long line. Instead, it would be very useful if they 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) ] ); # ... 

Then we can use it with one tag instead of five function names:

 use Foo qw(:working_days); 

Note that this is equivalent to:

 use Foo ':working_days'; 
+8
source

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


All Articles