Your first Perl script

The file

A Perl program is a text file with a .pl extension, though nothing forces the name. Create hello.pl and put four lines in it: a shebang, the two pragmas every modern file starts with, and one print.

Create the script

vi hello.pl

#!/usr/bin/perl use strict; use warnings; print "Hello, world\n";

The first line is the shebang: it names the interpreter that should run the file when you execute the file directly. On systems where perl lives elsewhere, #!/usr/bin/env perl is the portable form.

strict and warnings

use strict turns three common mistakes into errors: undeclared variables, barewords that look like functions, and string references used as symbolic ones. use warnings adds advice about suspicious code, such as a numeric comparison against text that never looks numeric.

Together they cost two lines and save hours. Add them to every file you write, including one-off scripts that you plan to delete tomorrow - those are the ones that survive for years.

Run it

Hand the file to the interpreter by name. No build step, no output folder: the program runs and prints one line.

perl hello.pl

Hello, world

If the shell answers Can't open perl script, you are in the wrong folder. Change into the directory that holds hello.pl and try again.

Make it executable

With the shebang in place, the file can run like any other command. Grant the execute bit once, then call it by name:

chmod +x hello.pl

./hello.pl

Hello, world

The leading dot and slash are required: without them the shell looks for hello.pl in the folders listed in PATH, not in the current one. That rule is the same for every script you will write in this course.

Questions

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

  1. What is the line #!/usr/bin/perl called, and what does it do?

    Answer

    A shebang: it names the interpreter that runs the file when the file itself is executed

    The kernel reads the first two characters of an executable file and starts the program the line names, which is why the line must come first.

  2. Which shebang keeps the script working on a system where perl lives outside /usr/bin?

    Answer

    #!/usr/bin/env perl

    env searches PATH for perl, so the file does not depend on one install location; this is the portable form.

  3. Which three mistakes does use strict turn into errors?

    Answer

    Undeclared variables, barewords that look like functions, and symbolic references

    strict has three parts: vars, subs and refs, and each catches one of those slips.

  4. What does use warnings add to a program?

    Answer

    Advice about suspicious code, such as comparing a number with text that never looks numeric

    warnings is Perl telling you about code that runs but probably does not mean what it says.

  5. Why does the page run the script as ./hello.pl and not as hello.pl?

    Answer

    The shell searches PATH for a bare name, and the current folder is usually not in it

    A bare name is a command lookup; the dot and slash name a path in the current folder.

  6. What does chmod +x hello.pl change?

    Answer

    It sets the execute bit, so the file can run as a command

    The execute bit lets the kernel start the file, and the shebang then chooses the interpreter.

  7. What does this script print? #!/usr/bin/perl use strict; use warnings; print "Hello, world\n";

    Answer

    Hello, world

    The pragmas change nothing at run time, and the escape \n ends the line without being printed.

  8. This program is saved as strict.pl. What does perl -c strict.pl report? use strict; use warnings; $name = "Ada"; print "$name\n";

    Answer

    Global symbol "$name" requires explicit package name (did you forget to declare "my $name"?)

    Without my, strict refuses the variable at compile time; the message names the declaration that is missing.

  9. What does this program print? use strict; use warnings; print "Hello, world\n"; print "Bye\n";

    Answer

    Hello, world / Bye

    Each print sends one line, and the run prints them in the order of the statements.

  10. The shell answers Can't open perl script "hello.pl": No such file or directory. What is wrong?

    Answer

    The shell is in another folder and does not see hello.pl there

    Perl received the file name and could not open it, which is a path problem: change into the folder that holds the script.

Article author: Arthur Isaev