Installing perl and writing your first perl program in Ubuntu

What Is Perl?

Perl is an acronym, short for Practical Extraction and Report Language. It was designed by Larry Wall as a tool for writing programs in the UNIX environment and is continually being updated and maintained by him. For its many fans,

  • Perl provides the best of several worlds. For instance: Perl has the power and flexibility of a high-level programming language such as C. In fact, as you will see, many of the features of the language are borrowed from C.
  • Like shell script languages, Perl does not require a special compiler and linker to turn the programs you write into working code. Instead, all you have to do is write the program and tell Perl to run it. This means that Perl is ideal for producing quick solutions to small programming problems, or for creating prototypes to test potential solutions to larger problems.
  • Perl provides all the features of the script languages sed and awk, plus features not found in either of these two languages. Perl also supports a sed-to-Perl translator and an awk-to-Perl translator.

In short, Perl is as powerful as C but as convenient as awk, sed, and shell scripts.

How to install Perl on Ubuntu

Perl is located in the ubuntu repositories, you can install it by the following command.

sudo apt-get install perl

perl is installed in usr/bin/
Writing your first perl program

#!/usr/bin/perl
# A simple perl program to print the user input
print ("Hello, type in something\n");
$inputline=;
print ($inputline);

Lets split up the code and see what each line does..
#!/usr/bin/perl
# Says this line is a comment and there are no executable instructions on this line
! Says this is a perl script
/usr/bin/perl give the location of the perl interpreter, many programing books mention the location as usr/local/bin/perl, but this is not correct in ubuntu. In ubuntu Perl interpreter is located at usr/bin/perl.
print “Hello, type in something\n”;
This line just prompts to user to type something, similar to printf statement in C.
$inputline=<stdin>
Here we are setting a variable named inputline, and storing the input from the keyboard into that variable. <stdin> takes the input from the keyboard.
print ($inputline);
This line echoes the typed in message.

Running your first Perl program

Copy the above above program in to your favorite text editor and save it as myFirstPerlProgram.pl and then change it to a executable, you can do that by typing the following in the terminal

chmod +x myFirstPerlProgram.pl

now you can run this code by typing myFirstPerlProgram.pl in the terminal

Output of the above perl program

Output of the above perl program

12 thoughts on “Installing perl and writing your first perl program in Ubuntu

  1. STDIN should be written in caps. You can even omit it.
    my $inputline=;

  2. characters where eaten.

    my $inputline=<>;

  3. @Alexandr Ciornii,

    STDIN need not be written in caps, i copy pasted the working program.

    You can not omit , If you omit how will the interpretor know where to take the input from??
    Character were modified by the wysiwig editor during an update, i will fix them now.

    Thanks for taking the time to comment

  4. Sweet! Thanks for the intro!

  5. If you can correct this –
    chomod +x myFirstPerlProgram.pl to remove the ‘o’ from chmod please do so. I got hung up for a little bit until I looked down at your example console input.

  6. Sorry for the trouble, corrected that now.

  7. this worked

    #!/usr/bin/perl
    #simple perl program to print the user input
    print (“Hello, type in something\n”);
    $inputline=;
    print ($inputline);

  8. oops !! it didn’t copied correctly..

    $inputline=;

    is what i meant

  9. $inputline=

    this also worked man!!

  10. varun singh says:

    i got syntax error

    syntax error at ./firstperl.pl line 4, near “=;”

    i typed in the same code exactly and it gives me this error

    i am runnning ubuntu lucid lynx 10.04

  11. good example .but in some perl programs i have seen print “Content-type: text/html\n\n”
    what does it mean .

  12. Hi, I am new to Perl as well as in programming feild and i just started to learn. I have to do Perl program in linux. Can u plz help me learn basics commands like opening file, saving, editing and running the program.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>