What perl uses Cwd qw (); I mean?

What is he doing

use Cwd qw(); 

mean in perl?

I know that qw gives quotes for each element inside (), but what about this case?

+6
source share
3 answers

qw() is a fancy way of writing an empty list. Executing () will be two characters shorter.

Passing an empty list to use is important to avoid importing. From the use documentation :

If you do not want to invoke a package import method (for example, to stop a namespace change), specify an empty list

eg. By default, the Cwd module exports getcwd . This does not happen if we give it an empty list:

 use strict; use Cwd; print "I am here: ", getcwd, "\n"; 

works but

 use strict; use Cwd (); print "I am here: ", getcwd, "\n"; 

aborts compilation with Bareword "getcwd" not allowed while "strict subs" in use .

+16
source

I believe that since qw() after use Module designed to import routines when it is left empty, it just loads the module, but does not import any routines into the namespace.

For example, this causes an error because getcwd is not imported:

 #!/usr/bin/perl use warnings; use strict; use Cwd qw(); my $dir=getcwd; 

But I'm not sure if this is the answer you were looking for ..!

+4
source

Usually it "imports" commands, so you do not need to create an object and call a function on them.

Example (from perlmonks):

  #without qw use CGI; my $q = CGI->new(); my $x = $q->param('x'); #with qw use CGI qw/:standard/; my $x = param("x") 

Source: http://www.perlmonks.org/?node_id=1701

Most modules have import groups such as: all or: standard.

-2
source

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


All Articles