Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Prototyping - is it necessary?

by bioMan (Beadle)
on Aug 31, 2005 at 17:51 UTC ( [id://488167]=perlquestion: print w/replies, xml ) Need Help??

bioMan has asked for the wisdom of the Perl Monks concerning the following question:

I am playing around with modules, and have created a very simple test module. However, it has a bug.

package bioMan; use strict; use warnings; sub getFileData(){ #variables my @data = (); (my $datafile, $chomp) = @_; open FH, $datafile or die "Can't open $datafile: $!\n"; @data = <FH>; chomp(@data) if $chomp; close FH or die "Can't close $datafile: $!\n"; wantarray ? return @data : return(join '', @data); } 1;

I am running ActivePerl 5.8.7 on Win2000. When I used the above package in a short test program with the statement:

@seqarray = bioMan::getFileData($in, 1);

I got a "too many arguments" error that was rectified by modifying line 6 of the package to:

sub getFileData($$){

I didn't think prototyping was necessary. Am I wrong?

I want $chomp to be an optional parameter with a default value of undef, but prototyping forces me to explicitly include a value for $chomp whenever I call getFileData.

Retitled by g0n from 'prototyping'.

Replies are listed 'Best First'.
Re: Prototyping - is it necessary?
by gargle (Chaplain) on Aug 31, 2005 at 18:32 UTC

    Hi,

    Functions prototyped with () indicate functions that take no arguments at all. Even more, functions with () are candidates for inlining, meaning the compiler will look if the result of the function is a constant or lexically scoped scalar with no other references. If so, then the value will be used in place of calls (== quicker!).

    So, if you prototype with () and pass arguments when calling you'll receive a 'too many arguments error'.

    You can leave out the () or prototype with the correct number of arguments ($$).

    --
    if ( 1 ) { $postman->ring() for (1..2); }
Re: Prototyping - is it necessary?
by Thelonius (Priest) on Aug 31, 2005 at 17:59 UTC
    Just use
    sub getFileData {
    without parentheses after the function name.
Re: Prototyping - is it necessary?
by Codon (Friar) on Aug 31, 2005 at 22:24 UTC

    You have another bug in your code (or you transcribed it incorrectly):

    (my $datafile, $chomp) = @_;
    my is not going to apply to $chomp. Under strict, this should throw an error Global symbol "$chomp" requires explicit package name.

    Try this instead:

    my ($datafile, $chomp) = @_;
    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.

      Yes, I caught that bug after my post. Originally I had a statement before

      (my $datafile, $chomp) = @_;

      That read,

      my $chomp = 0;

      ... but I removed it knowing that if I could remove the prototyping and only pass in a value for $datafile then $chomp would automatically be set to undef. I forgot to move "my" outside the parentheses. Poof! a bug!

Re: Prototyping - is it necessary?
by jdporter (Paladin) on Aug 31, 2005 at 19:59 UTC
    I want $chomp to be an optional parameter with a default value of undef, but prototyping forces me to explicitly include a value for $chomp whenever I call getFileData.

    The correct prototype is ($;$).

    However, the code you posted doesn't even compile. Did you commit the sin of re-typing rather than copy-and-pasting?

      Did you commit the sin of re-typing rather than copy-and-pasting?

      Does this mean that the OP needs to do some "hail Larrys"?

      Update: Added original quote for context.

      --MidLifeXis

        No, I did a cut and paste, but as pointed out in another post I have another bug.

        (my $datafile, $chomp) = @_;

        throws an error because $chomp isn't explicitly declared/scoped and strict pitches a fit, "my" should be outside the parentheses.

Re: Prototyping - is it necessary?
by xdg (Monsignor) on Aug 31, 2005 at 18:01 UTC

    Try it without the parens:

    sub getFileData {

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Prototyping - is it necessary?
by polypompholyx (Chaplain) on Aug 31, 2005 at 20:17 UTC

    You might want to consider if you actually want to use prototypes at all: FMTYEWTK about prototypes. Most people use them because they think they do something that they don't...

Re: Prototyping - is it necessary?
by bravenmd (Sexton) on Aug 31, 2005 at 19:48 UTC
    try leaving the parens off the sub definition as it doesn't seem like you need them. so use:

    sub test_sub {
       #code
    }

    instead of:

    sub test_sub() {
       #code
    }
Re: Prototyping - is it necessary?
by geektron (Curate) on Aug 31, 2005 at 18:34 UTC
    if you're going to use the prototyping parens, you need to specify the type of prototype. otherwise, leave them off, as others have said. this:
    sub getFileData() { }
    is missing the prototype ... and that's why it won't work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://488167]
Approved by davido
Front-paged by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-28 17:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found