Click to See Complete Forum and Search --> : Perl Prototyping


Jay5
11-27-2003, 10:37 PM
I'm finally getting to a point where I've got a good libary of these Perl scripts.

I want to start getting some organization and I need to check my understanding of prototyping and passing parameters to subroutines.

I think what makes most sense is to have routines in specific files and just "require" the ones I need to perform specific tasks for a certain project - noble idea, nothing too crazy I think.

So I've got a main PL with this code in it for example

#!/usr/bin/perl
require "utility.pl";
$variable = &someUtilFunction;
#end of program


and my utility.pl would be

#!/usr/bin/perl
sub someUtilFunction
{
return $someVariable;
} 1;
#end of program

I'm pretty fine with everything above, but problem I have is now I get to a point where I want to feed these subroutines parameters so they do different things and do something based on what I pass in and I stumble onto this area of prototyping which I'm not sure how it fits in.

I'm aware of the convienance operators such as $_ and @_ might make this whole topic mute. But for my purposes I want to be specific, with no short cuts - I get alot of pans on the stove at one time and if I have to leave one thing, move onto another, and then come back to this I don't want to be guessing about what I'm really passing back and forth.

So if I were to want to pass parameters into my utility function above I guess I would make the following changes.

#!/usr/bin/perl
require "utility.pl";
$variable = &someUtilFunction($parameter1, $parameter2);
#end of program


and my utility.pl would then change to

#!/usr/bin/perl
sub someUtilFunction($$)
{
return $someVariable;
} 1;
#end of program

Does "require" take care of the prototyping question and does this make everything nicey, nice for Perl?

Scriptage
12-01-2003, 09:14 AM
simple as:

Main:

#!/usr/bin/perl -w

use strict;
require "utility.pl";

my $variable = timesTen('50');

print $variable;

exit(0);

Utility:

#!/usr/bin/perl -w

use strict;

sub timesTen{
return $_[0] * 10;
}

1;

Scriptage
12-01-2003, 09:21 AM
By the way this isn't really a prototype because you aren't creating a new instance of the object. All this code does is require pieces of code from another perl file and incorporate it into itself, allowing you to use its subroutines etc.

To prototype you need to learn OOPerl ie, my $window = MainWindow->new(); then the variable $window becomes an instance of the MainWindow class.


Regards

Jeff Mott
12-01-2003, 10:58 AM
http://www.perldoc.com/perl5.8.0/pod/perlsub.html#Prototypes