Don't ask to ask, just ask | |
PerlMonks |
Re^4: Perl Not returning SQL query resultby afoken (Chancellor) |
on May 13, 2021 at 22:06 UTC ( [id://11132555]=note: print w/replies, xml ) | Need Help?? |
I am not quite sure about your Third point. If the DB connect fails, this code will DIE. The idea of an eval{} block was not under consideration here. The OP was asking some very basic questions and I tried (perhaps not successfully) to give a simple "roadmap". Well, once I start bean-counting, it's hard to stop. ;-)
$x will never be assigned, because perl dies before it could assign the result of the or operation. That's what happens in your code, and I could have stopped here. BUT, in beancounting mode, there may be an eval around the assignment, not shown in the posting. Yes, this is beyond basic use of perl.
That example aborts, but the last thing it does before is to display a message box contaning "Died at -e line 1.". That does not make much sense when hacking a one-liner in a console window. But it becomes really useful when perl is started by something like a self-extracting archive or simply a non-console application. Without that messagebox, you would just see a console window flashing into existance and disappear again, and you had no chance to see a useful error message. I used that trick almost two decades ago, see Re^3: How do you distribute your Perl application to your customers?.
And that silly example demonstrates that one can redefine die so it does not die. Just to make people really, really angry. A little extra: Quick, don't think, just answer for yourself: What does that example print out?
Wow, that was a little bit unexpected, at least for me. But yes, the precedence of or is lower than that of =, see perlop. So I have to correct the third point of my bean-counting: $dbh is assigned undef if DBI->connect() fails BEFORE the right-hand side of or is executed, i.e. the code die()s. Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
In Section
Seekers of Perl Wisdom
|
|