CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in
Add ListingModify ListingTell A FriendLink to TPASubscribeNew ListingsCool ListingsTop RatedRandom Link
Newest Reviews
  • review
  • hagen software
  • NOT GPL!
  • Hagan Software
  • Wasted Time with ...
  • poor pre-sale sup...
  • no response
  • rating the offer
  • Good Stuff
  • Good idea but use...


  • Brochure Templates  
     
    Perl Archive : TLC : Programming : Perl : Intro to Perl: Subroutines
    Guide Search entire directory 
     

    Date Published: 1999-10-01

    Intro to Perl
    Main Page
    Part 1: Scalars
    Part 2: Arrays
    Part 3: Hashes
    Part 4: Subroutines
    Part 5: Putting it Together
    by D. Jasmine Merced
    TNS Group, Inc.

    Subroutines

    By definition, a subroutine is a set of instructions that performs a specific task for a main routine, requiring direction back to the proper place in the main routine on completion of the task.

    Subroutines are generally used for tasks that are repeated throughout a program. By placing lines of code within a subroutine, that portion of code is readily available to the rest of the program.

    Please note that some programs are small, or don't need tasks to be repeated. In these cases, subroutines aren't needed or aren't used.

    As you may already know, perl programs are interpreted programs, which mean the perl interpreter reads and executes the program from top to bottom until the program completes its tasks (or fails). Subroutines are completely ignored until they are "called" by a line (outside of the subroutine) in the program.

    Let's take a look at subroutine's basic structure:

    sub name {
        print "hello";
    }

    Subroutines begin with the word sub, are then given a unique subroutine name, and the tasks are enclosed within curly braces {}.

    To "call" this sample subroutine, the programmer would use:

    &name;

    Wherever &name; appears in the program, the word hello will be inserted. Not a very useful subroutine, but you get the idea.

    As with most things, subroutines get more useful as they get more complicated. For the most part, unless you're programming your own scripts, you'll never need to touch them.

    The most useful subroutines have scalar variables passed to them. They then do something to the variables, such a perform calculations, format, etc. and return the result.

    For example, let's say you wanted to convert degrees Fahrenheit to Celsius. You can pass Fahrenheit to a subroutine, let the subroutine perform the proper conversion calculation, and give the Celsius back to the program. Let's see it in use.

    Running the program will print the following:

    95 degrees Fahrenheit is 35 degrees Celsius.

    The line-by-line.

    #!/usr/bin/perl
    This first line tells the program where to find the perl interpreter.

    $fahrenheit = 95;
    Here, we're defining $fahrenheit as 95.

    $celsius = &convert($fahrenheit);
    Here, we're saying that $celsius is equal to the result of the "convert" subroutine using the value of $fahrenheit. The $fahrenheit in parenthesis immediately after the &convert tells perl to use the value of $fahrenheit in the subroutine. Called "passing variables" to the subroutine, this allows you to use one subroutine from different parts of your program without duplicating the code.

    print "$fahrenheit degrees Fahrenheit is $celsius degrees Celsius.\n";
    Because the previous line acquired the value of $celsius, here we print the result.

    sub convert {
    Names and opens the subroutine.

    my $fahr = $_[0];
    Tells perl to use assign $fahr to the first value passed to the subroutine ($fahrenheit). Remember that perl starts counting at zero.

    my $cels = ($fahr - 32) * 5/9;
    This assigns $cels with the result of the Fahrenheit to Celsius equation.

    return $cels
    This line tells perl to return the value of $cels back to the point where this subroutine was invoked. Referring back to $celsius = &convert($fahrenheit) now $celsius equals $cels.

    }
    Closes the subroutine.

    Now let's say you needed to convert the degrees Fahrenheit to Celsius for the past 5 days. You can reuse the same subroutine for each day. Let's take a look...

    (It's important to note that this by far isn't the best way to write a multi-day/variable program, but I strove to demonstrate the efficiency of subroutines without introducing too many new concepts).

    As you can see, this "modular" use of subroutines increases program efficiency and overall usability.

    Subroutines are use to keep individual tasks together. Ranging from simple subroutines, that output static text to more robust subroutines, which receive and manipulate data, subroutines are the epitome of "rinse and reuse".

    Next month, we will bring all of these "Intro" articles:

    together and show you how real programs use each of these pieces. From there, we're ready to start customizing some programs!

     

    D. Jasmine Merced is a partner in Tintagel Net Solutions Group, Inc. and the administrator of The Perl Archive. She also serves as a Director of the World Organization of Webmasters.

     
     


    About The Perl ArchiveLink Validation ProcessSearch Tips
    Web Applications & Managed Hosting Powered by Gossamer Threads
    Visit our Mailing List Archives