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

is there a better way to write this script? can die take arguments like font as print does?
if ($host eq "dell") { if ($db = DBI->connect("DBI:mysql:nikos_db", "root", "XXXX")) {} else { print font( {-size=>5, -color=>'Lime'}, $DBI::errstr ); exit(); } } else { if ($db = DBI->connect("DBI:mysql:nikos_db:50free.net", "nikos_db", + "XXXX")) {} else { print font( {-size=>5, -color=>'Lime'}, $DBI::errstr ); exit(); } }
ans also can i put color on that line? and if so, how? print ol( li( $st->{NAME} ) ); thanks!

Edited by theorbtwo: Removed passwords, added code tags at bottom.

Replies are listed 'Best First'.
Re: Print and Die!
by Abigail-II (Bishop) on Feb 06, 2004 at 21:10 UTC
    Perl doesn't know about colours. Colour is a property of the displaying device - not of Perl.

    But you can give die arguments, just like print.

    Abigail

      Perhaps the OP is using a ANSI/Curses library or set of routines written by someone else. It might (doubtful) even be CGI related. I can't grok these functions as something I've seen before, but that appears to be the plan -- they don't show up on Google so they are surely custom.

      To the opening poster -- Anyhow, the question about die taking arguments is very very basic, and is covered by most Perl books. It seems you need more basic Perl knowledge, and need to be reminded that good books exist -- die is covered early in most books. Web sites should not be ground zero for your first stabs at a new language. Try O'Reilly's Llama or Camel books. We'll be glad to help once you pick up the basic concepts, else you will just be asking questions all day and not really learning on your own.

      Since I am a nice guy, try this with this strange "font" code, it might work, might not:

      die (font( {-size=>5, -color=>'Lime'} , $DBI::errstr ));
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Print and Die!
by Zaxo (Archbishop) on Feb 06, 2004 at 21:28 UTC

    This is a case where the logical and trinary operators can help,

    $db = $host eq 'dell' ? DBI->connect("DBI:mysql:nikos_db", "root", "") : DBI->connect("DBI:mysql:nikos_db:50free.net","nikos_db","obscure") or print font({-size=>5, -color=>'Lime'}, $DBI::errstr) and exit 0;

    After Compline,
    Zaxo

Re: Print and Die!
by hardburn (Abbot) on Feb 06, 2004 at 21:33 UTC

    Since it looks like this is part of a CGI, putting font in die will:

    1. Print the literal <font> HTML tags around the text to your server's error log
    2. If you have use CGI::Carp 'fatalsToBrowser'; on, the error message will be sent back to the browser and displayed in the given font.

    ----
    I wanted to explore how Perl's closures can be
    manipulated, and ended up creating an object
    system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

    A reply falls below the community's threshold of quality. You may see it by logging in.
meta sugguestion
by Fletch (Bishop) on Feb 06, 2004 at 21:15 UTC

    Perhaps one should consider removing database passwords before pasting example code to public web sites . . .

      Fletch is right. Even if root can only connect over local host you still have given away way too much information. Also, you should always create underpriviledged users to run programs -- allowing programs / whatever the least privilege possible is a very important programming concept. So create a database and a user (with a password that you don't display on Perlmonks) and use them to connet to the database. Also assign root a password. Do the following in case you don't want to look up the commands (assuming you're using mysql):

      I would strongly suggest you don't allow connections from anywhere but localhost. (i.e. use localhost or 127.0.0.1 as your hostname). If you really didn't release your password and deleted it (i.e. the "" was intentional), apologies. However, I figured it's better safe then sorry, so best to give you the information just in case.


      Want to support the EFF and FSF buy buying cool stuff? Click here.
Re: Print and Die!
by NetWallah (Canon) on Feb 07, 2004 at 06:43 UTC
    If I understand the question right, you were also looking for coding style improvement suggestions. Here is one:
    my %HostInfo = ( dell => {connectstr=>"DBI:mysql:nikos_db", user=>"root", pas +s="XXXX"}, __DEFAULT__ => {connectstr=>"DBI:mysql:nikos_db:50free.net", user=> +"nikos_db", pass="XXXX"} ); my $hostIdx = $host; if (not exists $HostInfo{$hostIdx)){ $hostIdx ="__DEFAULT__"; } if ($db = DBI->connect($HostInfo{$hostidx}{connectstr},$HostInfo{$host +idx}{user}, $HostInfo{$hostidx}{pass})){ # Connect OK }else{ print font( {-size=>5, -color=>'Lime'}, $DBI::errstr ); exit(); }

    Update: Changed "exist" to "exists" .. I still havent verified Synax...

    "When you are faced with a dilemma, might as well make dilemmanade. "

    A reply falls below the community's threshold of quality. You may see it by logging in.