Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Text strings - single or double quotes?

Posted by admin (Graham Ellis), 16 October 2002
In Perl, you'll get used to writing strings of text in single or double quotes:
       $abc = "This is some text";
       $def = 'This is some text';
and in this example both $abc and $def will end up containing exactly the same text.

However - the single quote tells Perl that you're specifying a literal string, but the double quote is, strictly speaking, and operator that translates the text within the quotes in what's known as double quotish context.  In this context:
       Scalar variable names that start with a $ are expanded
       List names that start with an @ are expanded, with a space (default) between each element of the list
       \ followed by a special character is a literal request for that special
       \ followed by a letter (\n \t etc) is a newline, tab, etc
       \c followed by a letter is a control code (e.g. \cL is conrol L)
       \u and \l mean make next character upper or lower case
       \U to \E forces upper case over an area of the string
       \x followed by 2 hex characters is a request for a specific character

As an alternative to double quotes, you can open your double quoted string with qq! and end it with !, and indeed you can replace the ! with almost any special character that you like.  You can use q! to ! for a single quoted string. Should you use ( { or [ as your open character, use ) } or ] as your close character. Examples:
       $abc = qq%There are $n in stock\n";
       $def = q(This is a single quoted string);

A further alternative, known as a here document, lets you write a whole block of text into a single or double quoted string:
       $ghi = <<"TAIL"
       This is some text to place in \$ghi, which
       will spread over several lines.  Today it
       is $dow and in on the menu tonight is
       @menustuff.  It's always hard to make up
       "good" test data for an example like this
       TAIL
       ;
The terminator in this case has been chosen to be TAIL - you can use any terminator you like, which must appear on a line on its own at the end of the text block.  The ; must NOT be on the same line as the terminator!




This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho