What is Perl and where it runs

What Perl is

Perl is an interpreted language created by Larry Wall in 1987. You write plain text, and the perl program runs it line by line with no compile step in between.

Perl was born as a report-processing tool on Unix, so text is its home ground: regular expressions are part of the syntax, not a library that you import. Files, pipes, processes and environment variables are equally close at hand.

print "Hello from Perl\n"; print 2 + 2, "\n"; # prints 4

perl demo.pl

Hello from Perl
4

Where Perl runs

Perl runs on Linux, UNIX systems such as FreeBSD, macOS, and Windows through the Strawberry Perl distribution. One script usually travels between them without changes, because the language and the standard library are the same everywhere.

Ask the interpreter which version is active. The answer is printed by the language itself, so it is the first sentence of any Perl debugging session:

print "$^V\n"; # prints something like v5.42.3

perl demo.pl

v5.42.3

Why learn Perl

Perl is the shortest path from a messy log file to a clean report. A one-line command can filter, transform and count millions of lines, which is why the language still runs inside build systems, mail filters and monitoring setups.

Learning it also pays off in maintenance work. Plenty of production code written between 1995 and 2010 is Perl, and someone has to read it. The habits you gain - regular expressions, testing, careful file handling - transfer to Python, Ruby and PHP.

my @words = qw(perl text report); print scalar @words, " words\n"; # prints 3 words

perl demo.pl

3 words

Three virtues

Wall named three programmer virtues: laziness, impatience and hubris. Laziness makes you write reusable tools instead of repeating work, impatience makes those tools fast to run, and hubris makes you write code that others can read without shame.

The same culture produced the famous motto there is more than one way to do it. Many problems have several idiomatic solutions, and the course shows the one that reads best, not the cleverest one.

my %seen = (); $seen{$_}++ for qw(a b a); print "$seen{a}\n"; # prints 2

perl demo.pl

2

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What kind of language is Perl?

    Answer

    An interpreted language that runs its source with no separate build step

    The perl program reads the source and runs it, so there is no compile step between the file and its result.

  2. Who created Perl, and in which year?

    Answer

    Larry Wall, in 1987

    Larry Wall wrote Perl in 1987 as a report-processing tool on Unix; the other three authors created Python, PHP and Ruby.

  3. Which field is the home ground of Perl, because the language grew out of it?

    Answer

    Text processing and reports

    Perl began as a report-processing tool, so text, regular expressions, files and pipes are part of the language itself.

  4. How is Perl installed on Windows?

    Answer

    From the Strawberry Perl distribution

    Strawberry Perl ships the interpreter and its build tools for Windows, and a script written there runs on Linux unchanged.

  5. Which variable holds the version of the running interpreter?

    Answer

    $^V

    $^V is a version object that prints as v5.42.3; $0 is the program name, @ARGV the arguments and $? the status of the last child process.

  6. What does this program print? print "Hello from Perl\n"; print 2 + 2, "\n";

    Answer

    Hello from Perl / 4

    The first line prints its string; the second evaluates 2 + 2 outside the quotes and prints 4.

  7. What does this program print? my @words = qw(perl text report); print scalar @words, " words\n";

    Answer

    3 words

    qw builds a three-element list, and scalar asks the array for its element count.

  8. What does this program print? my %seen = (); $seen{$_}++ for qw(a b a); print "$seen{a}\n";

    Answer

    2

    The key a is counted twice and b once, so $seen{a} holds 2.

  9. What does this program print? print "2 + 2", "\n";

    Answer

    2 + 2

    Everything inside the quotes is a string, so the arithmetic is never done: 2 + 2 is only text there.

  10. What does the Perl motto "there is more than one way to do it" mean?

    Answer

    Several idioms can solve one problem, and the course shows the one that reads best

    Perl offers several idiomatic answers to the same problem; the course picks the one that reads best rather than the cleverest.

Article author: Arthur Isaev