Perl
  Home arrow Perl arrow Page 2 - Perl 101 (Part 5) - Sub-Zero Code
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 
IBM Rational Software Development Conference
 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 5) - Sub-Zero Code
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2000-07-21

    Table of Contents:
  • Perl 101 (Part 5) - Sub-Zero Code
  • Great Movies...
  • ...And Memorable Friends
  • Popping The Question
  • Turning Up The Heat
  • My() Hero!
  • The Age Gauge

  • 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

    Dell PowerEdge Servers

    Perl 101 (Part 5) - Sub-Zero Code - Great Movies...
    (Page 2 of 7 )

    Ask a geek to define the term "subroutine", and he'll probably tell you that a subroutine is "a block of statements that can be grouped together as a named entity." Since this definition raises more questions than answers [the primary one being, what on earth are you doing hanging around with geeks in the first place], we'll simplify things by informing you that a subroutine is simply a set of program statements which perform a specific task, and which can be called, or executed, from anywhere in your Perl program.

    There are two important reasons why subroutines are a "good thing". First, a subroutine allows you to separate your code into easily identifiable subsections, thereby making it easier to understand and debug. And second, a subroutine makes your program modular by allowing you to write a piece of code once and then re-use it multiple times within the same program.

    Let's take a simple example, which demonstrates how to define a sub-routine and call it from different places within your Perl script:
    #!/usr/bin/perl
    # define a subroutine
    sub greatest_movie
    {
    print "Star Wars\n";
    }
    # main program begins here
    print "Question: which is the greatest movie of all time?\n";
    # call the subroutine
    &greatest_movie;
    # ask another question
    print "Question: which movie introduced the world to Luke Skywalker, Yoda
    and Darth Vader?\n";
    # call the subroutine
    &greatest_movie;

    Now run it - you should see something like this:
    Question: which is the greatest movie of all time?
    Star Wars
    Question: which movie introduced the world to Luke Skywalker, Yoda and
    Darth Vader?
    Star Wars

    Let's take this line by line. The first thing we've done in our Perl program is define a new subroutine with the "sub" keyword; this keyword is followed by the name of the subroutine. All the program code attached to that subroutine is then placed within a pair of curly braces - this program code could contain loops, conditional statements, calls to other subroutines, or calls to other Perl functions. In the example above, our subroutine has been named "greatest_movie", and only contains a call to Perl's print() function.

    Here's the typical format for a subroutine:
    sub subroutine_name
    {
    statement 1...
    statement 2...
    .
    .
    .
    statement n...
    }


    This article copyright Melonfire 2000. All rights reserved.

    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 6 hosted by Hostway