Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Function prototype

Reply
Thread Tools

Function prototype

 
 
pavunkumar
Guest
Posts: n/a
 
      08-06-2009

Dear Friends,

How can I declare the prototype for a function in
Perl.

Thanks




 
Reply With Quote
 
 
 
 
sln@netherlands.com
Guest
Posts: n/a
 
      08-06-2009
On Wed, 5 Aug 2009 22:02:11 -0700 (PDT), pavunkumar <(E-Mail Removed)> wrote:

>
>Dear Friends,
>
> How can I declare the prototype for a function in
>Perl.
>
>Thanks
>
>
>

On the top, put it on the top.

-sln
 
Reply With Quote
 
 
 
 
pavunkumar
Guest
Posts: n/a
 
      08-06-2009
On Aug 6, 5:45 pm, Tad J McClellan <(E-Mail Removed)> wrote:
> pavunkumar <(E-Mail Removed)> wrote:
> > How can I declare the prototype for a function in
> > Perl.

>
> 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
>
> --
> Tad McClellan
> email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      08-06-2009
pavunkumar wrote:
) 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.

Rewrite the function 'add' so that it can add any number of arguments.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
Ron Bergin
Guest
Posts: n/a
 
      08-06-2009
On Aug 6, 6:01*am, pavunkumar <(E-Mail Removed)> wrote:
> On Aug 6, 5:45 pm, Tad J McClellan <(E-Mail Removed)> wrote:
>
>
>
> > pavunkumar <(E-Mail Removed)> wrote:
> > > * How can I declare the prototype for a function *in
> > > Perl.

>
> > 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

>
> > --
> > Tad McClellan
> > email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

>
> 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.


The args passed to the sub are in the @_ array. So, just check the
number of elements in the array then print your warning and exit the
sub if the count isn't what you want.
 
Reply With Quote
 
Ted Zlatanov
Guest
Posts: n/a
 
      08-06-2009
On Thu, 06 Aug 2009 08:48:04 -0500 Tad J McClellan <(E-Mail Removed)> wrote:

TJM> pavunkumar <(E-Mail Removed)> wrote:

>> 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.


TJM> sub add {
TJM> die "add() must be called with exactly 2 arguments\n" unless @_ == 2;
TJM> ...

On Thu, 6 Aug 2009 06:43:18 -0700 (PDT) Ron Bergin <(E-Mail Removed)> wrote:

RB> The args passed to the sub are in the @_ array. So, just check the
RB> number of elements in the array then print your warning and exit the
RB> sub if the count isn't what you want.

He specifically asked for compile-time checks.

OP: look at `perldoc perlsub' to learn about prototypes. Here are some
examples from the documentation (you probably want the first example):

Declared as Called as

sub mylink ($$) mylink $old, $new
sub myvec ($$$) myvec $var, $offset, 1
sub myindex ($$;$) myindex &getstring, "substr"
sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
sub myreverse (@) myreverse $a, $b, $c
sub myjoin ($@) myjoin ":", $a, $b, $c
sub mypop (\@) mypop @array
sub mysplice (\@$$@) mysplice @array, @array, 0, @pushme
sub mykeys (\%) mykeys %{$hashref}
sub myopen (*;$) myopen HANDLE, $name
sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
sub mygrep (&@) mygrep { /foo/ } $a, $b, $c
sub myrand (;$) myrand 42
sub mytime () mytime

Prototypes have many flaws, but they *are* a Perl feature.

Ted
 
Reply With Quote
 
Graham Drabble
Guest
Posts: n/a
 
      08-06-2009
On 06 Aug 2009 Tad J McClellan <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> pavunkumar <(E-Mail Removed)> wrote:


>> 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.

>
>
> sub add {
> die "add() must be called with exactly 2 arguments\n"
> unless @_ == 2; ...


He does actually say he wants it done at compile time and your solution
works at run time.

I suspect that it's not possible to do what he wants but would love to
be proven wrong.

--
Graham Drabble
http://www.drabble.me.uk/
 
Reply With Quote
 
Charlton Wilbur
Guest
Posts: n/a
 
      08-06-2009
>>>>> "TZ" == Ted Zlatanov <(E-Mail Removed)> writes:

TZ> On Thu, 06 Aug 2009 08:48:04 -0500 Tad J McClellan
TZ> <(E-Mail Removed)> wrote:

TJM> pavunkumar <(E-Mail Removed)> wrote:

>>> 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.


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
TZ> <(E-Mail Removed)> wrote:

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.

Charlton


--
Charlton Wilbur
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      08-06-2009
On 2009-08-06 15:50, Charlton Wilbur <(E-Mail Removed)> wrote:
>>>>>> "TZ" == Ted Zlatanov <(E-Mail Removed)> writes:

>
> TZ> On Thu, 06 Aug 2009 08:48:04 -0500 Tad J McClellan
> TZ> <(E-Mail Removed)> wrote:
>
> TJM> pavunkumar <(E-Mail Removed)> wrote:
>
> >>> 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.

>
> 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
> TZ> <(E-Mail Removed)> wrote:
>
> 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?


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.


> 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.


Prototypes are a part of "how Perl works". I'd call denying the
existence of prototypes a fundamental miscomprehension of how Perl
works.

hp

 
Reply With Quote
 
Alan Curry
Guest
Posts: n/a
 
      08-07-2009
In article <(E-Mail Removed)>,
Peter J. Holzer <(E-Mail Removed)> wrote:
>
>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.


Once upon a time, I tried something like this, honestly expecting it to work:

sub foo ($$$$);

if($something) {
@args=($a, $b, $c, 0);
} else {
@args=($x, $y, $z, 1);
}

foo(@args);

It's easy to see at compile time that @args will always have the proper
number of elements to satisfy the prototype. But prototypes don't do that,
and furthermore there's nothing in perl that does do that, so you have to
live with run-time assertions. Oh well.

--
Alan Curry
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype WTP 0.2 released,this release for Prototype 1.6.0 javascript fish Javascript 0 10-11-2008 07:35 AM
Class prototype vs C function prototype June Lee C++ 2 04-13-2008 08:17 PM
function prototype vs function declaration Ravishankar S C Programming 44 12-23-2007 11:40 AM
Prototype Object.extend(new Base() | Hash | Hash.prototype) usage: jacobstr@gmail.com Javascript 3 03-27-2007 07:56 AM
relation between prototype and Prototype.js shypen42@yahoo.fr Javascript 9 05-26-2006 01:13 AM



Advertisments