Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Creating table DBI

by Win (Novice)
on Nov 10, 2003 at 17:28 UTC ( [id://305918]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks,

The following code works well.

#! perl -w scipt use strict; use DBI; use DBD::ODBC; my ( $data_source, $database, $user_id, $password ) = qw( ip_adddreaa +First natt silver ); #not real my $conn_string = "driver={SQL Server}; Server=$data_source; Database= +$database; UID=$user_id; PWD=$password"; my $dbh = DBI->connect( "DBI:ODBC:$conn_string" ) or die $DBI::errstr; my $user_specified_table_name = "Doncaster_Deaths 1975_onwards_slim_D" +; create_table($user_specified_table_name); sub create_table ($) { my($table_name) = $_; if (my $table_name eq /Doncaster_Deaths_1975_onwards_slim_D/){ my $sql = qq{ CREATE TABLE Doncaster_Deaths_1975_onwards_slim_D (y +ear_of_death int NOT NULL, ageyear +s int NOT NULL, sex int + NULL, undcaus +e varchar(5) NULL, oacode +varchar(10) NULL, )}; $dbh->do($sql); $dbh->disconnect(); } }
C:\Documents and Settings\Matthew\2003\Perl\November\7_11_03>perl Crea +ting_tabl e_B.pl main::create_table() called too early to check prototype at Creating_t +able_B.pl line 14. Use of uninitialized value in pattern match (m//) at Creating_table_B. +pl line 22 . Use of uninitialized value in string eq at Creating_table_B.pl line 22 +.

Can anyone explain the messages?

Replies are listed 'Best First'.
Re: Creating table DBI
by Abigail-II (Bishop) on Nov 10, 2003 at 17:35 UTC
    Yes. You have a function with a prototype, however, that prototype is found *after* the call to the function. Since checking a prototype is a compile time action, you ask Perl to check something without first saying what it should check against.

    Predeclare your function, put a

    sub create_table ($);
    at the top of your program.

    Abigail

Re: Creating table DBI
by jeffa (Bishop) on Nov 10, 2003 at 20:28 UTC
Re: Creating table DBI
by PodMaster (Abbot) on Nov 11, 2003 at 07:58 UTC
    Yes, diagnostics can:
    #!/usr/bin/perl -w use diagnostics; crash(); sub crash($$){die@_} __END__ main::crash() called too early to check prototype at file.pl line 3 (# +1) (W prototype) You've called a function that has a prototype before + the parser saw a definition or declaration for it, and Perl could not +check that the call conforms to the prototype. You need to either add a +n early prototype declaration for the subroutine in question, or mov +e the subroutine definition ahead of the call to get proper prototype checking. Alternatively, if you are certain that you're calling t +he function correctly, you may put an ampersand before the name to av +oid the warning. See perlsub. Died at file.pl line 4 (#2) (F) You passed die() an empty string (the equivalent of die "") or you called it with no args and both $@ and $_ were empty. Uncaught exception from user code: Died at file.pl line 4. main::crash() called at file.pl line 3

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Creating table DBI
by iburrell (Chaplain) on Nov 10, 2003 at 20:36 UTC
    Abigail pointed out how to fix the problem with prototypes. The other way is to not use prototypes. I went through a phase after I discovered them where I used prototypes everywhere. Then I discovered the problems that they can cause like this one.

    The current wisdom is that prototypes should only be used where they are required and not just for checking arguments. One place they are required is to make functions work like Perl operators. Another case I just ran into is that mod_perl use prototypes to figure out which functions are handler methods.

Re: Creating table DBI
by htoug (Deacon) on Nov 11, 2003 at 11:20 UTC
    The other messages come from the if line in
    sub create_table ($) { my($table_name) = $_; if (my $table_name eq /Doncaster_Deaths_1975_onwards_slim_D/){
    it's a wierd onstruction: you declare a local variable (unitialised) and compare it to the result of matching $_ against Doncaster_etc.

    You probably meant to compare the argument of create_table to the string "Doncaster...etc...". Please note that the construction /regex/ is a shorthand for m/regex/ that conares the value of $_ to the regular expression. Look at perldoc perlre for more information.

    Try something like:

    sub create_table ($) { my($table_name) = $_; if ($table_name eq "Doncaster_Deaths_1975_onwards_slim_D"){
    and the last two messages should disappear.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found