In this tutorial, we will learn how to write first perl program and run it on various Operating Systems.
First Perl Program: Hello World
This is a simple perl program that prints Hello World!! on the screen.
#!/usr/bin/perl #this is a comment print "Hello World!!";
Here first instruction #!/usr/bin/perl tells Operating systems to run this program with the interpreter located at /usr/bin/perl.
As I explained in the perl introduction that perl is a high level language that uses normal english, the problem is that machine doesn’t understand this high level language , therefore in order to make this program understandable by machine, we must interpret it into machine language. Interpreter is what interpret this program into machine language.
Second instruction #this is a comment is a comment, whatever you write after hash(#) is considered as comment
Third instruction prints “Hello World!!”; prints Hello World!! on the screen. Note the semicolon(;) at the end of instruction, you must put a semicolon at the end of every instruction, this tells interpreter that the instruction is finished.
How to run the perl program?
In order to run the above program, copy the code into a text editor like notepad for windows, textedit for mac etc. and save the file with .pl extension. lets say we save the file as firstprogram.pl.
To run the program on Windows, open command prompt (cmd) and write the following command
perl c:\perl\bin\perl.exe firstprogram.pl
Note: The above method is for running the perl program using command prompt. Alternatively, if you are using Strawberry Perl or Active Perl then you can simply click run.
On Linux/Unix/Mac OS
To run the program on Linux, Unix or Mac OS. Run the following commands on terminal.
chmod +x firstprogram.pl ./firstprogram