Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Uninitialized value and a dot between $schema and $table

by tukusejssirs (Beadle)
on Aug 08, 2019 at 20:59 UTC ( #11104194=perlquestion: print w/replies, xml ) Need Help??

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

I try to run the following code
use warnings; use strict; use autodie; my $databas = "db"; my $password = "pass"; my $user = "postgres"; $dsn = "DBI:Pg:dbname = $database"; $dbh = DBI->connect($dsn, $user, $password, { RaiseError => 1 }) or di +e "ERROR: The database could not be opened."; create_table($dbh, $schema, $table, "col1 char(20)"); sub create_table { # my ($dbh, $schema, $table, $cols_type) = @_; my $dbh = $_[0]; my $schema = "$_[1]"; my $table = "$_[2]"; my $cols_type = "$_[3]"; $dbh->do("create table $schema.$table ($cols_type);"); # Hear it +happens; line 142 return; }
It warns me: Use of uninitialized value $cols_type in concatenation (.) or string at ./test6.pl line 142. How can I prevent this error?

Replies are listed 'Best First'.
Re: Uninitialized value and a dot between $schema and $table
by stevieb (Canon) on Aug 08, 2019 at 21:13 UTC
    # my ($dbh, $schema, $table, $cols_type) = @_; my $dbh = $_[0]; my $schema = "$_[1]"; my $table = "$_[2]"; my $cols_type = "$_[3]";

    First, stop using the parameter array like this, use your first incantation: my (...) = @_; is how you'd do it with numerous parameters.

    Second, I think I saw something in a previous post that stated to stop double-quoting things ("$_[3]") unnecessarily. Take that to heart.

    Is there any chance you could state an entire use case of what you're trying to achieve in a single example, along with some input data? That'd help the Monks a lot.

    ...or is this something you're just fooling with? (This almost seems like deja-vu for some reason).

      (1) I already try to use my (...) = @_; in my scripts. :)

      (2) I try to remember what I should/shouldnít quote. I took it to heart, but it takes some timeóIím a slow learner. ;)

      (3) Ö entire use case Ö → I kind of canít because it is a work in progress. I try to make it work by myself (and/or by searching the web), but when I cannot fix the errors/warnings by myself I come here to seek the Wisdom. ;)

Re: Uninitialized value and a dot between $schema and $table
by holli (Abbot) on Aug 08, 2019 at 21:13 UTC
    This is what you've seen if you had actually used strict instead of just pretending to.
    Global symbol "$dsn" requires explicit package name (did you forget to + declare "my $dsn"?) at pm13.pl line 9. Global symbol "$database" requires explicit package name (did you forg +et to declare "my $database"?) at pm13.pl line 9. Global symbol "$dbh" requires explicit package name (did you forget to + declare "my $dbh"?) at pm13.pl line 10. Global symbol "$dsn" requires explicit package name (did you forget to + declare "my $dsn"?) at pm13.pl line 10. Global symbol "$dbh" requires explicit package name (did you forget to + declare "my $dbh"?) at pm13.pl line 12. Global symbol "$schema" requires explicit package name (did you forget + to declare "my $schema"?) at pm13.pl line 12. Global symbol "$table" requires explicit package name (did you forget +to declare "my $table"?) at pm13.pl line 12. Execution of pm732.pl aborted due to compilation errors.


    holli

    You can lead your users to water, but alas, you cannot drown them.

      (1) I donít just pretend, I use use strict;.

      (2) Yeah, I forgot my before definition of $dsn; $dbh, my mistake. However, I only copy-pasted this code, I have properly defined all variables in the full code. On the other hand, I modified the code so much that I cannot reproduce the error now. Still I have some other errors (like Global symbol "$cols_type" requires explicit package name at ./test6.pl line 156.), which are somewhat related to this, however, all my variables are properly declared/defined (I have double-checked it).

      Iím sorry I opened this poor-quality question. :(

        Iím sorry I opened this poor-quality question. :(

        No harm done. Just keep in mind: the more accurate the question, the better the answers.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Uninitialized value and a dot between $schema and $table
by erix (Parson) on Aug 09, 2019 at 10:02 UTC

    $schema and $table don't have values (i.e., they are undefined.)

    Make sure to give them valid values before calling the create_table() function

    when I add $schema = "public", and $table = "tempt", your code just works for me.

    ( By the way, you probably want text, not char(). )

    (Oh, and $databas should be $database)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2020-08-11 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?