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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl 101 (Part 5) - Sub-Zero Code - Popping The Question
    (Page 4 of 7 )

    Usually, when a subroutine is invoked in Perl, it generates a "return value". This return value is either the value of the last expression evaluated within the subroutine, or a value explicitly returned via the "return" statement. We'll examine both these a little further down - but first, here's a quick example of how a return value works.

    #!/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";
    In this case, the value of the last expression evaluated within the subroutine serves as its return value - this value is then assigned to the variable $result when the subroutine is invoked from within the program.

    Of course, it's also possible to explicitly specify a return value - use the "return" statement, as we've done in the next example:

    #!/usr/bin/perl # define a subroutine sub do_you { if ($tall == 1 && $dark == 1 && $handsome == 1) { return "Yes!\n"; } else { return "Nope, afraid I don't feel the same way about you!\n"; } } $tall = 1; $dark = 1; $handsome = 1; # pop the question print "Will you marry me?\n"; # assign return value to variable $answer = &do_you; # print the answer print $answer;


    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=Jumping Cows And Extra-Large Pumpkins} Return values from a subroutine can even be substituted for variables anywhere in a program. For example, you could modify the last two lines of the example above to read:

    #!/usr/bin/perl # define a subroutine sub do_you { if ($tall == 1 && $dark == 1 && $handsome == 1) { return "Yes!\n"; } else { return "Nope, afraid I don't feel the same way about you!\n"; } } $tall = 1; $dark = 1; $handsome = 1; # pop the question print "Will you marry me?\n"; # assign return value to variable and print print(&do_you);
    And, of course, return values need not be scalar variables alone - a subroutine can just as easily return an array variable, as we've demonstrated in the following example:

    #!/usr/bin/perl # define a subroutine sub split_me { split(" ", $string); } # define string $string = "The cow jumped over the moon and turned into a gigantic pie"; # invoke function and assign result to array @words = &split_me; # loop for each element of array foreach $word (@words) { print "Word: $word\n"; $count++; } # print total print "The number of words in the given string is $count\n";
    The output is
    Word: The
    Word: cow
    Word: jumped
    Word: over
    Word: the
    Word: moon
    Word: and
    Word: turned
    Word: into
    Word: a
    Word: gigantic
    Word: pumpkin
    The number of words in the given string is 12



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