Strange perl ($$) syntax in subroutine

What dose ( $$ ) to make in this code. I programmed Perl for a long time, but until recently I did not come across this syntax when I opened a very old Perl.plx file

These lines do not allow me to upgrade to a more modern version of Perl.

 sub help( $$ ){ } 

The reason this affects me is because I get an error message stating that the help function was called before it was declared. Any idea on how I can solve this problem without deleting the block ($$)

+4
source share
3 answers

This is a function prototype that is used to indicate the number and types of arguments that a routine performs. See the documentation.

Since this is in the current documentation, I do not understand why this is preventing you from updating.

Are you getting the help called too early to check prototype error message? Here is an explanation from perldiag :

(W prototype) You called a function that has a prototype before the parser sees a definition or declaration for it, and Perl was unable to verify that the call matches the prototype. You need to either add an early prototype declaration for the subroutine in question, or transfer the definition of the subroutine before the call to get a proper prototype check. Alternatively, if you are sure that you are calling the function correctly, you can put an ampersand in front of the name to avoid a warning. See Perlsub.

+7
source

They are called prototypes . This particular one says that the subroutine expects exactly 2 scalar variables to be called. Although prototypes are sometimes useful, this is mostly not the case.

If you can remove them, it depends on the rest of the code ...

+8
source

This is a prototype . $$ indicates that the help function expects two arguments, and each of them should be evaluated in a scalar context. Please note that this does not mean that they are scalar values! Perl prototypes are not like prototypes in other languages. They allow you to define functions that behave as built-in functions: brackets are optional, and the context is superimposed on the arguments.

 sub f($$) { print "@_\n" } my @a = ('a' .. 'c'); f(@a, 'd'); # prints "3 d" 

I assume the error message you see is

 help() called too early to check prototype 

which means that Perl saw the function call before he saw the function declaration and knew about the prototype. This means that the prototype has not been applied, and the call may not behave as expected.

 my @a = ('a' .. 'c'); f(@a, 'd'); # prints "abcd" sub f($$) { print "@_\n" } 

To fix the error, you need to either move the subroutine definition before the call, or add an announcement before the call.

 sub f($$); # forward declaration my @a = ('a' .. 'c'); f(@a, 'd'); # prints "3 d" sub f($$) { print "@_\n" } 

All this should have absolutely nothing to do with your ability to upgrade to a newer version of Perl.

+2
source

Source: https://habr.com/ru/post/1441992/


All Articles