Gabor Szabo Perl trainer and developer
http://szabgab.com
Gabor Szabo about programming in Perl, automated testing, dynamic languages and everyting elseen-usCopyright 2002-2012, Gabor Szaboszabgab@gmail.com1901-01-01T00:00+00:001hourlyPerl Maven Cookbook
http://szabgab.com/perl-maven-cookbook.html
After a long period of yak shaving, yesterday I decided to go for the short-cut
and set up a minimal system for the <a href="http://perlmaven.com/">Perl Maven</a>
web site.
<p>For the full article visit <a href="http://szabgab.com/perl-maven-cookbook.html">Perl Maven Cookbook</a></p>
Gabor Szabo2012-05-03T10:04:27+00:00PerlTODO for May 2012
http://szabgab.com/todo-2012-05.html
In January I started to write <a href="http://szabgab.com/todo-2012.html">annual reports and TODOs</a>.
In <a href="http://szabgab.com/todo-2012-02.html">February</a> and in <a href="http://szabgab.com/todo-2012-03.html">March</a>
I published monthly updates.
In March and April I was so busy with work at clients and holidays,
that I could not even keep up with e-mail. Publishing the <a href="http://perlweekly.com/">Perl Weekly</a>
was also hard. I could not even post my report at the beginning of April and there was not much point in
any planning. I did not know how much time I'll be able to allocate.
As the load now seem to lighten up, I try to collect what happened in the last 2 months
and make some plans.
<p>For the full article visit <a href="http://szabgab.com/todo-2012-05.html">TODO for May 2012</a></p>
Gabor Szabo2012-05-01T10:22:09+00:00Perl, TODOFacebook vs Google+ for Perl projects
http://szabgab.com/facebook-vs-google-plus-for-perl-projects.html
I guess the two dead-lines I have next week caused me to go in full procrastination mode
and search for Perl based projects on Facebook and Google Plus.
<p>For the full article visit <a href="http://szabgab.com/facebook-vs-google-plus-for-perl-projects.html">Facebook vs Google+ for Perl projects</a></p>
Gabor Szabo2012-04-28T19:28:32+00:00Perl, Facebook, Google PlusSplice to slice and dice arrays in Perl
http://szabgab.com/splice-to-slice-and-dice-arrays-in-perl.html
After learning about <span class="hl">pop()</span> and <span class="hl">shift()</span> students sometimes
ask me how to remove an element from the middle of an array?
I usually don't have time to explain it - there are other things to teach,
that seem to be more important than <span class="hl">splice()</span> in the limited
time we have, but I usually give them at least the direction.
<p>For the full article visit <a href="http://szabgab.com/splice-to-slice-and-dice-arrays-in-perl.html">Splice to slice and dice arrays in Perl</a></p>
Gabor Szabo2012-04-26T21:19:35+00:00Perl, splice, arrayPerl Weekly two days later
http://szabgab.com/perl-weekly-two-days-later.html
Two days ago I wrote that the <a href="http://szabgab.com/perl-weekly-newsletter-at-3000.html">Perl Weekly reached 3000 subscribers</a>.
The article was posted to <a href="http://news.ycombinator.com/item?id=3840406">Hacker News</a> and to
<a href="http://www.reddit.com/r/perl/comments/s9b2o/the_perl_weekly_newsletter_reaches_3000/">Reddit</a>.
I give some details on the numbers as I know you might be interested in this stuff.
<p>For the full article visit <a href="http://szabgab.com/perl-weekly-two-days-later.html">Perl Weekly two days later</a></p>
Gabor Szabo2012-04-15T20:19:30+00:00Perl, Perl WeeklyThe Perl Weekly newsletter reaches 3000 subscribers
http://szabgab.com/perl-weekly-newsletter-at-3000.html
It is time for celebration.
Yesterday the <a href="http://perlweekly.com/">Perl Weekly newsletter</a>
reached 3000 subscribers!
<p>For the full article visit <a href="http://szabgab.com/perl-weekly-newsletter-at-3000.html">The Perl Weekly newsletter reaches 3000 subscribers</a></p>
Gabor Szabo2012-04-13T09:26:53+00:00Perl Weekly, PerlSimple Database access using Perl DBI and SQL
http://szabgab.com/simple-database-access-using-perl-dbi-and-sql.html
<p>
While in most fields Perl adheres to the concept of TMTOWDI,
in accessing relation databases Perl has a de-facto standard
called DBI or <b>Database independent interface for Perl</b>.
</p>
<p>
<h2>Architecture</h2>
</p>
<p>
The Perl scripts use DBI which in turn uses the appropriate
<b>Database Driver</b> (e.g. <a href="http://metacpan.org/release/DBD-Oracle">DBD::Oracle</a>
for <a href="http://www.oracle.com/">Oracle</a>,
<a href="http://metacpan.org/release/DBD-Pg">DBD::Pg</a> for <a href="http://www.postgresql.org/">PostgreSQL</a>
and <a href="http://metacpan.org/release/DBD-SQLite">DBD::SQLite</a> to access <a href="http://sqlite.org/">SQLite</a>).
</p>
<p>
Those drivers are compiled together with the C client libraries
of the respective database engine. In case of SQLite, of course all the
database engine gets embedded in the perl application.
</p>
<p>
It is very hard to improve on the lovely ASCII-art that comes
with the documentation of <a href="http://metacpan.org/release/DBI">DBI</a> so let me reproduce it here:
</p>
<pre class="sh_perl">
|<- Scope of DBI ->|
.-. .--------------. .-------------.
.-------. | |---| XYZ Driver |---| XYZ Engine |
| Perl | | | `--------------' `-------------'
| script| |A| |D| .--------------. .-------------.
| using |--|P|--|B|---|Oracle Driver |---|Oracle Engine|
| DBI | |I| |I| `--------------' `-------------'
| API | | |...
|methods| | |... Other drivers
`-------' | |...
`-'
</pre>
<p>
<h2>Simple example</h2>
</p>
<p>
I'll use SQLite to demonstrate the examples. That will make it
very easy for you to try them on your computer.
(e.g All versions of <a href="http://dwimperl.com/">DWIM Perl</a> already come
with the necessary modules.)
</p>
<pre class="sh_perl">
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbfile = "sample.db";
my $dsn = "dbi:SQLite:dbname=$dbfile";
my $user = "";
my $password = "";
my $dbh = DBI->connect($dsn, $user, $password, {
PrintError => 0,
RaiseError => 1,
AutoCommit => 1,
FetchHashKeyName => 'NAME_lc',
});
$dbh->disconnect;
</pre>
<p>
We load DBI but we do <b>not</b> explicitly load the database driver.
That will be done by DBI. The DSN (Data Source Name) is very straight forward.
It contains the type of the database. That will be the clue to DBI which DBD to load.
In case of SQLite, the only thing we really need is the path to the database file.
</p>
<p>
The username and password fields were left empty. I think they are not
relevant at all for SQLite.
</p>
<p>
The last parameter of the connect call is a reference to a hash containing
some attributes I like to set.
</p>
<p>
The DBI->connect call returns a <b>database handle object</b> that usually we store in a variable
called <span class="hl">$dbh</span>.
</p>
<p>
The call to <b>disconnect</b> from the database is optional as it
will automatically be called when the variable <span class="hl">$dbh</span> goes out
of scope but it having it might be a clear indication that you are done with the
database.
</p>
<p>
<h2>CREATE TABLE</h2>
</p>
<p>
Of course having a connection is not enough. We need to be able to fetch data from the
database or insert data into the database but for our example to work first we
actually need to create the tables of the database.
</p>
<p>
In this case we can do this with a single command:
</p>
<pre class="sh_perl">
my $sql = <<'END_SQL';
CREATE TABLE people (
id INTEGER PRIMARY KEY,
fname VARCHAR(100),
lname VARCHAR(100),
email VARCHAR(100) UNIQUE NOT NULL,
password VARCHAR(20)
)
END_SQL
$dbh->do($sql);
</pre>
<p>
The first one is just a here document of an SQL statement to CREATE TABLE.
Then we call the <span class="hl">do</span> method of the database handle which will send the
SQL statement to the database.
</p>
<p>
<h2>INSERT</h2>
</p>
<p>
Now let's see the real thing, inserting data:
</p>
<pre class="sh_perl">
my $fname = 'Foo';
my $lname = 'Bar',
my $email = 'foo@bar.com';
$dbh->do('INSERT INTO people (fname, lname, email) VALUES (?, ?, ?)',
undef,
$fname, $lname, $email);
</pre>
<p>
To insert a row we call the <span class="hl">$dbh->do</span> method again but instead of passing
the actual data, we put question-marks <span class="hl">?</span> as place-holders.
</p>
<p>
The SQL statement is followed by the word <span class="hl">undef</span>.
That's actually the place of a hash-reference providing
parameters to this specific call, similar to the attributes
passed to the <span class="hl">connect</span> method but I think it is rarely used.
</p>
<p>
That is followed by the actual values that go in place of the the place-holders.
</p>
<p>
As you can see we did not have to put the place-holders in any kind of quotes
nor did we have to somehow convert the values. DBI did it for us.
</p>
<p>
This helps us avoid
<a href="http://en.wikipedia.org/wiki/Sql_injection">SQL injection</a> attacks.
Even if you meet someone called <a href="http://xkcd.com/327/">Bobby Tables</a>.
</p>
<p>
<h2>UPDATE</h2>
</p>
<p>
For updating some data in the database we also use the <span class="hl">do</span> method.
</p>
<pre class="sh_perl">
my $password = 'hush hush';
my $id = 1;
$dbh->do('UPDATE people SET password = ? WHERE id = ?',
undef,
$password,
$id);
</pre>
<p>
Nothing special here. An SQL statement with some place-holders. <span class="hl">undef</span>
instead of the extra attributes and the parameters to be used in place of
the place-holders.
</p>
<p>
<h2>SELECT</h2>
</p>
<p>
This is by far the most interesting part of the database access. As the SELECT
statement can return a lot of rows and a lot of values in each row we cannot use
a simple call to the <span class="hl">do</span> method.
</p>
<p>
Instead, there are several ways to fetch the data. I'll show here two.
For both we have 3 steps: <span class="hl">prepare</span> the SQL statement.
<span class="hl">execute</span> the statement with specific data and <span class="hl">fetch</span> the rows.
</p>
<p>
From these, the <span class="hl">prepare</span> statement can be shared by - assuming the queries
only differ in the data we pass to them. We create an SQL statement using
question marks (<span class="hl">?</span>) as place-holders instead of actual values.
</p>
<p>
This call returns a <b>statement handle object</b> that we usually save in a
variable called <span class="hl">$sth</span>.
</p>
<p>
Then we call the <span class="hl">execute</span> method of the <b>statement handle</b> passing to
it values that should be placed instead of the place-holders.
</p>
<p>
The third step is the really interesting one.
In a <b>while loop</b> we fetch the results, row-by row. For this we can use several methods:
</p>
<p>
The <span class="hl">fetchrow_array</span> method will return the values of the next row in the result set
as a list, that we can assign to an array. The order of the elements is as the order
of the fields in the query. (fname, lname in our case).
</p>
<p>
The <span class="hl">fetchrow_hashref</span> method will return a reference to a hash. In the hash
the keys will be the names of the fields in the database. As different databases might
return these names of the fields in different cases we configured our database handler
to make sure they - the names of the fields - will always converted to lower case.
(That's what the <span class="hl">FetchHashKeyName</span> parameter did, when connecting to the database.)
</p>
<pre class="sh_perl">
my $sql = 'SELECT fname, lname FROM people WHERE id > ? AND id < ?';
my $sth = $dbh->prepare($sql);
$sth->execute(1, 10);
while (my @row = $sth->fetchrow_array) {
print "fname: $row[0] lname: $row[1]\n";
}
$sth->execute(12, 17);
while (my $row = $sth->fetchrow_hashref) {
print "fname: $row->{fname} lname: $row->{lname}\n";
}
</pre>
<p>
<h2>Exercise</h2>
</p>
<p>
Take the above snippets of code. Use the first one to set up the
database and create a table. Then use the second one to insert
a few people in the table.
</p>
<p>
Finally use the last example to extract some data from the database
and print it out.
</p>
<p>
If you have any question, feel free to ask below.
</p>
<p>
<h2>Thanks</h2>
</p>
<p>
to sigzero for correcting a bug in the examples!
</p>
<p>
<h2>Subscribe to my mailing list</h2>
</p>
<p>
If you are interested getting updates when I post new articles,
please subscribe to my newsletter:
</p>
<p>
<div class="subscriberbox">
<!--<h1>Did you like this article?</h1>-->
<!--<h2>Get notified when I publish new content!</h2>-->
<form class="subscribe" action="http://szabgab.com/subscribe.html" method="POST" style="display:inline">
<span class="label">Enter your e-mail here:</span>
<input name="email" />
<span class="label"> and click here:</span>
<input type="hidden" name="list" value ="update" />
<input type="submit" value="Sign Me Up!" />
</form>
</div>
</p>
Gabor Szabo2012-04-05T22:12:40+00:00Perl, SQL, DBIPerl Test Automation Training in Madison, Wisconsin and in Kiev, Ukraine
http://szabgab.com/perl-test-automation-training-in-madison-wisconsin-and-kiev-ukraine.html
I have two public Test Automation Training courses scheduled:
The first one is planned to be on 10-11 May, the two days before
<a href="http://event.perlrussia.org/yr2012/">Perl Mova and YAPC::Russia</a>
in Kiev, Ukraine. If you are interested in this training, make sure you
<a href="http://szabgab.com/contact.html">contact me</a> ASAP.
The second one is
<a href="http://yapcna.org/conference/testing-workshop">Testing Workshop</a>
11-12 June 2012 in Madison, Wisconsin, USA, just before
<a href="http://yapcna.org/">YAPC::NA</a>.
You can register on the web site.
<p>For the full article visit <a href="http://szabgab.com/perl-test-automation-training-in-madison-wisconsin-and-kiev-ukraine.html">Perl Test Automation Training in Madison, Wisconsin and in Kiev, Ukraine</a></p>
Gabor Szabo2012-04-02T09:16:58+00:00Perl, testing, courseScalable Vector Graphics with Perl
http://szabgab.com/scalable-vector-graphics-with-perl.html
Scalable Vector Graphics allows you to create images that can scale up and
down without loss of quality.
I have already hear Tamir Lousky give two presentation on how to create
SVG images using Perl, so I thought I should give it a try.
<p>For the full article visit <a href="http://szabgab.com/scalable-vector-graphics-with-perl.html">Scalable Vector Graphics with Perl</a></p>
Gabor Szabo2012-03-31T12:47:01+00:00Perl, SVGHow to create a Perl Module for code reuse?
http://szabgab.com/how-to-create-a-perl-module-for-code-reuse.html
In your system you are creating more and more scripts that need to use the same function.
You know lots of Perl modules that allow you to use their functions and you also want it.
You just don't know how to create such module.
<p>For the full article visit <a href="http://szabgab.com/how-to-create-a-perl-module-for-code-reuse.html">How to create a Perl Module for code reuse?</a></p>
Gabor Szabo2012-03-28T10:57:35+00:00Perl