IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Perl
Search Perl Group:

Advanced
-Collapse +Expand Perl To/From
To/FromCODEGuides
-Collapse +Expand Perl Store
PRESTWOODSTORE

Prestwood eMagazine

September Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...Perl   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Perl Custom Routines (sub)
 
Posted 16 years ago on 11/27/2008
Perl Code Snippet:
 A flashcard from our Perl Flashcards Library
 A code snippet from our Perl Code Snippets Page

KB101659

Languages Focus: Custom Routines

For non-OOP languages, a custom routine is a function, procedure, or subroutine and for pure OOP languages, a custom routine is a class method. Hybrid languages (both non-OOP and OOP) combine both.

Perl Custom Routines

Perl uses subs and parameters are referenced in a special array. All arguments passed to a subroutine are stored in a special @_ array. To retrieve the arguments, you have to look inside the array and extract them.

Syntax Example:
sub sayHello {
 my ($pName) = $_[0];
 print("Hello $pName!");
}
sub add {
 my ($p1) = $_[0];
 my ($p2) = $_[1];
 return $p1 + $p2;
}

Complete Example

Notice in the following example the subs are located after the printing of the HTML closing tag. You can put them in a library but when I put them as part of a called page, I like to put them at the end.

#!/usr/local/bin/perl -w
 
print("Content-type: text/html\n\n");
 
#Start of HTML page.
print("<html>");
print("<head><title>Perl Tutorial</title></head>");
print("<body>");
 
&sayHello("Mike");
print("<br>");
 
#Example 1
$x = &add(2,1);
print("2+1=$x.<br>");
 
#Example 2 - no parens
$x = &add(2,2);
print "2+2=$x.<br>";
 
#Example 3 - parens with . concatenation operator
$x = &add(2,3);
print("2+3=" . $x . ".<br>");
 
#Example 4 - using sub directly
print("2+4=" . &add(2,4) . ".<br>");
print("</body></html>");
 
#
# Subs
#
sub sayHello {
 my ($pName) = $_[0];
 
 print("Hello $pName!");
}
 
sub add {
 my ($p1) = $_[0];
 my ($p2) = $_[1];
 
 return $p1 + $p2;
}

More Info


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P177A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101659 Counter
13920
Since 11/27/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]