I don't remember the circumstances that brought this module on. Probably drugs of some sort. In any case, I thought of this minimal DBI that uses nothing more than a tied scalar to play with databases. I've only really tested it with SELECT statements, but it works pretty well. Since I have a tendency to create somewhat crazy modules (like Parse::Tinymush), I thought this one would get my usual response ("Stop the madness!", "Peope are starving and you wrote THIS?"). Instead, people thought it was "perly and useful" and "/cool/". So, does this module demonstrate insanity or genius?

package Tie::DBI; use 5.008003; use strict; use warnings; use base 'Tie::Scalar'; use DBI; our $VERSION = '0.01'; sub TIESCALAR { my $class = shift; my ($dsn, $user, $pass) = @_; my $self = { dbh => undef, }; $self->{dbh} = DBI->connect($dsn, $user, $pass); bless $self, $class; } sub STORE { my ($self, $value) = @_; if ( $self->{sth} && ref($value) eq 'ARRAY' ) { $self->{sth}->execute(@$value); } else { $self->{sth} = $self->{dbh}->prepare($value); if ( $self->{sth}->FETCH('NUM_OF_PARAMS') == 0 ) { $self->{sth}->execute(); } } } sub FETCH { my ($self) = @_; return undef if !$self->{sth} || $self->{dbh}->err; return $self->{sth}->fetchrow_arrayref() || 0; } 1;
It works like this:
my $dbh; tie $dbh, 'Tie::DBI', $dsn, $user, $pass; $dbh = "select * from table where field = $value"; while ( my $row = $dbh ) { print $row->[0], "\n"; }

Assigning to the tied variable causes the value to be prepared and executed. Reading from the variable causes fetchrow_arrayref() to be called and one row is returned. 'undef' is returned if there is no statement handle or there is an error. 0 is returned if there are no more rows.

Placeholders can be used as well:

$dbh = "select * from table where field = ?"; $dbh = [$value]; while ( my $row = $dbh ) { print $row->[0], "\n"; }

Any comments or questions?

Note 1: I realize that Tie::DBI already exists on CPAN. I just couldn't think of a good name, except for Tie::DBI::Minimalist, which is kinda dumb. Any good suggestions?

Note 2: I think an even better minimal interface can be done using a tied filehandle. I'm working on that.

Note 3: On a side note, is this a meditation or a craft?


In reply to Genius or Insanity: My minimal DBI by Kageneko

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.