Perl First Example

After perl installation, we'll perform our first perl program.

Click on Start-> All Programs->SWIN Perl->Padre, it opens an editor where you can write your script.

Type the following:

snippet
print "Hello World! with Perl\n";

Perl statements end with semicolon (;) as shown above. The (\n) is used to denote a new line. As it is a string, it will be enclosed in double quotes (""). And finally 'print' will display it on the screen.

Saving File:

Save the file with (.pl) extension.

Running Script:

To run it, go to Run-> Run Script in the above tool bar. Or in short you can also run it by pressing F5.

Output:

Output will be shown in a different command line window displaying the following letters.

snippet
Hello World! with Perl

Displaying Output without using Perl Padre

If you are not using Padre editor or any other IDEs, your script will not run from the editor itself.

You'll have to open command line, change to the directory where you stored your script and type print with your file name as shown below.

snippet
Print abc.pl

Here, abc.pl file will run.

Perl print() and say()

The say() is not supported by the older perl versions. It acts like print() with only difference that it automatically adds a new line at the end without mentioning (\n).

Note
Note: you need to mention the version in your script to use the say() function.

Type in:

snippet
say "Hello World!";

Output:

snippet
Hello World!

Perl Recommendations

We recommend adding two pragmatas 'strict' and 'warnings' in every script. The use keyword is used to load and enable pragma.

These pragmatas will help you to catch some common bugs in your code or may prevent you from making them.

Stating the perl version at top in your script is also recommended. It tells minimum version of perl your code requires.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +