Ramanjaneyulu Gudipati
Ramanjaneyulu Gudipati

Reputation: 173

Can I use qq or q instead of qw in the following perl script?

The output for the following script would be the present date like " 26 Dec Mon"

#!/usr/local/bin/perl
@months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
@days = qw(Sun Mon Tue Wed Thu Fri Sat Sun);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
print "$mday $months[$mon] $days[$wday]\n";

Upvotes: 4

Views: 22548

Answers (2)

PerlDuck
PerlDuck

Reputation: 5720

No.

qw(a b c) is a shortcut for ('a', 'b', 'c'). It returns a list (of words, that's what the w stands for).

q() is simply another way to write single quotes, i.e. q(a b c) is identical to 'a b c'. It returns one string without interpolation.

qq() is a way to write double quotes, i.e. qq(a b c) is identical to "a b c". It returns one string with interpolation.

See http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators for details.

The q() and qq() syntax is sometimes used when the string to be quoted contains (lots of) single and/or double quotes and you want to avoid escaping them:

my $s1 = "This \"contains\" quotes - \" - and \"is\" no fun to \"type\"";

vs.

my $s2 = qq(This also "contains" quotes - " - but "is" easier to "type"); 

Upvotes: 13

ikegami
ikegami

Reputation: 385647

qw(...)

is functionally equivalent to

split(' ', q(...))

so it's obviously not equivalent to

q(...)

or

qq(...)

Upvotes: 3

Related Questions