Monday, December 05, 2005

 

Perl: Strings

There are several ways of quoting strings in perl, corresponding to the
three quote characters on the keyboard.
' (apostrophe)
The simplest quote, text placed between a pair of apostrophes is
interpreted literally - no variable expansion takes place.
To include an apostrophe in the string, you must escape it with a
backslash '\'

$instrument = 'saxophone';
$littleInstrument = 'soprano $instrument';
# the string value of $littleInstrument includes the
# text "$instrument" - probably an error in this case.

" (double quotes)
The double quote "interpolates" variables between the pair of quotes.

$instrument = "saxophone";
$littleInstrument = "soprano $instrument";
# the string value of $littleInstrument is "soprano saxophone"

To include an apostrophe in the string, you must escape it with a
backslash '\'.
` (backtick)
This quote performs as it does in the UNIX shells - the text inside the
backticks is executed as a separate process, and the standard output of
the command is returned as the value of the string.
Backticks perform variable interpolation, and to include a backtick in the
string, you must escape it with a backslash '\'.

$memberList = "/usr/people/conductor/roster";
$memberCount = `wc -l $memberList`;
# $memberCount is the number of members in the conductor's roster,
# assuming that each member is listed on a separate line.

--------------------------------------------------------------------------------

Example

# the sendmail binary.
$sendmail = "/usr/lib/sendmail";

# base of your httpd installation.
$basedir = '/www';
# log file
$logfile = "$basedir/etc/logs/$progname.log";

# today
$date = `date`;


Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?