Perl Topics





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Perl Tutorial
Introduction Introduction
Installing Perl Installing Perl
Basics of Perl Basics of Perl
Scalar Variables Scalar Variables
Operators Operators
Control Structures Control Structures
Lists Lists
Array Array
Arrays Manipulation Arrays Manipulation
Arrays Functons Array Functions
Hash Hash
Hash Functions Hash Functions
String Functions String Functions
Regular Expression Regular Expression
Regular Expression Functions Regular Expression Functions
Numerical Functions Numerical Functions
List Functions List Functions
User Defined Function User Defined Function
File Handling File Handling
Forums Ask Your Doubts
Feedback Feedback
 





User Defined Functions in Perl


Tutorials Perl

Topic

What is a User Defined Function in Perl?
How to call a function using user defined functions?


Explanation

The User Defined Function is a set of code that will be executed when the function is called. In Perl user defined function does'nt require arguments, but if needed arguments can be passed.

Syntax:
    sub function_name
     {
      function_body
     }

In the above syntax "function_name" specifies a unique name for the function, in the "function body" contains a set of code to be executed when there is a function call.

Example for user defined func, using function call:
    #! C:\programfiles\perl\bin\perl
    print "content-type: text/html\n\n";
    sub present 
     {
      print "The Key is present.\n";
     }
    sub notpresent
     {
      print "The Key not present.\n";
     }
    %emp = ('empno' => '12422' , 'empname' => 'Kate Anderson', 
            'empph' => '9167552148', 'empcode' => 'ADS212');
    if (exists $emp{'empcode'})
     {
      present();
     }
    else
     {
      notpresent();
     }
Result:
    The Key is present. 

In the above example using exists() function a value is checked in an array, if present, it calls a function "present()" otherwise, it calls a function "notpresent()". Since the "empcode" is present, it displays "The Key is present".






A Note
Simple introduction, basic CGI perl programming codes with examples. Do send your feedback or suggestions on this tutorial. This is a copyright content.

Other Links

web hosting