Function Templates in PERL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sreemakam
    New Member
    • Aug 2006
    • 3

    Function Templates in PERL

    Hello,

    Does any body know if PERl supports function templates just as C++ does. I have heard of object-oriented PERL. Hence, wanted to use the feature. Please give the snippet of code also in case u have any.

    Thanks,
    sreemakam
  • Rykus
    New Member
    • Sep 2006
    • 5

    #2
    Originally posted by sreemakam
    Hello,

    Does any body know if PERl supports function templates just as C++ does. I have heard of object-oriented PERL. Hence, wanted to use the feature. Please give the snippet of code also in case u have any.

    Thanks,
    sreemakam
    in C++ you would have:

    Code:
    int something( int arg1, int arg2 )
    {
      ...processing...
    }

    in PERL this is done as follows:

    Code:
    sub something
    {
      ...processing...
    }
    and called by putting an ampersand (&) at the front of the function name:
    Code:
    &something( val1, val2 );

    Input parameters are passed via the @_ array variable.


    SO a sample function would be:


    Code:
    sub ADD
    {
      $val1 = $_[0];
      $val2 = $_[1];
    
      return $val1 + $val2;
    }
    
    
    $answer = &ADD( 2, 3 );

    Comment

    Working...