Perl Postfix Control Constructs

Watch out! This tutorial is over 6 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

Control statements in Perl can also be written with the conditional following the statements (called “postfix”). This syntax functions (nearly) identically to the usual one you would expect.


statement if Boolean expression;
statement unless Boolean expression;
statement while Boolean expression;
statement until Boolean expression;
statement foreach list;

Here’s a working example:

#
# Perl postfix conditionals (which are actually expression operators).
# They follow what they control and don't use { }.

use strict;

# Read some lines.
my $wasquit = 0;
print "ctl2> ";
while(my $line = <STDIN>) {
    chomp $line;
    print "Jones\n" if $line eq 'smith';

    # Quitting is here.
    if($line eq 'stop') {
        print "That's better.\n" if $wasquit;
        last;
    }
    if($line eq 'quit') {
        $wasquit = 1;
        print "You must say stop if you want to quit.\n";
    } else {
        $wasquit = 0;
    }

    print "$line to you!\n" if $line;

    print "ctl2> " unless $line eq 'sssh';
}