Perl
  Home arrow Perl arrow Page 3 - Perl 101 (Part 1) - The Basics
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 14
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    The Web Buyer's Guide is your best source for white papers on a wide range of IT products and services. This Week's Featured White Papers: Building a Virtual Infrastructure from Servers to Storage by Network Appliance

    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

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway