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

This is really confusing. I make a sub routine call at line 1047 (add_client) and 1054 (get_Origserver). The add_client call works and has worked for months. The get_Origserver only compiles if I don't pass any variables or if I remove the parens in this line
sub get_Origserver() {

The code for the get_Origserver is literally copied from the add_client so it is technically the same.

1024 LABEL: 1025 print "\nCREATE A DATABASE FOR EXISTING CLIENT OR COPY AN EXIS +TING DATABASE? \n"; 1026 print "Create Existing Client Database => 1\n"; 1027 print "Create New Client Database C=> 2\n"; 1028 print "Copy Existing Database => 3\n"; 1029 chomp(my $answer = lc(<STDIN>)); 1030 print "$answer\n"; 1031 if ($answer eq "1") { 1032 1033 my $cltlist = display_client(\$dbhTarget); 1034 if ($cltlist) { 1035 $cltlist = "Existing clients: \n" . $cltlist . 1036 "\nEnter Client ID: "; 1037 print $cltlist; 1038 chomp(my $cltid = <STDIN>); 1039 $params->put("Client.ID", $cltid); 1040 }#end if statement 1041 1042 } elsif ($answer eq "2") { 1043 1044 print "Please enter new client name: \n"; 1045 chomp(my $clt = <STDIN>); 1046 my $passwd = $params->get("Prod.Password"); 1047 my $cltid = add_client(\$dbhTarget, $clt, $passwd); 1048 $params->put("Client.ID", $cltid); 1049 1050 } elsif($answer eq "3"){ 1051 1052 print "Please enter name of the database to copy: \n"; 1053 chomp(my $Origdb = <STDIN>); 1054 my $Origserver = get_Origserver(\$dbhTarget, $Origdb); 1055 print "$Origserver is the server for the original db\n"; 1056 1057 } else{ 1058 1059 print "YOU MUST CHOOSE AN OPTION!!!!\n"; 1060 redo LABEL; 1061 1062 }#end else statement
This works
sub add_client() { print "******ADDING ROW TO CLIENT TABLE******\n"; print "I AM HERE\n"; my ($dbh, $clt, $prdpasswd) = @_; my $count = 1; my $cltid; my $sql = "select distinct CLT_ID from ENRMASTER..CLT order by CLT_ID" +; my $sth = $$dbh->prepare($sql) or $app->error($FATAL, "can't prepare SQL statement [ $sql ] : +:$DBI::errstr"); $sth->execute(); }
This does not compile
sub get_Origserver() { 919 920 print "******GETTING SERVER NAME OF THE DB BEING COPIED******\ +n"; 921 922 my ($dbh, $Origdb) = @_; 923 print "This is the db $dbh\n"; }

Replies are listed 'Best First'.
Re: One sub routine call works a similar one does not.
by rdfield (Priest) on Nov 01, 2002 at 15:43 UTC
    Get rid of the parentheses after the sub name in the declaration. There's been a few threads regarding prototyping recently.

    rdfield

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: One sub routine call works a similar one does not.
by PodMaster (Abbot) on Nov 01, 2002 at 15:47 UTC
    You are using prototypes without understanding how/what they are. Please read `perldoc perlsub'.
    perl -le"sub NOARGS(){q,NONE,};print NOARGS();eval q{print NOARGS(1);} +;print$@"

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: One sub routine call works a similar one does not.
by thinker (Parson) on Nov 01, 2002 at 15:50 UTC
    Hi mnlight,

    I think you will find all you need to know here.
    Perhaps even far more than you needed to know. :-)
    I think conventional wisdom has it that prototypes should only be used when necessary. And that prototypes are almost never necessary. :-)

    hope this helps

    thinker
Re: One sub routine call works a similar one does not.
by BrowserUk (Patriarch) on Nov 01, 2002 at 16:19 UTC

    I'll admit it. I'm confused too. You say that you add_client() sub "works fine", but I don't undertsand how it could? I just tried to simulate your code and any attempt I made to call add_client(); with paramters caused the compile time error:

    Too many arguments for main::add_client at ... near "'password' )"

    This is exactly what I expected. Maybe I am missing something?

    #! perl -sw use vars qw($app $FATAL); use strict; $app = 'test'; $FATAL = 'FATAL'; sub add_client() { print "******ADDING ROW TO CLIENT TABLE******\n"; print "I AM HERE\n"; my ($dbh, $clt, $prdpasswd) = @_; my $count = 1; my $cltid; my $sql = "select distinct CLT_ID from ENRMASTER..CLT order by CLT +_ID"; my $sth = $$dbh->prepare($sql) or $app->error($FATAL, "can't prepare SQL statement [ $sql ] : +:$DBI::errstr"); $sth->execute(); } # 1047 my $cltid = add_client(\$dbhTarget, $clt, $passwd); add_client(); # the compiler accepts this but it wouldn't do anything +useful. add_client( 'handle', 'clt_id', 'password' ); ## << Line 25 __END__

    gives

    c:\test>209750 Too many arguments for main::add_client at C:\test\209750.pl line 25, +near "'password' )" Execution of C:\test\209750.pl aborted due to compilation errors. c:\test>

    Nah! Your thinking of Simon Templar, originally played by Roger Moore and later by Ian Ogilvy
Re: One sub routine call works a similar one does not.
by derby (Abbot) on Nov 01, 2002 at 18:01 UTC
    Not that you asked this question (or want an answer) but do you really want to do this:

    my $cltid = add_client(\$dbhTarget, $clt, $passwd); ... sub add_client() { ... my $sth = $$dbh->prepare($sql) ...

    Unless a *SCALAR* is holding a large amount of data, this is just unnecessary. In this case (if it's DBI), you all ready have a small, easy on the stack, reference.

    -derby