Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the function declaration "sub function($$)" mean?

I have been using Perl for some time, but today I came across this code:

sub function1($$)
{
   //snip
}

What does this mean in Perl?

like image 276
sud03r Avatar asked Mar 20 '10 22:03

sud03r


People also ask

How do I fix sub or Function not defined?

To correct this errorMake sure that the procedure name is spelled correctly. Find the name of the project containing the procedure you want to call in the References dialog box. If it does not appear, click the Browse button to search for it. Select the check box to the left of the project name, and then click OK.

What does () with sub Function do in VBA?

Both are sets of commands that are used to perform specific tasks in Microsoft Excel's Visual Basic Application (VBA). A sub, also known as a subroutine or sub procedure, is a piece of code that is used to perform a specific task mentioned in the code but does not return any kind of value.

What does sub mean in macro?

A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub statements that performs actions but doesn't return a value. A Sub procedure can take arguments, such as constants, variables, or expressions that are passed by a calling procedure.

What does sub or Function not defined mean in VBA?

“Sub or Function not Defined” is a compile error that occurs when VBA cannot find a procedure or other reference by name. A typo is the most common cause of this message.


2 Answers

It is a function with a prototype that takes two scalar arguments.


There are strong arguments for not actually using Perl prototypes in general - as noted in the comments below. The strongest argument is probably:

  • Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl

There's a discussion on StackOverflow from 2008:

  • SO 297034

There's a possible replacement in the MooseX::Method::Signatures module.

like image 156
Jonathan Leffler Avatar answered Nov 15 '22 07:11

Jonathan Leffler


As the other answer mentions, the $$ declares a prototype. What the other answer doesn't say is what prototypes are for. They are not for input validation, they are hints for the parser.

Imagine you have two functions declared like:

sub foo($)  { ... }
sub bar($$) { ... }

Now when you write something ambiguous, like:

foo bar 1, 2

Perl knows where to put the parens; bar takes two args, so it consumes the two closest to it. foo takes one arg, so it takes the result of bar and the two args:

foo(bar(1,2))

Another example:

bar foo 2, 3

The same applies; foo takes one arg, so it gets the 2. bar takes two args, so it gets foo(2) and 3:

bar(foo(2),3)

This is a pretty important part of Perl, so dismissing it as "never use" is doing you a disservice. Nearly every internal function uses prototypes, so by understanding how they work in your own code, you can get a better understanding of how they're used by the builtins. Then you can avoid unnecessary parentheses, which makes for more pleasant-looking code.

Finally, one anti-pattern I will warn you against:

package Class;
sub new ($$) { bless $_[1] }
sub method ($) { $_[0]->{whatever} }

When you are calling code as methods (Class->method or $instance->method), the prototype check is completely meaningless. If your code can only be called as a method, adding a prototype is wrong. I have seen some popular modules that do this (hello, XML::Compile), but it's wrong, so don't do it. If you want to document how many args to pass, how about:

sub foo {
    my ($self, $a, $b) = @_; # $a and $b are the bars to fooify
    ....

or

use MooseX::Method::Signatures;

method foo(Bar $a, Bar $b) { # fooify the bars
    ....

Unlike foo($$), these are meaningful and readable.

like image 21
jrockway Avatar answered Nov 15 '22 07:11

jrockway