![]() |
Subroutines![]() ![]() ![]() ![]() ![]()
The main purpose of subroutines is to consolidate repeated sets of statements into a single block. This has the following advantages: If we want later to change the block of lines, we can do it in just one place instead of having to search the program for all places where the set of lines appears. Our program will be shorter and will thus run faster In long programs, subroutines can act like named "subprograms" to help us organise our program For example, imagine a program that has a dozen or so arrays, and at different points in the program we want to print just the last item in each list (array) and put a copy of that item into @big. Without subroutines it might look like this:
....
print("Price:$List[$List]); To call a subroutine, you use the ampersand (@) character followed by the subroutine name. If the subroutine takes arguments, you include those in parentheses just like we do for Perl's built in functions. Perl makes the argument called available in the special list @_ In the preceding example, the argument was a list, so @_ is a entire list. It is also common to have one or two arguments that are scalars. In this case, we can use shift function to get the arguments out of the @_ list in the order they went in.
Eg.: sub Add( Perl creates the variable $a to hold the first argument and $b to hold the second argument. The return function is the most common way to return a value. We can return either a scalar or a list.
|
|
PERL Perl Program Data Types Types of Variables Operator Types Functions String Functions Array Functions Expressions Statement Blocks The if Statement Unless Statement While Loops Until Loop For Loop Foreach Loop Jump Keywords Subroutines Perl References Die Function Exit Function Localtime Function Standard Files File Test Operators File Functions Opening Files Reading into Hash Printing Revisited Globbing Splitting a Record into Fields Perl's Special Variables Regular Expression Simple True-False Searches Inexact Matches in Regular Expressions Matching Any Character Characters with Class Special Locations Quantifiers Greedy Matching Modifiers Localtime Function Subsituations Letter for Letter Translations Generating A Simple Report Defining a Report Template Specifying the Picture Line Specifying the Argument Line Adding a Header Writing the ReportDomain Names Home Importance of a domain name How does a web domain name work? Domain name syntax Choosing a domain name Domain Transfer Domain Parking IP AddressWeb Design HTML DHTML XML JAVASCRIPT VBSCRIPT PHOTOSHOPWeb Hosting Web Server Web Server Software Microsoft Internet Information Server Web hosting ISP Shared web hosting Virtual web hosting Dedicated web hosting Web hosting colocation Managed web hostingE Commerce Principles of E-Commerce E-Banking Electronic Payment System E-Security Credit Cards Smart Cards
The Mailbox POP Server SMTP Server IMAP Protocol |
| Home | Web Hosting | Web Design | Sitemap |
| Copyright (C) 2007. Web Domain design hosting. All rights reserved. |