Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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.

require General::Disclaimer;
Perl is

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

      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.


    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 (Canon) 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://410270]
[marto]: oh you guys...
[robby_dobby]: LanX: Calling the mayor of London, a transport commissar sounds about right. He seems especially fond of talking about "TfL" aka "Transport for London"

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-12-15 18:52 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (442 votes). Check out past polls.