11.3. What happened to the quotation marks?

Perl 4 programmers especially may be surprised to find a serious dearth of quotation marks around strings in perl 5 scripts such as in perl/Tk. The "rules have been relaxed" somewhat for the use of quotation marks. Basically it is OK to leave them out if the context of the string in question is unambiguous. However, it never hurts to leave them in and may help readability.

Here is Larry Wall's synopsis of the string situation:

Newsgroups:
comp.lang.perl.misc
Subject:
Re: To string or not to string...
In article <4e49fv$j0u@panix3.panix.com>,
Andy Finkenstadt <genie@panix.com> wrote:
! Back when I was learning perl (after receiving a review copy of
! learning perl, and buying the real perl book, each from ORA),
! I always got bit by when I needed to use "strings" and when
! I could get away with bare_words within braces for associative
! arrays.  (Yes, this is under 4.036 if it matters.)
! 
! the most typical example would be:
! 
! When must I use $assoc{"trailer"} and when can I get away with
! $assoc{trailer}?   Similarly, $ENV{CONTENT_LENGTH} versus
! $ENV{"CONTENT_LENGTH"}?  Unfortunately sometimes my strings
! end up being numbers in their own right, i.e.:  $message{"0"}
! or $msg=0; $message{$msg}.  Which is more appropriate,
! which are merely stylistic, and which are stricly perl5
! features now that I'm upgrading most of my installations
! of perl.
Perl 4 let you use a "bareword" for a string if it had no other interpretation. It would warn you under -w if you used a word consisting entirely of lower-case characters, since such a word might gain an interpretation someday as a keyword.

Perl 5 still works the same way, but with several twists.

  1. ) Since you can now call predeclared subroutines as though they were builtins, you have to worry about collisions with subroutine names too. However...
  2. ) You can completely disallow the default interpretation of barewords by saying "use strict subs", which requires any such bareword to be a predeclared subroutine. But...
  3. ) Overriding all that, Perl 5 (in recent versions) will FORCE string interpretation of any bare identifier used where a single hash subscript is expected, either within curlies or before a =>. (Those are the places you might usually want the old barewords anyway.)
The upshot of these rules is that you can write Perl 5 with much less punctuation than Perl 4, yet also with less ambiguity. If you so choose.

Larry

Tcl programmers should note that in Perl the single quotation marks '' act much as the curly brace {} enclosure does in Tcl (no escaping special characters $@\ etc.). Whereas the double quotation marks "" allow for substitution of $variables (the rules are a little different between Tcl and Perl however).

Note also that a frequently seen short hand in perl5/Tk scripts is the @list returned by qw():

    @list = qw(zoom schwartz bufigliano);
which is equivalent to:
    @list = split(' ','zoom schwartz bufigliano');
or more simply:
    @list = ('zoom','schwartz','bufigliano');
i.e. the qw/STRING/ @list is not equivalent to the quotation marks provided by q/STRING/, qq/STRING/, or qq(STRING)...

There are, ironically enough, situations in perl/Tk where one needs to use quotation marks as in the following by post by <a904209@pluto.tiuk.ti.com>:


 Paul Wickman wrote in article <4b4o0fINNlu8@CS.UTK.EDU>:
!
!    Why does the following statement work fine:
!
!$day->pack(-before => $year, -side => 'left');
!
!    But the below generates the given error:
!
!$day->pack(-after => $year, -side => 'left');
!
!Ambiguous use of after => resolved to "after" => at line 191.
!
Because there is a sub after in scope, probably imported from Tk via use Tk;.

There are two workrounds:

    use Tk qw(MainLoop exit ...); # just ones you use
or
    $day->pack('-after' => $year, -side => 'left');

Previous | Return to table of contents | Next