Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Ternary confusion

by Nik (Initiate)
on Feb 27, 2004 at 20:35 UTC ( [id://332367]=perlquestion: print w/replies, xml ) Need Help??

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

Can somebody explain me exaclty what this part of code actually does? thank you!
$db = ($ENV{'SERVER_NAME'} ne "50free.net") ? DBI->connect("DBI:mysql:nikos_db", "root", "") : DBI->connect("DBI:mysql:nikos_db:50free.net","nikos_db","macgyver +") or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0

janitored by ybiC: Retitle form "clarification question"

I also wanted to show you that when i use the above code my index.pl page gives me this error:
Can't connect to local MySQL server through socket '/var/run/mysqld/my +sqld.sock' (2)
but when i am using the following it works ok!
$db = DBI->connect('DBI:mysql:nikos_db', 'root', '') or $db = DBI->connect('DBI:mysql:nikos_db:50free.net', 'nikos_db', 'macgy +ver', {RaiseError=>1});
why is this happening?

Replies are listed 'Best First'.
Re: Ternary confusion
by gryphon (Abbot) on Feb 27, 2004 at 20:45 UTC

    Greetings Nik,

    $db =
    Whatever we end up returning, save it into a scalar.

    ($ENV{'SERVER_NAME'} ne "50free.net")
    A conditional that evaluates whether the enviornment variable SERVER_NAME is not equal to "50free.net"

    ? DBI->connect("DBI:mysql:nikos_db", "root", "")
    If the conditional is true, then connect to a database on the localhost as root with no password. (Root with no password. Wow, that's dumb.)

    : DBI->connect("DBI:mysql:nikos_db:50free.net","nikos_db","macgyver")
    If the conditional is false, then connect to a database on a remote box called "50free.net" with a username and password combo.

    or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0
    If the db connection doesn't return true, then print an error message and exit the program.

    gryphon
    code('Perl') || die;

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Ternary confusion
by esskar (Deacon) on Feb 27, 2004 at 20:49 UTC
    you could rewrite it like this
    if($ENV{'SERVER_NAME'} ne "50free.net") { $db = DBI->connect("DBI:mysql:nikos_db", "root", ""); } else { $db = DBI->connect("DBI:mysql:nikos_db:50free.net","nikos_db","macg +yver"); } unless($db) { print font({-size=>5, -color=>'Lime'}, $DBI::errstr); exit(0) }
      And, you would be smart to do so. The above code is obfuscated seemingly for the sake of obfuscation. There is no reason not to write it cleanly. Also, by doing so, opportunities for expansion become available, like if you were dealing with three different databases...

      ------
      We are the carpenters and bricklayers of the Information Age.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        I'm not going to quibble with the basic thrust of your advice, but the following quote struck my eye:

        The above code is obfuscated seemingly for the sake of obfuscation.

        What leads you to say that? Because it uses the ternary operator? The first thing I noticed about the code is that it put each significant chunk on its own line. That hardly seems like the practice of a determined obfuscator. Apart from the issue of whether or not a given developer is familiar with the ternary operator, it boils down to :

        $db = [connection specification] or die;
        which is plain 'ol idiomatic Perl.

        There are a number of reasons one might write code like that, having nothing to do with obfuscation. Perhaps the original author liked the terse idiom and compensated for it by formatting the code the way s/he did. Maybe s/he had five minutes and somebody asked her/him to make the script use a different connection if it was running on a specific server.

        Of course, if you're going to rewrite it for later expansion, you might as well move the connection selecting code off into its own subroutine or, if it's complex enough, module. But I think we should all be aware that developers don't always have the time to develop code like that right from the very start -- especially when you don't know whether your code is going to be in use two weeks after you write it, which is a distinct possibility.

        If not P, what? Q maybe?
        "Sidney Morgenbesser"

    A reply falls below the community's threshold of quality. You may see it by logging in.
Uhh change your passwords.
by mutated (Monk) on Feb 27, 2004 at 21:07 UTC
    If those are your machines and those username/passwords are valid you may want to change them after posting them to a public forum....


    daN.
Re: Ternary confusion
by derby (Abbot) on Feb 27, 2004 at 20:39 UTC
    Check out perlop for the x = (cond) ? a : b portion (that's the ternary operator). The 'or print" section happens if whichever DBI is choosen from the ternary operator fails.

    -derby

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 07:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found