Perl
  Home arrow Perl arrow Page 3 - Perl 101 (Part 1) - The Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (Part 1) - The Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 16
    2000-05-23


    Table of Contents:
  • Perl 101 (Part 1) - The Basics
  • ...And The Little Language That Could!
  • Your First Perl Program
  • To Err Is Human...To Debug, Divine!
  • What's Next?

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl 101 (Part 1) - The Basics - Your First Perl Program
    ( Page 3 of 5 )

    And now that you've got Perl installed and configured, how about actually doing something with it? Use your favourite text editor to type the following lines of code:
    #!/usr/bin/perl
    # Perl 101
    print ("Groovy, baby!\n");

    Save this file as "groovy.pl".

    Next, you need to tell the system that the file is executable. On a UNIX system, this is accomplished by setting the "executable bit" with the "chmod" command:
    $ chmod +x groovy.pl

    And now run the script - in UNIX, try
    $ ./groovy.pl

    On a Windows system, you need to pass the name of the script to the Perl executable as a parameter, like this:
    > perl groovy.pl

    or
    >  groovy.pl

    In both cases, the script should return Austin Powers' trademark line. Ain't Perl groovy, baby!

    In case things don't work as they should, it usually means that the system was unable to locate the Perl binary. This is a good time to holler for the system administrator.{mospagebreak title=Your First Perl Program Dissected} Let's take a closer look at the script. The first line
    #!/usr/bin/perl

    is used to indicate the location of the Perl binary. This line must be included in each and every Perl script, and its omission is a common cause of heartache for novice programmers. Make it a habit to include it, and you'll live a healthier, happier life.

    Next up, we have a comment.
    # Perl 101

    Comments in Perl are preceded by a hash [#] mark. If you're planning to make your code publicly available on the Internet, a comment is a great way to tell members of the opposite sex all about yourself - try including your phone number for optimum results.

    And finally, the meat of the script:
    print ("Groovy, baby!\n");

    In Perl, a line of code like the one above is called a "statement". Every Perl program is a collection of statements, and a statement usually contains instructions to be carried out by the Perl interpreter.

    In this particular statement, the print() function has been used to send a line of text to the screen. Like all programming languages, Perl comes with a set of built-in functions - the print() function is one you'll be seeing a lot of in the future. The text to be printed is included within double quotes, and the entire thing is then surrounded by parentheses. Note the n character, used to indicate a new line.

    C programmers will be familiar with the print() function call above, as also with the fact that in C, it is mandatory to place function arguments within parentheses. Perl is more flexible than C in this regard - in many cases, parentheses can be excluded in function calls, as in this example:
    #!/usr/bin/perl
    # Perl 101
    print "Groovy, baby!\n";

    Every Perl statement ends with a semi-colon. Don't ask why - just do it!

    And here's an interesting bit of trivia - every Perl statement can be further sub-divided into smaller units called "tokens". If you take a look at the statement above, you'll see three distinct tokens - print, ("Groovy, baby!\n") and the semi-colon. The interesting thing about tokens is that they can be separated with white space and tabs - which, translated, means that you could also write the above line as
    print 	("Groovy, baby!\n")       ;

    or
    print("Groovy, baby!\n")		;

    and things would still work as advertised. Needless to say, this comes in very useful when dealing with long and complex scripts.{mospagebreak title=Two Plus Two} So now you know how to print text - how about a little math? Try this:

    #!/usr/bin/perl # Some addition print (10+2);
    And this should give you the answer of that particular mathematical operation.

    You can even try subtraction, multiplication and division:

    #!/usr/bin/perl # Some more math print (10-2); print (10 * 2); print (10/2);
    Note how the various results are concatenated together in the absence of the newline character.

    And you can even combine text and mathematical operations - this will be covered in greater detail over the next few weeks, but for the moment, you can play with this simple example:

    #!/usr/bin/perl # Synthesis print ("Hello there! And what are you doing ", 1+1, "night, baby?\n");


     
     
    >>> More Perl Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT