Perl
  Home arrow Perl arrow Page 5 - 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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Perl 101 (Part 5) - Sub-Zero Code - Turning Up The Heat
    (Page 5 of 7 )

    Now, if you've been paying attention, you've seen how subroutines can help you segregate blocks of code, and use the same piece of code over and over again, thereby eliminating unnecessary duplication. But this is just the tip of the iceberg...

    The subroutines you've seen thus far are largely static, in that the variables they use are already defined. But it's also possible to pass variables to a subroutine from the main program - these variables are called "arguments", and they add a whole new level of power and flexibility to your code.

    Consider the following simple example:

    #!/usr/bin/perl # define a subroutine sub add_two_numbers { $sum = $_[0] + $_[1]; return $sum; } $total = &add_two_numbers(3,5); print "The sum of the numbers is $total\n";
    A few words of explanation here:

    You're already familiar with the special Perl array @ARGV, which contains parameters passed to the Perl program on the command line. Well, Perl also has a variable named @_, which contains arguments passed to a subroutine, and which is available to the subroutine when it is invoked. The value of each element of the array can be accessed using standard scalar notation - $_[0] for the first element, $_[1] for the second element, and so on.

    In the example above, once the &add_two_numbers subroutine is invoked with the numbers 3 and 5, the numbers are transferred to the @_ variable, and are then accessed using standard scalar notation within the subroutine. Once the addition has been performed, the result is returned to the main program, and displayed on the screen via the print() statement.

    Note the manner in which arguments are passed to the subroutine, enclosed within a pair of parentheses.

    How about something a little more useful? Let's go back a couple of pages, and consider the &change_temp subroutine we've defined:

    #!/usr/bin/perl # define a subroutine sub change_temp { $celsius = 35; $fahrenheit = ($celsius * 1.8) + 32; } # assign return value to variable $result = &change_temp; print "35 Celsius is $result Fahrenheit\n";
    Now, suppose we alter this to accept the temperature in Celsius from the main program, and return the temperature in Fahrenheit.

    #!/usr/bin/perl # define a subroutine sub change_temp { $fahrenheit = ($_[0] * 1.8) + 32; } print "Enter temperature in Celsius\n"; $temperature = ; chomp ($temperature); $result = &change_temp($temperature); print "$temperature Celsius is $result Fahrenheit\n";
    And here's what it would look like:
    Enter temperature in Celsius
    45
    45 Celsius is 113 Fahrenheit

    Take it one step further - how about allowing the user to specify the temperature to be converted on the command line itself?

    #!/usr/bin/perl # define a subroutine sub change_temp { $fahrenheit = ($_[0] * 1.8) + 32; } # get the command-line parameters # and pass them to the subroutine # and assign the result $result = &change_temp(@ARGV); # print the result print "$ARGV[0] Celsius is $result Fahrenheit\n";
    If you saved this program as "convert_temp.pl", and ran it like this

    $ convert_temp.pl 35
    you'd see
    35 Celsius is 95 Fahrenheit

    The above example also neatly demonstrates the relationship between @ARGV and @_ - the temperature entered on the command line first goes into the @ARGV variable, and is then passed to the subroutine via the @_ variable. Remember that the @_ variable is only available within the scope of a specific subroutine.

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