in reply to How can I use the values which is passed as arguments?
You seem to have an answer to your question, but moving forward, there are some things to keep in mind with prototypes, namely that: Perl prototypes are a guide to the compiler, not a list of expected types (as in Java and other languages).
In general you should stay away from them unless
All other uses are likely not a good idea. For example, you can use prototypes to force the compiler to fail unless a certain general category of variable is passed. However, to do that, you will have to go against normal ideas of how parameters are used. If you do use prototypes for this purpose, document it very carefully. Even then people are likely to mess things up.
Suppose, for example, you are defining a function (not method) and you have a specific reason for insuring that the parameter being passed is an lvalue (i.e. something to which a value can be assigned, like a variable). If function foo is defined sub foo(\$) then the call foo(1) will fail to compile with the complaint: "Type of arg 1 to main::foo must be scalar (not constant item)". Only calls like foo($x) will be accepted by the compiler.
However, the normal Perl convention is to copy parameters to named variables, so users reading the calling code may not be expecting that foo($x) is actually passing \$a (a reference to $a) and is going to change the value of $x in some way. If you had forgone the prototype and passed foo(\$x) instead, readers seeing the function call would know that something might be happening to the value of $x and would take special care.
Best, beth
|
|---|