Your first Perl program
A Perl program is a text file you hand to the perl command.
In this page:
Your first Perl program
Save your code in a file that ends in .pl, then run it with perl filename.pl. Every statement ends with a semicolon, and print sends text to the screen. The special sequence \n at the end of a string starts a new line.
Note:
On Unix-like systems you can add a first line such as #!/usr/bin/perl and make the file executable, so you can run it without typing perl.
Example: Your first Perl program
#!/usr/bin/perl
use strict;
use warnings;
print "Hello, World!\n";
print "My first Perl program\n";
print "Script name: $0\n";
# Output:
# Hello, World!
# My first Perl program
# Script name: script.pl
Login to try C/C++/Java/PHP code in the editor
Common Mistakes
- Forgetting the semicolon at the end of a statement
- Forgetting \n so that all output ends up on one line
- Running the script without the perl command when it is not executable
Chapter Summary
- Save scripts as .pl files
- Run them with perl script.pl
- Statements end with semicolons
- print writes text and \n ends a line
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: