Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

my and its use

by tame1 (Pilgrim)
on Nov 16, 2000 at 00:06 UTC ( [id://41854]=perlquestion: print w/replies, xml ) Need Help??

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

Strange behavior
The following is a chunk of code I use in almost all my scripts

#!/usr/local/bin/perl -w # # This is a small program meant to output a report in xls format use strict; use DBI; use CGI qw(redirect); use Spreadsheet::WriteExcel; my $query = new CGI; # Let's set up the database handle, etc. fixed variables my $user = "jrobiso2"; my $auth = ""; my $db = "dbi:mysql:personnel"; # and now the database handle my $dbh = DBI->connect($db, $user, $auth) || die "Cannot connect!: $!\ +n$dbh->errstr";
The problem is that if you perl -cw this, it says "Global
symbol "$dbh" requires explicit package name . . . . .
So I am forced to put a my $dbh; one line before it.

Can someone tell me where I am wrong in thinking that I can
do my $dbh = DBI->connect($db, $user, $auth); ?

What does this little button do . .<Click>; "USER HAS SIGNED OFF FOR THE DAY"

Replies are listed 'Best First'.
RE: my and its use
by rlk (Pilgrim) on Nov 16, 2000 at 00:12 UTC
RE: my and its use
by btrott (Parson) on Nov 16, 2000 at 00:16 UTC
    If $dbh isn't defined (false), and the second half of that statement evaluates, then you're going to get a fatal error, because $dbh won't be defined. And you can't call the method errstr on an undefined value. So you should use:
    my $dbh = DBI->connect(...) or die $DBI::errstr;

      update: I knew I was posting with too much zeal lately. Just a great way to show what you don't know (or think you do). See tye's comment below for the real deal.

      btrott beat me to it, but I can add that or works because of precedence. You see, as everyone has said, the problem is that when the right side of the || get evaluated the handle doesn't exist yet because the my hasn't done it's magic yet. Using or avoids this because it's less important in terms of its precedence and therefore let's my do its thang (no typo) before checking out what it has to - and now $dbh does exist and can be checked. see perlman:perlop to read about the || and or operators and their respective precedence.

      <myExperience> $mostLanguages = 'Designed for engineers by engineers.'; $perl = 'Designed for people who speak by a linguist.'; </myExperience>

        Sorry, wrong:

        #!/usr/bin/perl -w use strict; eval ' my $x= hello() or die $x->error(); 1 ' or warn "$@\n"; eval ' my $y= hello() || die $y->error(); 1 ' or warn "$@\n"; __END__ Global symbol "$x" requires explicit package name at (eval 1) line 1. Global symbol "$y" requires explicit package name at (eval 2) line 1.

        The use of or is more correct for code like this since, when hello() fails, you don't want $x set to the return value of die. Of course, die doesn't return so this isn't a good example.

        But or doesn't cause what comes after it to be a separate statement so the my still hasn't created the variable yet.

                - tye (but my friends call me "Tye")

        or might not generate an error when the connect succeeds but calling $dbh->errstr with $dbh being undef is not going to do you much good! Stick to

        my $dbh = DBI->connect(...) or die $DBI::errstr;
RE: my and its use
by autark (Friar) on Nov 16, 2000 at 00:14 UTC
    | Can someone tell me where I am wrong in thinking that I can
    | do my $dbh = DBI->connect($db, $user, $auth); ?

    There is nothing wrong with your connect statement. However, in your code you are using the $dbh variable before it is properly defined.

    my $dbh = DBI->connect($db, $user, $auth) || die $dbh->errstr;
    Here you use the $dbh handle in the 'else' part of your statement. Think of it as this:
    (my $dbh = DBI->connect($db, $user, $auth) ) || ( die $dbh->errstr );
    So if the first part fails, it tries the second part, which happens to use the $dbh variable which failed to be defined because the connect call failed.

    In this case I would put the 'my $dbh' part as a separate stament above the connect statment.

    Autark.

(jeffa) RE: my and its use
by jeffa (Bishop) on Nov 16, 2000 at 00:14 UTC
    It's because of the or die part:
    || die "Cannot connect!: $!\n$dbh->errstr";
    You are referencing $dbh on the same line, so declare $dbh before you try to connect.

    Jeff

Log In?
Username:
Password:

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

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

    No recent polls found