Perl Archives

SQL Abstraction

SQL abstraction is hard it seems. Trying to insert an empty record in SQLite should look like this:

INSERT INTO "authors" DEFAULT VALUES

In SQL::Abstract and SQL::Maker, it does not work:

INSERT INTO "authors" () VALUES () # syntax error

The only module that seems to get things right is vti's ObjectDB2, which is in a very early stage of development and not on CPAN so far.

DBIx::Inspector Schema Loader

Today, I was looking for a simple module that fetches my DB schema. Played around with DBIx::Class::Schema::Loader and Rose::DB::Object::Metadata, with no success.

Have already given up, but finally decided to make a Github search for DBIx, which provided exactly what I needed. Just curious why it was so hard to find this module?

DBIx::Inspector

use DBI;
use DBIx::Inspector;
use 5.010000;

my $username = "";
my $password = "";

$dbh = DBI->connect('dbi:mysql:mydb', $username, $password)
            or die $DBI::errstr;

my $inspector = DBIx::Inspector->new(dbh => $dbh);

my $table = $inspector->tables('users')->next;

say "Name";
say "    ", $table->name;

say "Primary Keys";
for my $pk ($table->primary_key) {
    say "   ", $pk->name;
}

say "Columns";
for my $column ($table->columns) {
    say "    ", $column->name, " ", $column->type_name,
      "(", $column->column_size ,")";
}

1;

Mo supports chained accessors

with the help of ingydotnet++ valuable suggestions, Mo now has the option to chain accessors:

package ChainedAccessors;
use Mo qw/chain/;

has 'first'  => (chain => 1);
has 'second' => (chain => 1);


package main;

$f = ChainedAccessors->new;

$f->first(1)->second(2);

$f->first;  # 1
$f->second; # 2

thanks also for the feedback in response to my previous blog post

Chaining in Moose, Mouse, Moo and Mo

Recently, I created a new issue on Github for Mo (https://github.com/ingydotnet/mo-pm), suggesting a chained interface:

package Hello;
use Mo;
has 'first';
has 'second';

my $hello = Hello->new;

# current implementation
$hello->first('foo');
$hello->second('bar');

# result
print $hello->first;  # foo
print $hello->second; # bar

# chaining (suggested)
$hello->first('foo')->second('bar');

The suggestion was rejected, providing the following reasons:

"Neither Moo nor Moose nor Mouse does this by default, and nor do the vast majority of pre-Moose accessor builders."

While this is in line with Mo guidelines

"Mo will attempt to not do the things it does in an incompatible style to the Moose family.",

the question is: why not switch to a chaining interface in Moose, Mouse and Moo, and Mo? Should this even become the default behaviour?

Playing around with Method::Signatures::Simple

Method::Signatures::Simple: Nice, very simple, and almost as fast as pure perl, is this module ready for use in modules you depend on?

package Test;

use Method::Signatures::Simple;
use Benchmark qw/:all/ ;


sub new {
    bless {}, shift;
}


# with Method::Signatures::Simple
method signatures ($first, $second) {
    die unless $self;
    die unless $first;
    die unless $second;
}


# default perl
sub default {
    my $self = shift;
    my ($first, $second) = @_;

    die unless $self;
    die unless $first;
    die unless $second;
}


# some benchmarking
my $self = __PACKAGE__->new;

my $call_signatures = sub {
    $self->signatures(1,2)
};

my $call_default = sub {
    $self->default(1,2)
};


cmpthese(10000000, {
    'Signatures' => $call_signatures,
    'Default'    => $call_default
});

Results:

                Rate    Default Signatures
Default    1316309/s         --        -8%
Signatures 1434103/s         9%         --

About Forward Ever

user-pic I blog about Perl.