Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Pearls (not really) of Perl programming

by radiantmatrix (Parson)
on Nov 24, 2004 at 23:16 UTC ( #410270=note: print w/ replies, xml ) Need Help??


in reply to Pearls (not really) of Perl programming

One of mine, in the early days of learning about DBI...

open INFILE, '<source.txt'; while (<INFILE>) { my $dbh = DBI->connect("dbi:mysql:database=maindb;host=localhost"); my $sth = $dbh->prepare("SELECT * FROM $table WHERE uid=$user"); $sth->execute; print join(',', $sth->fetchrow_array); print "\n"; }

Note the complete lack of error handling, no use of prototypes, and the assumption that only one row would be returned. I also particularly enjoy the two separate print statements. And, I printed to STDOUT (without setting $|) even though I wanted to write to a file.


radiantmatrix
require General::Disclaimer;
Perl is


Comment on Re: Pearls (not really) of Perl programming
Download Code
Replies are listed 'Best First'.
Re^2: Pearls (not really) of Perl programming
by hubb0r (Pilgrim) on Nov 25, 2004 at 07:00 UTC
    Plus, depending on the number of lines in the INFILE, you probably swamped the DB by not keeping the $dbh outside of the while.

      Quite. Not to mention that particular app connected to a mysql instance that was about 5 router hops away over not-the-fastests links. Connect times averaged 5s, which really reared its ugly head when the input file was 2_000 lines. ;-)


      radiantmatrix
      require General::Disclaimer;
      Perl is

Re^2: Pearls (not really) of Perl programming
by ihb (Deacon) on Nov 26, 2004 at 02:17 UTC

    Also, there's no localization of the filehandle.

    ihb

    See perltoc if you don't know which perldoc to read!
    Read argumentation in its context!

Re^2: Pearls (not really) of Perl programming
by Mr. Muskrat (Abbot) on Dec 04, 2004 at 04:05 UTC

    I could probably write a book on the subject of the OP but I'll stick to just one DBI example.

    my $dbh = DBI->connect($database, $user, $password) or die $dbh->errstr;

    When I found it, I decided to grep for it. It was being used in every script that pulled from the database!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2015-07-30 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls