Subroutine arguments in perl [duplicate] - perl

This question already has answers here:
What does the function declaration "sub function($$)" mean?
(2 answers)
Closed 7 years ago.
I have been browsing a few of the perl modules where they have used $$%, $$, $$$, #, $%, #.. and so on in the function arguments.
I understand that $$ in the argument ensures that you have to pass 2 non-optional parameters and $ for 1 non-optional parameter. Do the others have similar meaning as well?

These are function prototypes. Everything you need to know about prototypes is in perlsub.
Before you read it a second time, read all of the answers in Why are Perl 5's function prototypes bad? and Far More Than Everything You've Ever Wanted to Know about Prototypes in Perl and see if it disabuses you of the notion to use prototypes.

Related

Why use &getcwd() and not getcwd() in Perl?

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.

Perl 6 - Subroutine taking "Bareword" input

So I've been diving into Perl 6 and have been looking at interpreting another language using Perl 6's operator definitions. I understand that this could be done by parsing the code but I'm looking to push Perl 6's capabilities to see what it can do. Having this functionality would also make the parsing a lot easier
I'm trying to make a variable definition in a C-style format.(The language isn't important)
Something like:
char foo;
Where the char represents the type and the foo is the variable name. From my understanding the char can be interpreted using an operator definition like so:
sub prefix:<char>($input) {
say $input;
}
Which calls a subroutine with the foo as $input. The idea from here would be to use foo as a string and hold it's reference in a hash somewhere. The problem with this is that Perl 6 seems to see any bareword as a function call and will complain when it can't find the "Undeclared routine".
I've looked possibly everywhere for an answer to this and the only thing that makes me still think that this may be possible is the qw function from Perl 5 which is now < > in Perl 6. The < > is obviously an operator which leads me to believe that there is a subroutine defined somewhere that tells this operator how to work and how to deal with the bareword input.
So to my question:
Is there a way of accepting bareword input into a subroutine just like the < > operator does?
Cheers!
The best way to do that would be to create a Grammar that parses your language. If you additionally want it to run the DSL you have just created, combine it with Actions.

Strange perl syntax ( $$ ) in sub routine [duplicate]

This question already has answers here:
What does the function declaration "sub function($$)" mean?
(2 answers)
Closed 7 years ago.
What dose the ( $$ ) do in this code. I have programmed Perl for a long time but never came across this syntax until recently when I opened a very old Perl .plx file
These rows prevent me from upgrading to a more modern Perl version.
sub help( $$ ){
}
The reason it affects me is because I get an error message stating that the help function was called before it was declared. Any idea of how I can solve this without removing the ( $$ ) block??
The are called prototypes. This particular one says that the sub routine expects to be called with exactly 2 scalar variables. Although prototypes are sometimes useful, mostly they are not.
If you can drop them depends on the rest of the code...
That's a function prototype, which is used to specify the number and types of arguments that the subroutine takes. See the documentation.
Since it's in the current documentation, I don't see why it's preventing you from upgrading.
Is the error you're getting help called too early to check prototype? Here's the explanation from the perldiag documentation:
(W prototype) You've called a function that has a prototype before the parser saw a definition or declaration for it, and Perl could not check that the call conforms to the prototype. You need to either add an early prototype declaration for the subroutine in question, or move the subroutine definition ahead of the call to get proper prototype checking. Alternatively, if you are certain that you're calling the function correctly, you may put an ampersand before the name to avoid the warning. See perlsub.
It's a prototype. The $$ specifies that the help function expects two arguments and that they should each be evaluated in scalar context. Note that this does not mean that they are scalar values! Perl's prototypes aren't like prototypes in other languages. They allow you to define functions that behave like built-in functions: parentheses are optional and context is imposed on the arguments.
sub f($$) { print "#_\n" }
my #a = ('a' .. 'c');
f(#a, 'd'); # prints "3 d"
I'm guessing that the error message you're seeing is
help() called too early to check prototype
which means that Perl saw a call to the function before it saw the declaration of the function and knew about the prototype. This means that the prototype wasn't enforced and the call may not behave as expected.
my #a = ('a' .. 'c');
f(#a, 'd'); # prints "a b c d"
sub f($$) { print "#_\n" }
To fix the error you need to either move the subroutine definition before the call, or add a declaration before the call.
sub f($$); # forward declaration
my #a = ('a' .. 'c');
f(#a, 'd'); # prints "3 d"
sub f($$) { print "#_\n" }
All of this should have absolutely nothing to do with your ability to upgrade to a newer version of Perl.

What does &# mean for sub? [duplicate]

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

What is "1;" in a Perl source? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does 1; mean in Perl?
I'm new to Perl and learning how to build a class with Perl.
As from this example:
http://www.tutorialspoint.com/perl/perl_oo_perl.htm, I see a line which is written as "1;"
just can't find information about this interesting line from perldoc.perl.org
Do you know what it is? And why is it there in the Perl source code?
A module is normally a bunch of subroutine definitions, but it can also include code that is not in a subroutine (such as initialisation code). This code might fail, so Perl lets you indicate this by returning false whereupon Perl aborts with an error.
However, since the default return value is false, we must explicitly return true at the end of a module.
The perldocs have this to say:
The file must return true as the last statement to indicate successful execution of any initialization code, so it's customary to end such a file with 1; unless you're sure it'll return true otherwise. But it's better just to put the 1; , in case you add more statements