Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does perl use Cwd qw(); mean?

Tags:

perl

What does

use Cwd qw(); 

mean in perl?

I know qw gives quotes to each elements inside () But what about this case?

like image 976
in His Steps Avatar asked Sep 26 '13 22:09

in His Steps


2 Answers

The qw() is a fancy way of writing an empty list. Doing () would 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 call the package's import method (for instance, to stop your namespace from being altered), explicitly supply the empty list

E.g. by default, the Cwd module exports getcwd. This doesn't happen if we give it the 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.

like image 60
amon Avatar answered Nov 16 '22 13:11

amon


I believe that since qw() after use Module is for importing subroutines, when left empty it simply loads the module but doesn't import any subroutines into the namespace.

For example this throws an error since 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 was looking for..!

like image 4
psxls Avatar answered Nov 16 '22 12:11

psxls