Get real random numbers from Perl's rand()

On Stackoverflow, someone asks how to get 100 random numbers without a loop. It's one of those dumb homework problems that tries to forbid only one of many things instead of specifying the technique it really wants the student to practice. In Perl land, that leaves the door open for the sick and twisted minds of people such as Tom Christiansen and Sinan Ünür. I wonder if the teacher would even understand their solutions, much less accept them.

Many of the answers went to great pains to avoid certain Perl keywords while still creating loops. Some people debated if map is a loop. Some people used recursion, forgetting that every time you recurse in Perl, God kills a kitten. Maybe someday people will realize that recursion in Perl isn't the same kung fu they see in other languages. Perl actually must recurse because it has no way of knowing if it's going to call the same function definition.

Most of the solutions used Perl's built-in rand, which I think ignores half of the problem, the random numbers themselves. I use rand too, but I replace its definition to use the random.org web service to get lists of random numbers generated from atmospheric noise. Not only that, but I change rand in very sick and twisted ways, adding a list context.

Javascript scoping != Perl's

I'm doing a lot of jQuery of one of my current clients. Along the way I'm learning fun Javascript gotchas. Here's a demonstration of one scoping difference. Below the .change() event tied to bar works as I expected, but baz is a global variable.

function addARow() {
   var i = $('#next_id').val();
   var d = $("div"),                                      //COMMA then foo
   foo = $("<input>",{id:"foo" + i,size:"3"}).appendTo(d)
   .change(function() { bar.val($(this).val() * 2); })
   .change(function() { baz.val($(this).val() * 2); }),   //COMMA then bar
   bar = $("<input>",{id:"bar" + i,size:"3"}).appendTo(d);//SEMICOLON then baz
   baz = $("<input>",{id:"baz" + i,size:"3"}).appendTo(d);
   $("<br>").appendTo(d);
   $('#next_id').val(i - 1 + 2);   // + 1 would concat 1 onto the string  :)
};

See it in action here. jQuery creates a set of 3 input fields (foo, bar, baz) on each click of "add". When foo is changed by the user, bar and baz are updated. But the wrong baz gets the update.

Live and learn. :)

PgWest 2010

PgWest 2010

Day one was good. I learned about GUCS, sharding, backups and recovery. We met at Cafe Royale last night for a drink, courtesy of PostgreSQL Experts Inc and the San Francisco PostgreSQL Users Group.

Day 2 just came to a close. I got to hear Scott McNealy speak about a variety of topics. I learned about performance pitfalls, Node.js/Postgres.js, database-driven cache invalidation and the system tables. Lots of good info.

This evening EnterpriseDB is providing dinner and drinks at Harry Denton's Starlight Room this evening.

Help name my code! I am terrible at it.

I write a lot of apps that need a simple database. I tend to use SQLite, but I found I was implementing the same things over and over again. I finally started wrapping it up in a role that I can reuse, but now I'm stuck on a name. The role provides the following:

  • Database connect/disconnect
  • Automatic creation of db if it doesn't already exist
  • Transactions
  • Blocking locks (SQLite's locking kept biting me, so I worked around it using flock)


The name I picked out of the blue was DBIx::Cradle. It sort of makes sense, but I don't really like it. It is hardly an extension of DBI, so DBIx doesn't feel right, even though it does provide some general purpose database goodness. The Cradle part is supposed to mean it makes life easy and comfortable, but again, doesn't feel quite right.

Any suggestions? Any other similar modules I should look at?

PS: In case anyone remembers my earlier post about database abstraction, you might notice I'm backtracking a bit. These things happen. The comments pushed me away from that course of action, and this role has simplified things enough that I'm a lot happier now.

Perl101: Encapsulation via Protected Abstract Methods

Imagine you have an employee base class, but you know that the salary calculation will be different per employee type. You might decide to do this:

package Employee;

use strict;
use warnings;
use Carp 'croak';

sub new { bless {} => shift }

sub salary { croak 'You must override salary() in a subclass' }

1;

Listing All Installed Programs in Windows XP

I wanted to get a list of all the programs installed on my computer listed in 'Add or Remove Programs' in the Control Panel in Windows XP. Win32::TieRegistry provided a nice way to do this.

CPAN Testers Summary - October 2010 - Nine In A Pond Is Here

Back in January 2008 we were celebrating the one millionth post submitted to CPAN Testers. Although that article proclaimed it to be the one millionth report, many initial posts to the mailing list also included discussions and announcements of uploads. It wasn't until I created the Interesting Stats page that we started to see the true picture. However, we only had to wait until March 2008 for the real one millionth report to be posted. Now some 2 years and 7 months later we've had the nine millionth report submitted. It took 9 years to produce 1 million reports, but only a further 2½ years to produce another 8 million reports. The rate at which CPAN Testers has been able to get people involved in the project has been phenomenal. We are now submitting over 500,000 reports a month, so I have no doubt we will pass the 10 millionth mark before the end of the year .. probably just before Christmas :)

Marpa's Sudden Portability Adventure

I've made bold claims for the portability of Marpa. [ Marpa is a general BNF parser generator -- it parses from any grammar that you can write in BNF. If the grammar is of one of the kinds currently in practical use (yacc, LR(k), LALR, LL, recursive descent, etc.), this parsing is in linear time. ]

The boldness of my claims evinced no ambition to test them under fire. But, when my main development box (a 2-year Dell laptop running Ubuntu) suddenly died, my only other choice for a development platform was a MacBook G4 running Mac OS Tiger. So test my portability claims is what I had to do.

Not that my brags about portability had been without foundation. I upload development versions for cpantesters regularly. (By the way, to all of you at cpantesters: a big "thank you".) As far as the configuration, build and runtime environments went, I had a lot of reason to be confident.

My Programming-Related Todo List

Dave Rolsky posted his Programming-Related Todo List, and here's mine:

  • Make a proper programming-related to-do list
  • Write that new CPAN client I was talking about
  • I've been indexing BackPAN just fine, but I have to figure out a way to make all of the data available to people in a sane way. The plain text uncompressed data for about 140,000 distros is several gigabytes. I have half of a web service written, and even have mycpan.com to host it. I just need the tuits.
  • Get my CPAN stats and trending project going again. It used to be part of The Perl Review, but I just need to set up the cron jobs again and maybe use some prettier charting tools.

There are some modules that I want to write, or fool someone else into writing:

Internal redirects in Dancer

I really like the forward function in Catalyst. It lets you chain actions together and create a clean flow. I decided I want that in Dancer. We now have it.

Some background:
I was working with my brother on a project with Dancer. We wanted a client to be able to go to a personal demo page of the website to be able to try out editing features without destroying the actual website. The original (awful) code did this by copying all the (CGI) code into a different folder and making that available.

Our idea was to have a demo database that starts as a replication of the existing one and then can be mutated and twisted with editing tests. The data is no longer important, just that there's something to play with.

Using Plack to "Like" a Module on Facebook

We've made a couple of fun changes to cpan-mangler. First off, you can now "like" modules on Facebook:

mangler-facebook.png

Now, up and downvoting of modules would be a lot more useful, but this can be fun (and confusing for your friends):

mangler-facebook-like.png

Secondly, Lee Aylward added support for HTML::Highlighter:

mangler-highlighter.png

Thirdly, we've added a tweaked version of Jesse Thompson's CPAN Dependents Greasemonkey script.

mangler-search.png

If you've already got cpan-mangler installed, a "git pull" should get you going (you'll likely need to install some additional modules). If you haven't, you can clone the repo and get yourself started in just a couple of minutes. Have fun with it!

random thoughts on YAPC::Asia

YAPC::Asia 2010 is over. Well, it actually ended more than two weeks ago. It was fabulous as a whole. Larry's keynote was quite interesting. Jesse's was fabulous in many ways (including that nico-nico-ish twitter stream). Miyagawa-san's was moving. This year we also had a special session where Japanese perl mongers group leaders were invited to discuss issues and encourage people to join or start another. As a consequence, a few new perl mongers groups were born and some more may come. I'm really glad at that.

"Works for me!"

For some time that on different situations I have heard programmers answer and close bug reports with a "Works for me!" sentence.

I think that kind of answer is from somebody that doesn't want to bother asking for details on what is going on, and making his/her software better. When a programmer answers this, why is he or she making his code available? Or, if he or she is making the code available `as is', why they create a project page and the ability to submit bug reports? Also, if it works for you and you are not interested why that piece of code is not working elsewhere, I can't understand why we, at Perl community, have CPAN Testers.

Note that there are situations and situations, and I can agree that in some situations this would be a suitable answer.

But what I would like to say here is, please try to reduce that amount of "Works for me!" sentences per square meter.

Perl 6 on FLOSS Weekly

FLOSS Weekly's Randal Schwartz interviews Patrick Michaud about Perl 6

First Grant Report: Perl 6 Tablets

Not that I was too lazy but my time was very compartmentalized. So i just start slowly and since I don't get bucks til its done, you don't have to worry anyway. I just completed Tablet 2. Its a nice and dense overview what Perl 6 is about and whith which mindset Larry is designing Perl 6. (already got very positive feedback from #perl6 people).

On other hand It's small and not part of the official grant, but it belongs to the overall concept of the Perl 6 Tablets which is much more work anyway. It was not the only thing I've done but for now i mention this because its completed and you are invited to write your comment of extentions here or in the wiki.

Thanks.

P.S.

thanks to lue for fixing typos

Multiple packages in one file

I was reading an interesting discussion on python-dev, and it made me think about the analogous situation in Perl. I've long been in the habit of putting each package into its own file, no matter what. Now I'm starting to consider combining related packages into one file, and only breaking things up along lines of reuse.

I initially thought there was consensus in Perl circles to have a single file per package, but on further reflection, I started to doubt myself. I don't actually have much confidence that this is true. I don't usually look at the file structure of distributions I use from CPAN. Maybe there is more combining than I realized. Any thoughts?

Remove a Git Tag From Github When There is Also a Branch of the Same Name

I have been using git and Github off and on for a while now but I’ve never really learned much about it.

Today, I mistakenly added a tag with the same name as a branch and pushed it to Github. I was able to get rid of it locally but I was pulling my hair out trying to figure out to remove it from Github. I finally stumbled on the solution so here is what I learned.

Removing a git tag:

git tag -d <tag>

Removing a git branch:

git branch -d <branch>

Deleting a tag or branch from Github:

git push origin :<tag or branch>

Deleting a tag (with the same name as a branch) from Github:

git push origin :refs/tags/<tag>

Deleting a branch (with the same name as a tag) from Github:

git push origin :refs/heads/<branch>

9 Million Test Reports!

As the Statistics site wasn't being regularly updated for the past couple of weeks, the latest milestone on the Interesting Stats page of the CPAN Testers Statistics site was way down the watch list. So I was surprised to see that we now have over 9 million test reports in the CPAN Testers eco-system. Many thanks to all the testers who have help to contribute to the milestone.

Congratulations once again go to Andreas for posting the 9 millionth report. It was a PASS for Log-Report-0.28.

App::Office::CMS - Close to release

Over the last couple of months, a startling array of events have transpired, and hence conspired to delay progress, but finally I have App::Office::CMS working at home.

I’ll shortly release a dev version (V < 1.00), so you can check the installation instructions, if you wish, and the docs.

I just have to finish off the POD, which is mostly written.

The dev versions will start by using textareas for input, not TinyMCE, the editor I’ll activate for V 1.00. After all, these versions are just for playing with.

Also, I have not yet integrated revision history, so there is no roll-back capability. I’m looking at Git::Repository for that. Only 1 copy of the data is stored, in a database via DBI.

Perl101: finding words in words

Recently a friend of mine back in the US mentioned an art project she was working on. She was looking for words which are composites of two words. She didn't necessarily want obvious composites like "bluebird", but less obvious composites that can be worked into her art project. For example, "homeless" could be "home less", or "garbage" to "garb age". A few people struggled to come up with examples. I came up with /usr/share/dict/words and a few lines of Perl. I use a few nifty idioms that every Perler should be familiar with.

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.