Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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]
[Eily]: do you pronunce that /xju:mən/ then ?
[Eily]: that's not what I'd call a silent h :)
[LanX]: see soundfile https://en. wiki/human# Pronunciation
[Your Mother]: Just Another Perl 'Acker.
[LanX]: I pronounce it 'juːmən'
[Your Mother]: Hey, let's not bring antisemitism into the conversation.
[LanX]: for centuries posh English speakers tried to approach French, that's where all the trouble starts
[Eily]: Just another Perl coeurs
[Eily]: (perl with hearts, 'Acker with a French accent is pronunced like coeurs)
[LanX]: IMHO 'ju' for 'u' happens in English where they can't pronounce the French u (or German )

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2017-03-27 17:11 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.