Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: Perl Not returning SQL query result

by afoken (Chancellor)
on May 13, 2021 at 22:06 UTC ( [id://11132555]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Perl Not returning SQL query result
in thread Perl Not returning SQL query result

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. ;-)

C:\Users\alex>perl -Mstrict -w -E "sub false { 0 } my $x=false or die +'Died'; say 'not reached';" Died at -e line 1.

$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.

C:\Users\alex>perl -MWin32 -E "sub false { 0 } eval { my $x=false or d +ie 'Died'; say 'not reached'; 1 } or Win32::MsgBox(qq[$@]);"

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?.

C:\Users\alex>perl -Mstrict -w -E "BEGIN { *CORE::GLOBAL::die=sub { 'I + am evil' } } sub false { 0 } my $x=false or die 'Died'; say 'not rea +ched';" not reached

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?

C:\Users\alex>perl -Mstrict -w -E "BEGIN { *CORE::GLOBAL::die=sub { 'I + am evil' } } sub false { 0 } my $x=false or die 'Died'; say 'not rea +ched, $x=',$x;"
not reached, $x=0

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". ;-)

Replies are listed 'Best First'.
Re^5: Perl Not returning SQL query result
by pryrt (Abbot) on May 13, 2021 at 23:06 UTC
    EDIT: after writing all that up and posting, I re-read your final paragraphs, and realized you said basically the same thing.

    So ignore all the original detail in the spoiler

Re^5: Perl Not returning SQL query result
by Marshall (Canon) on May 15, 2021 at 02:31 UTC
    I don't see any big issue here.

    I will mention something that is very useful about die messages. If you put a trailing \n, like "some message\n", that will suppress the details like line number, etc. I write different code for a sysop versus Windows user. For the inexperienced, usually a simple "what can I do about it?" message works best and extra detail just confuses them.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (1)
As of 2024-04-24 16:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found