Training, Open Source computer languages

This is page http://www.wellho.net/forum/Perl-Programming/Text-str ... uotes.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

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