About Linux FAQ

Browse More FAQs:

Howto: Write script in Perl

Posted by Vivek on Tuesday July 17, 07 @7:03 pm

Q. I’m new to Linux and Perl. I’ve printed perl man page but it is bit confusing and omits a lot of simple things or details. Can you tell me how do I write a perl script? How do I open perl editor?

A. Larry Wall began work on Perl in 1987 and it is a dynamic programming language. Traditionally perl programs are written using text editor such as vi or emacs. The overall structure of Perl derives broadly from C.

Also when you start learning a new programming language, always start with Hello world program.

Hello world Perl Program

Let us print Hello world from a shell prompt. Type the following command (excluding $ ):
$ perl -e 'print "Hello, world!\n"';
Output:

Hello, world!

Let us write hello world program using vi text editor:
$ vi hello.pl
Append code as follows:
#!/usr/bin/perl
print "Hello, world!\n";

Save and close the file. Now setup a execute permission:
$ chmod +x hello.pl
Finally execute perl program:
$ ./hello.pl

  • First, I used the vi command to create a file named hello.pl
  • The first line of the script used to specify that the script is to be executed by perl program (#!/usr/bin/perl) and not by a shell.
  • Print command prints hello world on screen. Please note that the notation \n which stands for newline i.e. print a newline.

Further reading

This is just a simple introduction. You should consider following text books & resources for more information and mastering the perl:

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Linux / UNIX FAQ:

Discussion on This FAQ

  1. Andy Says:

    While you answered the question adequately, I think I understand the ‘asker’.

    When I started leaning PHP, all you have to do is go to php.net and it has an online reference regarding all the functions, even about the language syntax and grammar itself.

    I have looked at Perl, read a book, understand most of it’s syntax and quirks, but I have yet to find a site of PHP.net’s quality to really ‘dig’ into Perl.

  2. vivek Says:

    Andy,

    I agree with you. It is hard for a new person to get started with Perl. Php.net is one of the best site for all php related information.

  3. Andy Says:

    Chucks, part of the reason of my post, was polling if you knew a perl equivalent of php.net… guess not!

  4. vivek Says:

    Noop, there is no such a site exists, AFAIK. You can try http://perldoc.perl.org/ (same as reading perldoc on UNIX)

    HTH

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Please do not use the comment form to ask for help / question. Ask your question on the excellent Linux tech support forum. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , ~ Last updated on: July 17, 2007

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.