DISCLAIMER: Image is generated using FREE
version of ChatGPT
.
Welcome to Perl
1. Introduction
2. The Shebang
3. Command Interpretation
4. Interpreter Initialisation
5. File Handling
6. Lexical Analysis
7. Bytecode Generation
8. Runtime Execution
9. Output
10. Termination
11. Returns Control
Introduction
Are you new to Perl
?
If so, I’m pretty sure, you’d want to write your first Hello World
program in Perl
.
Here’s what it looks like:
#!/usr/bin/perl
use v5.38;
say "Hello World!!";
To run the script, use the following commands:
$ chmod +x hello-world.pl
$ ./hello-world.pl
Hello World!!
The Shebang
Let me explain the first line in the program, it is very important and plays an important role.
The line #!/usr/bin/perl
at the beginning of a Perl
script is called a shebang
or hashbang
.
What’s the purpose of this line?
It tells the system which interpreter should execute the script.
When you run the script directly e.g. ./hello-world.pl
, the system reads this line and runs /usr/bin/perl
to interpret the script.
Without it, the shell wouldn’t know whether the file is a Perl
script, Bash
script, or something else.
With this shebang
, you can run the script directly:
$ ./hello-world.pl
Hello World!!
Instead of explicitly calling:
$ perl hello-world.pl
Hello World!!
The shebang
also allows us to include command-line options:
a) Enables warning: #!/usr/bin/perl -w
b) Enables taint : #!/usr/bin/perl -T
A better shebang
i.e. #!/usr/bin/env perl
.
Why use this?
Some systems may have Perl
installed in a different locations, such as:
a) /usr/local/bin/perl
b) /opt/homebrew/bin/perl on macOS
Using this, #!/usr/bin/env perl
, we let the env
command to find Perl
in the user’s $PATH
, making the script more portable across different environments.
Thus, a script without a shebang
must be executed explicitly:
The shebang is a crucial part of making Perl
scripts self-executable.
$ perl hello-world.pl
Hello World!!
But with a shebang
, you can simply run:
$ ./hello-world.pl
Hello World!!
The shebang
is a crucial part of making Perl
scripts self-executablle.
Ever wondered what happens when you run a Perl
script?
Let’s go through all the stages before you see the message: Hello World!!
Command Interpretation
The shell parses the command perl hello-world.pl
.
It checks if perl
is a valid command by looking it up in the $PATH
environment variable.
After that, the shell prepares to execute the Perl
interpreter.
Interpreter Initialisation
The Perl
interpreter (perl
) is loaded into memory.
It initialises its runtime environment, including:
a) Setting up memory management.
b) Loading core Perl modules.
c) Parsing command-line arguments, if any.
File Handling
Perl
opens and reads the file hello-world.pl
.
If the file doesn’t exist, Perl
throws an error e.g. Can't open perl script "hello-world.pl": No such file or directory
and the process stops.
Lexical Analysis
The source code is broken into tokens.
For example: say "Hello World!!";
is tokenised into:
a) say (built-in function),
b) "Hello World!!" (string literal),
c) ; (statement terminator).
Bytecode Generation
The tokens are parsed into an Abstract Syntax Tree (AST)
.
Perl
then checks syntax.
If there are syntax errors, Perl
reports them and exits.
If successful, Perl
generates bytecode.
Runtime Execution
The compiled bytecode is executed line by line:
a) Variables are initialised.
b) Functions (e.g., say) are called.
c) System calls (if any) are executed.
d) External modules (if loaded via use or require) are initialised.
Output
Any output from print
, say
, or other I/O
functions is sent to:
a) Standard output i.e. STDOUT.
b) Standard error i.e. STDERR.
Buffering may delay output until a newline or buffer flush.
Termination
The script exits when:
a) It reaches the end of the file.
b) An explicit exit or die is encountered.
Perl
then performs cleanup:
a) Closes open file handles.
b) Calls END blocks, if defined.
The interpreter releases memory and exits.
Returns Control
The shell regains control after Perl
exits.
The script’s output, if any, is displayed in the terminal.
The shell prompt reappears, waiting for the next command.
Happy Hacking!!