469,323 Members | 1,247 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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
Sep 15 '06 #1
1 1434
Rykus
5
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:

Expand|Select|Wrap|Line Numbers
  1. int something( int arg1, int arg2 )
  2. {
  3.   ...processing...
  4. }

in PERL this is done as follows:

Expand|Select|Wrap|Line Numbers
  1. sub something
  2. {
  3.   ...processing...
  4. }
and called by putting an ampersand (&) at the front of the function name:
Expand|Select|Wrap|Line Numbers
  1. &something( val1, val2 );

Input parameters are passed via the @_ array variable.


SO a sample function would be:


Expand|Select|Wrap|Line Numbers
  1. sub ADD
  2. {
  3.   $val1 = $_[0];
  4.   $val2 = $_[1];
  5.  
  6.   return $val1 + $val2;
  7. }
  8.  
  9.  
  10. $answer = &ADD( 2, 3 );
Sep 20 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Sam | last post: by
22 posts views Thread by E. Robert Tisdale | last post: by
7 posts views Thread by Hendrik Schober | last post: by
2 posts views Thread by Markus Dehmann | last post: by
6 posts views Thread by Stuart McGraw | last post: by
3 posts views Thread by abhishek.smu | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.