CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Meaning of -w and qw



Oct 27, 2012, 6:57 AM

Post #1 of 4 (2988 views)
Meaning of -w and qw Can't Post

Hi all,

I have a basic question! Please help me understand.

What is the purpose of -w switch and qw? And what would be the difference if we dont include in the program?


Veteran / Moderator

Oct 27, 2012, 7:25 AM

Post #2 of 4 (2986 views)
Re: [PerlUser12] Meaning of -w and qw [In reply to] Can't Post

The -w switch enables warnings. It is described in perlrun, which is accessed via the perldoc command.

c:\>perldoc perlrun 
perlrun - how to execute the Perl interpreter

perl [ -sTtuUWX ] [ -hv ] [ -V[:*configvar*] ]
[ -cw ] [ -d[t][:*debugger*] ] [ -D[*number/list*] ]
[ -pna ] [ -F*pattern* ] [ -l[*octal*] ] [ -0[*octal/hexadecimal*] ]
[ -I*dir* ] [ -m[-]*module* ] [ -M[-]*'module...'* ] [ -f ]
[ -C [*number/list*] ] [ -S ] [ -x[*dir*] ] [ -i[*extension*] ]
[ [-e|-E] *'command'* ] [ -- ] [ *programfile* ] [ *argument* ]...

The normal way to run a Perl program is by making it directly
executable, or else by passing the name of the source file as an
argument on the command line. (An interactive Perl environment is also
possible--see perldebug for details on how to do that.) Upon startup,
Perl looks for your program in one of the following places:


or online at:
Quoted from that doc:


prints warnings about dubious constructs, such as variable names mentioned only once and scalar variables used before being set; redefined subroutines; references to undefined filehandles; filehandles opened read-only that you are attempting to write on; values used as a number that don't look like numbers; using an array as though it were a scalar; if your subroutines recurse more than 100 deep; and innumerable other things.

This switch really just enables the global $^W variable; normally, the lexically scoped use warnings pragma is preferred. You can disable or promote into fatal errors specific warnings using __WARN__ hooks, as described in perlvar and warn. See also perldiag and perltrap. A fine-grained warning facility is also available if you want to manipulate entire classes of warnings; see warnings or perllexwarn.

As mentioned in the doc, it's better to use the warnings pragma instead of the -w switch. You should also be using the strict pragma, so every perl script you write should have these 2 lines after the shebang line.

use strict; 
use warnings;

Those 2 pragmas will point out lots of common mistakes in your code, which aides in writing better quality code.

qw is one of the quote operators and is covered in perldoc perlop under the "Quote and Quote-like Operators " section.

One of the ways it can be used is when assigning values to an array.

my @array = qw( one two three );

(This post was edited by FishMonger on Oct 27, 2012, 7:28 AM)

Veteran / Moderator

Oct 27, 2012, 7:34 AM

Post #3 of 4 (2983 views)
Re: [PerlUser12] Meaning of -w and qw [In reply to] Can't Post


The -w enables warnings, which is a very good thing to do because it point you to errors in your program, except that this is a slightly outdated (and somewhat deprecated) way of enabling the warnings. Nowodays, the best way to do it is to use the following pragma:

use warnings;

The qw (quote words) statement is synctatic sugar to define an array of strings.

@array = qw/foo bar . . ./;


@array = qw(foo bar . . .);

is the same as

@array = (’foo’, ’bar’,. . .).

Basically, you avoid having to type the single quotes and the commas. There is nothing more to it.

EDIT: Fishmonger was faster than me. ;-)

(This post was edited by Laurent_R on Oct 27, 2012, 7:37 AM)


Oct 27, 2012, 11:21 AM

Post #4 of 4 (2976 views)
Re: [Laurent_R] Meaning of -w and qw [In reply to] Can't Post

Thanks to FishMonger and Laurent_R. Your explanations with sample codes and links is very helpful :)


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives