[MUSIC PLAYING]
Today, we're talking about manipulating strings in Perl.
Dynamic languages often provide specialised types, operators,
and/or functions that make string manipulation tasks easier,
and Perl is no exception.
Strings in Perl are scalar values,
and we need to quote all strings that we use.
We can use single quotes or we can use double quotes.
There are also quote-like operators
that serve a similar purpose.
'q' is similar to single quotes, and 'qq' is similar to double quotes.
The quote-like operators allow us to use other delimeters--
like, braces here--
to make our code clearer.
So what's the difference between the single-quoted or 'q'-quoted
strings or these double-quoted or 'qq'-quoted strings?
Well, somewhat like the Bourne shell,
single-quoted or 'q'-quoted strings not interpolated.
But double-quoted strings or 'qq'-quoted strings are.
You'll need to escape, with backslashes,
the '$', '@', and '%' sigils, if you want them to
appear literally in an interpolated string.
Perl's interpolation rules are very friendly,
allowing us to subscript arrays or hashes inline.
But it's probably best to be sparing in doing so,
as this can make your code harder to read.
One curious gotcha is that single quotes
are a valid part of a variable name, which
can have unintended effects when interpolating.
If we want to concatenate strings,
we can use the dot operator.
Watch out for this, because many other languages choose
to overload the plus operator.
As with other binary operators in Perl,
I can also dot assign to concatenate onto an existing
variable.
In other languages, the same comparison operators
can compare numbers and strings.
In Perl, though, there are separate operators
to do a numeric comparison--
that is, to compare numeric values--
and lexicographic comparison-- that is,
to compare dictionary order of words.
If I compare two strings using the numeric equality operator,
I get a warning.
Because, instead, I should use the string equality operator,
'eq'.
And there are analogues for the whole range of comparison
operators--
our less than becomes 'lt'. Less equal, of course, becomes 'le'.
Greater than becomes 'gt'.
And greater equal becomes 'ge'.
There's also a general comparison operator--
the "rocket-ship" operator--
which returns 0 if the values are equal, less than 0
if the value is less, or greater than 0 if the value is more--
like 'strcmp' in C. Again, this only works on numeric values.
To compare strings, we'd use 'cmp'.
Perl lets us interact with the system,
which makes it invaluable for system administration tasks.
One useful feature is that I can run a command
and capture its output using backticks, or the 'qx' operator.
I could, for example, use 'date', which gives us the system's
idea of the date and time.
In backticks, this yields a string value,
which we can then manipulate further by other means.
Newlines in the program's output are left intact
in the string.
I've written a simple subroutine, 'showS',
which wraps quote delimiters around our string,
so we can see where that newline is.
I can use 'chomp' to remove that trailing newline.
It returns the number of characters it removed.
To extract a portion of the string,
I can use 'substr', which takes three arguments--
a string, an offset, and a length--
and extracts a substring from that zero-indexed offset
for that length of characters.
If the offset is negative, it counts backwards
from the end of the string.
If we don't specify a length at all,
it returns all the characters to the end of the string.
And if we specify a negative length,
it skips that many characters back
from the end of the string.
One of Perl's more curious features
is expression evaluation--
on-the-fly evaluation of strings as Perl.
'eval' is incredibly powerful and exceptionally dangerous,
as it can allow arbitrary code execution in your code.
Given Perl's capable built-in operations,
it's easy for a malicious attacker to wreak untold havoc.
So be careful.
Most of the Perl you write won't be at an interactive prompt,
either in this sort of read, evaluate, print loop--
or REPL--
or using Perl as a filter in the Bourne shell
environment-- but, rather, in standalone scripts.
Sometimes you'll be writing Perl to deal with injecting
particular text into some complex predefined template,
which you want to inline into your script--
if you're generating web pages, for example.
There's a few ways to do that.
For example, strings in Perl can span multiple lines.
However, that's not especially idiomatic Perl.
More common, is a mechanism called
a here-doc, which looks just like the Bourne shell.
I have a pair of less than signs and an identifier.
And everything from that identifier,
until that identifier appears again,
is captured with interpolation.
By default, that interpolation happens;
single quoting the identifier name
prevents that interpolation.
I can also save a here-doc value into a variable
and use it again later.
I can also mix this with other strings too.
So here's what our whole program looks like to run.
Lovely.
And for better characterised coverage of these topics,
check out the Perl Documentation Collection.
And don't forget to check out our video
on working with regular expressions in Perl.
[MUSIC PLAYING]