CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Very N00B question - Predeclaring SUBs ?

 



Calab
Novice


Jul 9, 2012, 4:24 AM

Post #1 of 5 (1806 views)
Very N00B question - Predeclaring SUBs ? Can't Post

Should I predeclare the subroutines that I am using in my perl scripts? If so, what is the correct method to do so? What if the sub is expecting a parameter (or multiple parameters) to be passed to it?

Example:

Code
#!/usr/bin/perl 

use CGI::Carp qw( fatalsToBrowser );

sub SayHello(); # Print my HELLO message.

print "Content-type: text/html\n\n";
print "<html><head></head><body>";

print "Testing... ";

SayHello;

print "</body></html>";

exit;

sub SayHello () {
print "Hello!<br/>\n";
}



lukdk
Novice

Jul 9, 2012, 5:05 AM

Post #2 of 5 (1802 views)
Re: [Calab] Very N00B question - Predeclaring SUBs ? [In reply to] Can't Post

Hello,

I've never used predeclaraion of subs.

I always call my subs using something like &SayHello("HELLO WORLD");
You can pass variable(s) this way.


sub SayHello () {
my $parameter=$_;
print "$parameter<br/>\n";
}

it's also possible for arrays.
my @parameter = @_;


FishMonger
Veteran / Moderator

Jul 9, 2012, 8:11 AM

Post #3 of 5 (1798 views)
Re: [Calab] Very N00B question - Predeclaring SUBs ? [In reply to] Can't Post

Predeclaring the subs is almost never needed and the majority of the Perl community do not do it. It's considered by most to be "Best Practice" to not predeclare. The only time it's "needed" is when you want to call your sub like a Perl built-in command i.e., without using the parens or sigil such as when you want to write your own version of one of the built-in's and have it override the built-in.


FishMonger
Veteran / Moderator

Jul 9, 2012, 8:20 AM

Post #4 of 5 (1797 views)
Re: [lukdk] Very N00B question - Predeclaring SUBs ? [In reply to] Can't Post


Quote
I always call my subs using something like &SayHello("HELLO WORLD");

snip

Quote
sub SayHello () {


Please don't do it that way unless you understand what the side affect is and need that side affect.

It's best practice to not use & when calling a sub, just use the parens.

Prototype definitions, especially empty ones, are almost never needed or wanted.


Code
sub SayHello () { 
my $parameter=$_;

That's not doing what you think. The assignment should have been written as:

Code
my $parameter = shift;

or

Code
my $parameter = $_[0];



Calab
Novice


Jul 9, 2012, 9:39 PM

Post #5 of 5 (1782 views)
Re: [FishMonger] Very N00B question - Predeclaring SUBs ? [In reply to] Can't Post

Thanks for the reply. It's appreciated.

I didn't think it was necessary to pre-declare my subs, but I was seeing "called too early to check prototype" warning messages in Eclipse. Pre-declaring my subs seemed to be the best way to deal with the message.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives