in reply to my and its use

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;

Replies are listed 'Best First'.
RE: RE: my and its use
by jptxs (Curate) on Nov 16, 2000 at 00:36 UTC

    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;