Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference about subroutine has the * prototype between Perl 5.22 and before?

Tags:

perl

I read the Perl document about Changes to the * prototype

Changes to the * prototype
The * character in a subroutine's prototype used to allow barewords to take precedence over most, but not all, subroutine names. It was never consistent and exhibited buggy behavior.

Now it has been changed, so subroutines always take precedence over barewords, which brings it into conformity with similarly prototyped built-in functions

Code example:

sub splat(*) { ... }
sub foo { ... }
splat(foo); # now always splat(foo())
splat(bar); # still splat('bar') as before
close(foo); # close(foo())
close(bar); # close('bar')

Can anyone explain to me the difference? Thank for any help.

like image 654
Canon Avatar asked Sep 19 '21 13:09

Canon


People also ask

What are prototypes in Perl?

A Perl function prototype is zero or more spaces, backslashes, or type characters enclosed in parentheses after the subroutine definition or name. A backslashed type symbol means that the argument is passed by reference, and the argument in that position must start with that type character.

What is subroutine in Perl?

A Perl subroutine or function is a group of statements that together performs a task. You can divide up your code into separate subroutines. How you divide up your code among different subroutines is up to you, but logically the division usually is so each function performs a specific task.

How do you call a subroutine from another file in Perl?

If you have subroutines defined in another file, you can load them in your program by using the use , do or require statement. A Perl subroutine can be generated at run-time by using the eval() function. You can call a subroutine directly or indirectly via a reference, a variable or an object.

How do I return a value from a subroutine in Perl?

You can return a value from Perl subroutine as you do in any other programming language. If you are not returning a value from a subroutine then whatever calculation is last performed in a subroutine is automatically also the return value.


Video Answer


1 Answers

All that perldelta entry is stating is that when foo() is a predefined function then, prior to 5.22, the call splat(foo) might have been interpreted by the parser as either splat(foo()) or splat('foo'), but you couldn't easily tell which. Now it will always be seen as splat(foo()).

like image 155
Dave Mitchell Avatar answered Oct 27 '22 16:10

Dave Mitchell