Perl

Perl is an dynamically typed programming language. It was initially designed as a language for system administration, but has since grown to provide a wide range of functionality in many different areas of software development and computer science.

Perl has imperative, functional and object oriented features, including both lexical scoping and dynamic scoping (although the latter isn't used much). Perl source code is executed by the Perl interpreter which internally compiles the code to bytecode for better performance.

Why would I learn this language?

Perl is a flexible dynamic programming language with many features. It is a great tool for system administration, web development and software testing.

Perl code is very succinct and can be written very quickly, while still having good performance.

Starting Points

Perl Beginners' Site
A set of resources aimed at beginners interested in learning about Perl.
Learning Perl
Resources and references for learning Perl.
Beginning Perl
A beginner guide to install program using Perl.

Example Code

Here is an example of the Fizz Buzz problem:

for my $i ( 1 .. 101 ) {
    if ( $i % 3 == 0 && $i % 5 == 0 ) { print 'fizzbuzz' }
    elsif ( $i % 3 == 0 ) { print 'fizz' }
    elsif ( $i % 5 == 0 ) { print 'buzz' }
    else { print $i }
    print "\n";
}

Here is an example of the 100 doors problem:

#!/usr/bin/perl

use strict;
use warnings;

sub doors
{
    # doors accept the number of doors as an argument.
    my ($num_doors) = @_;
    # All the doors are closed by default.
    my @is_open = ((0) x $num_doors);

    # Loop over the passes of all the doors.
    for my $pass (0 .. ($num_doors-1))
    {
        my $door = $pass;
        # Loop over all the doors in the pass.
        while ($door < $num_doors)
        {
            # Flip the door's state.
            $is_open[$door] = !$is_open[$door];    
        }
        continue
        {
            # Advance the door number.
            $door += $pass+1;
        }
    }

    # Print the status of all the doors.
    foreach my $door (0 .. $#is_open)
    {
        printf("Door #%d is %s.\n",
            $door+1, ($is_open[$door] ? "open" : "closed")
        );
    }

    # Return false so programmers won't depend on our return value.
    # See Perl Best Practices .
    return;
}

doors(100);

Further Reading

comments powered by Disqus