Perl 101: Things Every Perl Programmer Should Know.

Strings

Embed strings in other strings with interpolation

Double-quoted strings can interpolate other variables inside.

    my $name = "Inigo Montoya";
    my $relative = "father";

    print "My name is $name, you killed my $relative";

Non-interpolating strings

If you don't want interpolation, use single-quotes:

    print 'You may have won $1,000,000';

Or you can escape the special characters (sigils):

    print "You may have won \$1,000,000";

Be careful with email addresses in strings

This email address won't be what you want it to be:

    my $email = "andy@foo.com";
    print $email;
    # Prints "andy.com"

The problem is that @foo is interpolated as an array. This problem is obvious if you have use warnings turned on:

    $ perl foo.pl
    Possible unintended interpolation of @foo in string at foo line 1.
    andy.com

The solution is either to use non-interpolating quotes:

    my $email = 'andy@foo.com';
    my $email = q{andy@foo.com};

or escape the @

    my $email = "andy\@foo.com";

A good color-coding editor will help you prevent this problem in the first place.

Use length() to get the length of a string

    my $str = "Chicago Perl Mongers";
    print length( $str ); # 20

Use substr() to extract strings

substr() does all kinds of cool string extraction.

    my $x = "Chicago Perl Mongers";

    print substr( $x, 0, 4 );   # Chic

    print substr( $x, 13 );     # Mongers

    print substr( $x, -4 );     # gers

Don't worry (too much) about strings vs. numbers

Unlike other languages, Perl doesn't know a "string" from a "number". It will do its best to DTRT.

    my $phone = "312-588-2300";

    my $exchange = substr( $phone, 4, 3 ); # 588
    print sqrt( $exchange ); # 24.2487113059643

Increment non-numeric strings with the ++ operator

You can increment a string with ++. The string "abc" incremented becomes "abd".

    $ cat foo.pl
    $a = 'abc'; $a = $a + 1;
    $b = 'abc'; $b += 1;
    $c = 'abc'; $c++;

    print join ", ", ( $a, $b, $c );

    $ perl -l foo.pl
    1, 1, abd

Note that you must use the ++ operator. In the other two cases above, the string "abc" is converted to 0 and then incremented.

Create long strings with the '' operators

You can create long strings with the '' operators

Create long strings with heredocs

Heredocs are

  • Allows unbroken text until the next marker
  • Interpolated unless marker is in single quotes
    my $page = <<HERE;
    <html>
        <head><title>$title</title></head>
        <body>This is a page.</body>
    </html>
    HERE

XXX Discuss dangers of heredocs.


Want to contribute?

Submit a PR to github.com/petdance/perl101


Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.