What is Difference between Function and subroutine in perl ?
I found difference in few site, there i found Subroutine does not return value but function returns but actually Subroutine also return value .
Please let me know What is Exact difference between Function and subroutine in perl ?
Generally in computer science a function is a special type of subroutine that returns a values (as opposed to being called just for its side-effects). But in Perl (as the cookbook says) we don't make that distinction.
The two words mean the same thing. They're synonyms.
Course: Perlmonks.
Update:
They are synonyms only because Perl returns last value of expression, evaluated in sub-block.
Related
Background
When specifying formal parameters for a subroutine in perl, I am aware of the following notation. I'm not sure entirely what they mean, but form context clues and seeing other's explain their code, I have deduced this much:
sub method1($$){...} <-- Means it takes in two scalar parameters
sub method2(#){...} <-- Means it takes in a bunch of parameters as a hash
sub method3($#){...} <-- Menas it takes in a scalar parameter, then a bunch of other parameters as a hash.
However, I have also found this notation and am unaware of what it means:
sub method4(#;$)
Question
What, functionally, does the formal parameter declaration of #;$ do that #$ does not?
This feature is called prototypes, and it is not a formal parameter specification, but instead a ruleset for the parser of how to parse arguments passed to your subroutine, which also happens to do some rudimentary arity checking and coercion (sometimes in ways that will be surprising to the user). It is explained in that feature's documentation:
A semicolon (;) separates mandatory arguments from optional arguments. It is redundant before # or %, which gobble up everything else.
It is often simpler to not use prototypes at all than risk the confusion that they can cause. Some examples: a $ parameter will be forced into scalar context even if it is an array, and the prototype is ignored entirely when the subroutine is called as an object or class method (because it is not yet determined what subroutine will be called at the time the call is parsed).
For formal parameter specifications, use the signatures feature, or Function::Parameters (which currently has the benefit of being feature-complete - more features for signatures are coming soon).
I am new to Perl language, and in the code I am using there is this line :
$BASEDIR = &getcwd();
I am wondering why there is a & in front of the call to getcwd, and I could not find any reference about that. Could someone help me with this ?
Thanks a lot !
It causes the subroutine's prototype to be ignored.
$ perl -e'
sub f($#) { print("#_\n"); }
my #a = (4,5,6);
f(4,5,6);
f(#a);
&f(#a);
'
4 5 6
3
4 5 6
But that's probably not the reason it's used here. After all, Cwd::getcwd doesn't have a prototype. & can't be used on named operators (i.e. the functions of perlfunc, e.g. print), so some beginners use it on subroutines to distinguish them from operators. I believe this is a practice recommended by a popular Perl book.
Short answer is that it is optional unless you are running a really old version of perl.
For more detail see the perlsub man page (run perldoc perlsub or man perlsub depending on your environment)
A subroutine may be called using an explicit "&" prefix. The "&" is optional in modern Perl, as are parentheses if the subroutine has been predeclared. The "&" is not optional when just naming the subroutine, such as when it's used as an argument to defined() or undef(). Nor is it optional when you want to do an indirect subroutine call with a subroutine name or reference using the "&$subref()" or "&{$subref}()" constructs, although the "$subref->()" notation solves that problem. See perlref for more about all that.
I've inherited some Perl code and occasionally I see subroutines defined like this:
sub do_it($) {
...
}
I can't find the docs that explain this. What does the dollar symbol in brackets mean?
It is a subroutine prototype.
The single $ means that the sub will only accept a single scalar value, and will interpret other types using scalar context. For instance, if you pass an array as the param e.g. do_it(#array), Perl will not expand #array into a list, but instead pass in the length of the array to the subroutine body.
This is sometimes useful as Perl can give an error message when the subroutine is called incorrectly. Also, Perl's interpreter can use the prototypes to disambiguate method calls. I have seen the & symbol (for code block prototype) used quite neatly to write native-looking routines that call to anonymous code.
However, it only works in some situations - e.g. it doesn't work very well in OO Perl. Hence its use is a bit patchy. Perl Best Practices recommends against using them.
The ($) is called a subroutine prototype.
See the PerlSub man page for more information: http://perldoc.perl.org/perlsub.html#Prototypes
Prototyping isn't very common nowadays. Best Practice is not using it.
I´m newbie in perl and I need to define a subroutine in perl, but I don't understand the difference between subroutine and function.
When should I use any of them and how can I send parameters?
In the documentation, "function" refers to list operators (e.g. chr, print), named unary operators (e.g. chdir) and named nullary operators (e.g. time). These are sometimes called "builtin functions" to avoid ambiguity (though there are builtin subroutines too, such as utf8::upgrade).
In practice, "function" is commonly used to refer to both builtin functions and anything declared with sub.
Arguments are usually passed to subroutine as follows:
foo($x, $y)
Operators don't technically have parameters; they have operands. Most operators that qualify as functions resemble subroutines. perlfunc documents how to use each one.
There isn't a real difference. "Subroutine" is just a name for a function that you write, as opposed to one of Perl's builtin functions. It's okay to call them functions too.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does the function declaration “sub function($$)” mean?
sub t(&#) {
print #_;
}
t {print 1};
I tried to change &# to &$ and it will fail.
What's the lingo for it so that I can search?
&# is a subroutine prototype. This lets you create syntax similar to the builtin grep function (accepts a BLOCK and then a LIST). The LIST can even be the empty list: ().
&$ when used will force the second argument (which is mandatory) to be evaluated in scalar context. Since there is no second argument in t {print 1}; it will fail to compile.
Read more about subroutine prototypes at: perldoc perlsub.
I'm not quite clear on what you want the code to do, but you are creating a prototype for your sub, see the perldocs. The & means the sub t takes a block as the first argument and the # means the rest of the arguments are an array.
When you call your function, you are passing it one argument, the block {print 1} and that is what you are then printing out - the CODE reference as a string. The reason &$ fails is you are not passing a second argument. That is fine for &# as the second argument is the empty array.
The term you are looking for is "perl function prototypes".
"Function prototype", http://perldoc.perl.org/perlsub.html#Prototypes