P
pavunkumar
Dear Friends,
How can I declare the prototype for a function in
Perl.
Thanks
How can I declare the prototype for a function in
Perl.
Thanks
On the top, put it on the top.Dear Friends,
How can I declare the prototype for a function in
Perl.
Thanks
It is exceedingly likely that you do NOT want to declare a
prototype for a function.
What is it that you hope to accomplish by using a prototype?
See why Perl's prototypes are nearly always not what you are looking for:
http://www.perl.com/language/misc/fmproto.html
Dear Sir,
I want to write a function , that should accept specific
number of arguments .
example :
I have a function called "add" , I wrote definition for that adding
two numbers,which I am going to pass as a argument. In this case if I
pass more than two argument, compile has to say error. Actually it is
not saying . So How can I achieve this one.
Thanks.
sub add {
die "add() must be called with exactly 2 arguments\n"
unless @_ == 2; ...
TZ> On Thu, 06 Aug 2009 08:48:04 -0500 Tad J McClellan
TJM> sub add { die "add() must be called with exactly 2 arguments\n"
TJM> unless @_ == 2; ...
TZ> On Thu, 6 Aug 2009 06:43:18 -0700 (PDT) Ron Bergin
RB> The args passed to the sub are in the @_ array. So, just check
RB> the number of elements in the array then print your warning and
RB> exit the sub if the count isn't what you want.
TZ> He specifically asked for compile-time checks.
And how do you propose that the compiler can check that?
If the compiler sees
my $sum = add(some_function());
how is it supposed to know that some_function() will return exactly two
elements in a list, for instance?
TZ> Prototypes have many flaws, but they *are* a Perl feature.
In this case, however, they're a poor attempt to work around a
fundamental miscomprehension of how Perl works. Better to fix the
miscomprehension than to patch over it with prototypes.
With a suitable prototype in scope, it knows that some_function() will
return exactly one element, since it is called in scalar context.
Yes, that means that with a prototype
sub add($$);
this
my $sum = add(some_function());
cannot work, because the compiler will notice at compile time that add
has only one argument, not two, and will abort the compilation. This may
be a problem or it may be what the OP wants.
Charlton said:TZ> He specifically asked for compile-time checks.
And how do you propose that the compiler can check that?
If the compiler sees
my $sum = add(some_function());
how is it supposed to know that some_function() will return exactly two
elements in a list, for instance?
See why Perl's prototypes are nearly always not what you are looking
for:
http://www.perl.com/language/misc/fmproto.html
pavunkumar said:I want to write a function , that should accept specific
number of arguments .
Perl's prototypes do not do that...
Quoth (e-mail address removed):
If you have 5.10, you can use the '_' prototype character for that
behaviour.
There's no need for the '\': $_[0] is passed by reference.
You missed the magic 'goto &name', the arg-less '&name' that reuses the
current @_
Quoth (e-mail address removed):There's no need for the '\': $_[0] is passed by reference.
Not if the actual argument is an expression. Then Perl creates a
temporary variable and passes a reference to that, so any assignments
to it are silently discarded after the sub ends.
Not true. Perl passes an alias to the result of the expression, so you
can perfectly well assign to $_[0] and change the variable passed:
~% perl -E'sub foo ($) { $_[0] = 2 } my $x; foo($x); say $x'
2
~% perl -E'sub foo ($) { $_[0] = 2 } foo(1); say $x'
Modification of read-only value attempted at -e line 1.
(the ($) prototype has no effect in any of these cases).
Array and hash elements *are* allowed,
pavunkumar said:I want to write a function , that should accept specific
number of arguments .
example :
I have a function called "add" , I wrote definition for that adding
two numbers,which I am going to pass as a argument. In this case if I
pass more than two argument, compile has to say error. Actually it is
not saying . So How can I achieve this one.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.