Perl Strings

Strings

Perl strings are defined by using single or double quotes. When single quotes are used, strings appear as they are written. Double quoting strings forces string interpolation. String interpolation in Perl is the processing of the string, by converting variables and special characters to their prospective values.

If you want to use string delimiters inside the string, all you need to do is to escape it using backslashes.

If you need to use too many escape characters, you can simply use quoting and double quoting operators q and qq. q operator is the same as using single quotes (except it escapes single quotes within a string), there is no interpolation. A double quoting operator is the same as using double quotes (except it escapes double quotes within a string) and the string is interpolated.

Sometimes we might have a paragraph long string in our programs, to make it look nicer, we can use Perl’s built-in heredoc syntax, which allows us to create a block of strings.

Perl interprets this string using the tag name we choose after << sign, in this case, we chose HELPTEXT as our delimiter string. If you use variables in this syntax, resulting string will be interpolated.

Encoding

Internally, Perl treats all characters as 8-bit numbers. However, outside data coming from files, over the network or other applications can be in a different encoding. To overcome these issues, Perl Core module has built-in features encoding and decoding features.

Best practice is to always encode and decode data according to your data encoding.

Leave a Reply

Your email address will not be published. Required fields are marked *