Perl
  Home arrow Perl arrow Page 3 - 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 
 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 - ...And Memorable Friends
    (Page 3 of 7 )

    Of course, defining a subroutine is only half of the puzzle - for it to be of any use at all, you need to invoke it. In Perl, this is accomplished by calling the subroutine by its name, as we've done in the last line of the example above. When invoking a subroutine in this manner, it's usually a good idea to precede the name with an ampersand [&]- this is not essential, though, and the following would also work:

    #!/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;
    However, it's good programming practice to precede the name of a Perl subroutine with an ampersand when invoking it - this helps to differentiate the Perl subroutine from array or scalar variables that may have the same name, and from pre-defined Perl functions. For example, consider this piece of code, in which we've defined a subroutine called &print, which also happens to be the name of the in-built Perl print() function:

    #!/usr/bin/perl # define a subroutine sub print { print "Ross\n"; } # main program begins here print "Question: which Friend once had a pet monkey?\n"; # call the subroutine print;
    In this case, when you run the program, you'll get the following:
    Question: which Friend once had a pet monkey?

    Here, Perl assumes that the line containing the print statement is a call to the in-built Perl print() function, rather than the user-defined Perl &print subroutine. To avoid this kind of error, you should either avoid naming your subroutines after reserved words, or precede the subroutine call with an ampersand. In the example above, if you changed the last line from

    print;
    to

    &print;
    Perl would understand the subroutine call correctly, and display the desired output.


    Question: which Friend once had a pet monkey?
    Ross



    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