4

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?

Community
  • 1
  • 1
new_perl
  • 7,345
  • 11
  • 42
  • 72

4 Answers4

5

&@ 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.

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
1

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.

cftarnas
  • 1,745
  • 10
  • 9
0

The term you are looking for is "perl function prototypes".

Hugh
  • 8,872
  • 2
  • 37
  • 42
0

"Function prototype", http://perldoc.perl.org/perlsub.html#Prototypes

tripleee
  • 175,061
  • 34
  • 275
  • 318