Looking at DBI Part 13, An Evil Twin!

In part 12 we had a look at a look at the array_execute and now we are going to look at bind_param's Evil Twin bind_col.

Bind what?

DBI's master chef designed it from the outset for 'SPEED', to get quickly to the point any extra under the hood calls to DBI will slow things down, bind_col eliminates some of the under the hood call to DBI and will make your assignment magically disappear

Lets that this example;

$sql = 'SELECT city, state_province, country_id FROM locations'; $sth = $dbh->prepare($sql); $sth->execute();

while (my $row = $sth->fetchrow_arrayref()){
my $r_city = $row->[0];
my $r_state = $row->[1];
my $r_country = $row->[2];
print "$r_city,$r_state,$r_country";
};

Remembering that is it best to put our bind_col assignment after the execute we change the code to this;

$sql = 'SELECT city, state_province, country_id FROM locations'; $sth = $dbh->prepare($sql); $sth->execute(); my ($r_city, $r_state,$country);

$sth->bind_col(1,$r_city);
$sth->bind_col(2,$r_state);
$sth->bind_col(3,$r_country);

while ( $sth->fetchrow_arrayref()){
print "$r_city,$r_state,$r_country";
};

How to resize a NTFS qemu qcow2 image (the easy way)

There's so much bad information out there how to simply resize a NTFS qemu qcow2 (windows in kvm) image, and I need to frequently enhance my windows images, esp. on win8 (64 bit) so I'll document it here for the next time:

  • Don't use fdisk if you have gparted!
  • Don't waste space converting a qcow2 to raw
  • Shutdown the vm!

  • $ sudo su -

  • $ ls /var/lib/libvirt/images/*.qcow2
  • $ qemu-img resize /var/lib/libvirt/images/windows_x64.qcow2 +5GB

  • $ modprobe nbd max_part=63

  • $ qemu-nbd -c /dev/nbd0 /var/lib/libvirt/images/windows_x64.qcow2

  • $ # fdisk -l /dev/nbd0 # or better

  • $ kpartx /dev/nbd0 # to check the partitions and sizes
  • $ ntfsresize --info /dev/nbd0 # to check if the volumes are not dirty

  • $ gparted /dev/nbd0 # enhance the partition size to max

  • $ killall qemu-nbd

  • Start the vm and let windows do the chkdsk /f (in win8 automatically)

  • In winxp you might need to do ntfsresize -x /dev/nbd0p2 manually

Is it possible to run Movable Type under Apache HTTP Server?

That was the question posted to Quora which seemed strange to me given that Apache is the web server most used by MT end users, developers and even (to my knowledge) every single core developer who has even worked on MT. Not to mention the rest of the world! I'd love to know what spurred on the question and what web server people generally use in the author's world.

Anyhow, the reason I'm writing is mainly because the REST of my answer which is a rant about still-persistent use of CGI by some administrators/developers. facepalm

My answer, after the jump, just in case Quora goes the the bit graveyard one day...

Simplifying imports (mainly in tests)

During test writing often I find myself having to use a lot of modules and setting up various things that should be common for all of my tests in a given project. In some examples it may get ugly and I really don't like having a ~30 lines long setup in every single test file of mine. After some experimenting, I came up with a quick-and-dirty solution. I create a module in my 't' directory (let's say t/lib/Test/MyApp.pm) and put everything I need into it's import sub:

Looking at DBI Part 12, Exe that Array!

In part 11 we had a look at a look at getting values back from SPs (stored procedures) now we are going to look a another variation on binding the array_execute.

One of Those Days

You know what its like. You have just been handed a requirement to store 200 individual selections from a web page. Well as a diligent programer with you now epic DBI skills you come up with this

my $sth = $dbh->prepare("INSERT INTO picks (user_id, pick_id) VALUES(:p_user_id, :p_pick_id)");

for (my $index=0;$index $sth->bind_param(':p_user_id', $user_id);
$sth->bind_param(':p_pick_id', $picks[$index]);
$sth->execute();
}

Well at least you know it works but you may be sucking up a good deal or resources with an execute at each iteration and bind as well any error along the line could cause all the data sent to the DB to be lost.

array_execute to the Rescue

Test::Class::MOP now has an 'is testcase' trait

When I previously blogged about Test::Class::MOP, I showed the bare basics of a test. Per a suggestion from Toby Inkster, I've now added an is testcase trait for methods. Unlike Test::Class::Moose, test methods no longer need to start with test_. Instead, you can do something like this:

method simple_test($report) is testcase {
    $report->plan(1);
    is $self->test_fixture->full_name, 'Bob Dobbs',
        'Our full name should be correct';
}

Obviously there's a lot going on there, so I'll explain a bit more about this.

Plz recommend an in-browser irc client

I'm thinking of switching from a standalone irc client to 1 which runs in Chrome, or perhaps in FF.

Any ideas?

Oh My Glob

Hello fellow Perl developers,

I wrote up a blog entry about typeglobs on my personal blog: what they are, and what they are used for. If you've never heard of typeglobs, or you feel you could stand to pick up a trick or two, check out the post:

Oh My Glob

Looking at DBI Part 11, Stored Whaaaat!

In part 10 we had a look at a look at the $sth 'bind_param_inout' method to get something extra from an DBI query. This time we will stick with this but look at another common use at was hinted at in the last post.

Store This!!

Unlike many perl programmers I think SPs are a great. The encapsulate biz-rules really well, in the old days they ran faster, are easier to maintain and they can protect you when the Boss tells you we want to move over to 'Elixir' front end with a 'D' back end.

Calling all Cars

Calling an SP with DBI is quiet strait forward, though implementation is different between RDBMS languages. Some require some sort keyword others only let you run an SP inside their own procedural language.

WWW::Mechanize - Adding form_action()

https://github.com/markleightonfisher/WWW-Mechanize is a fork of WWW::Mechanize with a form_action method for finding a form using a regex that matches the form's action. (I've submitted this as a possible enhancement to WWW::Mechanize.)

I needed this when I encountered a page where the forms moved around based on whether you logged in as an ordinary user or an administrator. (Not my page, so don't blame me :( .)

TPF Grant Progress Report: November 2013

This past May, The Perl Foundation awarded a grant to fund development of a couple features in Pinto. Pinto is a robust tool for curating a private repository of CPAN modules, so you can build your application with the right modules every time. This is my third progress report on that work.

I've been preoccupied with Stratopan lately, so I have no progress to report this month. But now that the beta has been launched, I expect to have more time for grant work.

By the way, if you sign up for the Stratopan beta before December 15, you'll get unlimited access for life.

Viva Perl!

jHackers

I found the site that Japanese hackers is introduced by English. This will help to know what Japanese hacker think.

jHacker


Looking at DBI Part 10, Getting it For Free


In part 9 we had a look at ASIC transactions as implemented by DBI now we are going to have a look at getting more back from DBD $sth than just the rows updated or the the record set.

Look Ma no Hands

There are many times when playing with RDBS that we would like to get just a little more back form the DB when we do something. What comes to mind first of is those nasty primary_key values after we do an insert. Most if not all RDBS have some way to get them back at least in the SQL world but how do I do this in DBI.

bind_param_inout

Well the answer is to use the SQL and the bind_param_inout method to get value from the DB. Most mainstream DBD do work I will just use DBD::Oracle as I have the code handy.

AUTOINCREMENT

Post #1

Hello everyone, this is my first post here.
I'm fairly new to Perl (~1 year experience) and to testing. I'd like to write not so much articles, but short, to the point code snippets with comments that are useful for those, who are new to Perl, DBIx::Class, PerlDancer2 and testing.
I will try to post things that I would like to know when I started out, but could not find them.

Porting Test::Class to the p5-mop

Stevan Little has been asking people to port things to the p5-mop-redux or to create new modules. I decided to reimplement Test::Class::Moose as Test::Class::MOP. The following works:

Perl and Raspberry Pi

Perl and Raspberry Pi :

[From my blog.]

Looking at DBI Part 9, d'ho

Ouch I didn't Mean to do That

In part 8 we used fetchall_arrayref to get just the fields we want and not the entire row now we will looks at when we make mistakes.

DBI Transactions

In a perfect world, we would never need our old friends commit and rollback. But hard disks die, the power goes out, and fingers click the wrong buttons; so DBI supports the age-old ACID Module for transactions. As long as the underlying DBD driver and RDBMS and Driver support it as well.

To get transactions to work on DBI, you first have to set the AutoCommit attribute of our database handle to off. One normally does this when creating the handle like this:

$dbh = DBI->connect('dbi:Oracle:', 'hr@localhost/XE', 'hr', {AutoCommit => 0});


We can now use the DB handle's rollback and commit methods much as you would in any PSQL program.

UPDATE employees SET salary = ? WHERE last_name = ?

To use transactions with the foregoing SQL, you would create the handle like this:

In Delicious, I start to inform translation of Perl Tutorial by Code Examples

In Delicious, I start to inform translation of Perl Tutorial by Code Examples. You can know the post of my transation.

Delicious - yukikimoto

Debuging method for perl Complex data structures

For complex data structures (references, arrays and hashes) you can use the Data::Dumper

use Data::Dumper qw(Dumper);

print Dumper \@an_array;
print Dumper \%a_hash;
print Dumper $a_reference;

These will print something like this, which helps understand the content of the variables, but shows only a generic variable name such as $VAR1 and $VAR2.

OT: DataCite Recommends "doi:" Scheme Prefix

(Posted here so I don't forget it and others can find it...)

In the latest DataCite Metadata Kernel document, section "2.2 Citation", DataCite recommends ("prefers" is their term) that original-format DOIs used in citations include the doi: scheme prefix. ("Original format" refers to non-URL DOIs.) Unfortunately, the DataCite Metadata Kernel document does not mention that the string doi: is a scheme prefix, making it hard to find.

An example DOI with the scheme prefix is doi:10.1006/ijhc.1996.0110.

About blogs.perl.org

blogs.perl.org is a common blogging platform for the Perl community. Written in Perl with a graphic design donated by Six Apart, Ltd.