This question already has answers here:
When should I use the & to call a Perl subroutine?
(4 answers)
Closed 9 years ago.
Every now and then I see Perl scripts where subroutines are called with a leading '&'.
Is this legacy, or does it give any benefit?
As calling the subroutine without the ampersand sign works as well.
sub mysub {
print "mysub\n";
}
mysub;
&mysub;
Thx/Hermann
Calling with & is generally a code smell that somebody doesn't know what they're doing and are in a Perl4 mindset. In your specific example, it works exactly the same. However, calling with & disables function prototypes, so advanced users may use it in certain circumstances. You should expect to see a comment why next to the call in that case.
Related
This question already has answers here:
Can I dynamically get a list of functions or function names from any Perl module?
(3 answers)
How do I list available methods on a given object or package in Perl?
(5 answers)
List all the subroutine names in perl program
(3 answers)
Get list of methods/functions defined explicitly in a module
(1 answer)
Closed 6 months ago.
First of all, I am entirely new in perl so I apologize if this is a rather basic question.
We have a perl script that calls on a module — lets name it module_x
It’s called in the script using this line.
require module_x
Sadly, this does not have a documentation and we can’t access the server where this script is.
Is there a way to know what functions are available in the module? Similar to desc in python.
This question already has answers here:
How can I inline Perl subroutines?
(4 answers)
Closed 7 years ago.
I know non-constant subroutines usually will not be inlined, since they can be redefined on the fly. However, I have code where inlined subroutines would actually offer a small but non-trivial optimization; but I don't want to unroll them myself since it would make the code much harder to read.
Is there some way to make Perl inline these methods, a way to indicate that the subroutine will not be modified at runtime and I want the interpreter to inline during the preprocessing phase?
Constants can be folded, but Perl subs are never inlined. Practically, they can't be. macro and Macro attempt to provide inlinable subs, but I don't know how reliable they are. You will definitely find limitations.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Suppressing a function’s command window output
Suppress Output
Is there a way to "silence" the output of a Matlab function? In other words, if a function generates some displayed text in the command window, is there a way to run it in a quiet mode, where the output is suppressed?
In my case, I am using a third-party function iteratively that displays a lot of text, and I want to find a way to suppress that text without modifying the function itself. I'm thinking there must be some kind of wrapper function like quiet(thirdpartyFunction) that gives this kind of behavior. Or is this wishful thinking?
You can probably use evalc and discard the return value.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When should I use the & to call a Perl subroutine?
In perl scripts, why is that method of invoking the function is written differently some times. I see &function and sometimes function(). Are they both the same and this is just a style that one would want to flaunt? If they are the same, why are they both available, would not one just suffice? I am guessing there is some semantic difference between the both the methods which distinguishes them from each other ... but at what kind of circumstances?
--- Since I cannot answer my own question for timeout reasons -- I am updating the answer in the section of question itself. When I get a chance to update the answer block, I will put it there.
I found the relevant text in 'Learning Perl' book..thanks for the tip though. Chapter 4: Subroutines -- Omitting the Ampersand.
I was more interested in ampersand & usage for perl functions. If a subroutine is already defined before being invoked, then subroutine can be invoked without using & while calling the function similar to invoking the builtin functions. & is also used to distinguish between the builtin functions and the user defined functions if the function to be invoked uses the same name that of one of the builtin function, provided it is defined before being invoked.
Usage of (), is merely to justify the passing of the arguments to the subroutines, while if not used, the default list of current arguments are passed in the form #_. If the arguments are specified in () for a subroutine, it is assumed to be a subroutine and is invoked even if not previously defined while parsing.
It has very specific uses:
& tells Perl to ignore the sub's prototype.
& can allow you to use the caller's #_. (&foo; with no parens or arguments).
goto &foo; and defined &foo.
Getting a reference (e.g. \&foo).
Some people (mostly beginners) use it to distinguish user subs from builtin functions, since builtin functions cannot be preceded by &.
As mentioned by #manatwork and #KeithThompson you can find information in these articles:
A general description - What's the difference between calling a function as &foo and foo()?
Subtle information about using & or not for a function call - perlsub: Perl Subroutines: Description.
This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How do I implement dispatch tables in Perl?
I have a hash table that contains commands such as int(rand()) etc.
How do I execute those commands?
You can use eval($str) to execute Perl code you store in a string variable, $str. You could alternatively store your code as function references within a hash, so something like:
$hash{'random'} = sub { int(rand()) };
This way, you could write $hash{'random'}->() to execute the function whenever you want a random value.
See also Implementing Dispatch Tables on PerlMonks.
As other have said, you can execute them using eval. However, please note that executing arbitrary strings of possibly tainted origin via eval is a major security hole, as well as prone to be slow if performance of your application matters.
You can use the Safe module to remove the security hole (not sure how bulletproof that is but much better than naked eval), but performance issues will always be there as Perl will have to compile your code prior to executing it WHILE executing the main program.