Home

Mutators

  • Nov. 8th, 2007 at 2:06 PM
obscure
Let us consider the following code to loop over the keys from a dictionary in Python:

  for key in a_dict.keys():
This would be nearly the same in Perl:

  for my $key in (keys %a_dict) {
And Perl 6*:

  for %a_dict.k -> my $key {
But now you say: I want to have in a sorted rather then arbitrary order!

In Python you have to allocate a temporary variable, sort it, then loop over the result:

  the_keys = a_dict.keys()
  the_keys.sort()
  for key in the_keys:
Where as Perl takes a functional approach that is, in my opinion, a lot clearer:

  for my $key in (sort keys %a_dict) {
And Perl 6*:

  for sort %a_dict.k -> my $key {
* I vaguely recall something about the temporary variables used in loops not requiring an explicit "my" but I can't seem to find something saying that explicitly now.

[Edit] So as many people pointed out to me, this was fixed for Python as of 2.4 (my laptop and work machines all use 2.3) with: for key in sorted(a_dict.keys()): What's more, the .keys() is assumed so you can actually just do for key in a_dict: or for key in sorted(a_dict):

Mar. 2nd, 2006

  • 4:28 PM
obscure
I think I just leveled up my sql skill. )
That's probably the most complicated query I've ever written and it's massively faster then the much simpler query that I had been using. I went from several minutes down to a hundreth of a second or so. I note that I'm actually using three distinct types of sub-selects, in addition to both inner joins and left joins.

Feed parsing in Perl

  • Aug. 31st, 2005 at 4:52 PM
obscure
For the last month or so I've been writing a feed parser for Perl in the same vein as Python's Universal Feed Parser.  The closest to this that I could find was XML::RAI, which does a nice job of abstracting the different RSS formats but doesn't support Atom and has no facility for getting info about elements that the parser doesn't know about.  It's also based on XML::Parser and is thus unwilling to continue after a well-formedness error*.

* This is in compliance with the XML specification.  The XML specification is dumb in this respect.

And so, I present to you the likely soon to be renamed:

Feed::Parser

(The HTMLized PODs look dumb because pod2html sucks.  search.cpan.org and perldoc.perl.org have really nice pod renderers but they don't seem to be publically available.)

It can handle Atom 0.3, Atom 1.0, RSS 1.0, RSS 0.91-0.94, RSS 2.0, and 25 extension namespaces.  It can meaningfully parse and provide access to namespaces that it knows nothing about.

The API is feed type agnostic.  You can use the RSS or the Atom names interchangably.

Perl detail of the day:

  • Aug. 31st, 2005 at 3:20 PM
obscure
my $foo = {};
sprintf("%s(0x%x)",ref $foo,$foo+0) eq "$foo" # True

Stupid Perl 6 Tricks

  • Aug. 6th, 2005 at 6:31 PM
obscure
Revised
#!/usr/local/bin/pugs
use v6;

class Currency {
 )
}

my $money = USD 50 + 25¢ + £12.50 + 1200円;
say $money.str; # USD 83.21
my $foo = $money€;
say $foo.str;   # EUR 67.51

Jan. 28th, 2004

  • 12:24 PM
obscure
I recently discovered Eric Sink's weblog.  He writes about software and small business.  He's probably best known for being the guy behind Abiword.  Most recently he's been getting passed around blogs for his Career Calculus article. He was also slashdotted for his MSDN article Make More Mistakes.

Anyway, there are a couple of his pieces that I felt I should share.  I think they're worth checking out.

  • Small ISVs: You need Developers, not Programmers: Small companies need flexibility from their employees, not a single task mentality, even from their programmers.  I'm a bit biased against the term "developer," but I think that was due to poisoning by the term "web developer," which was liberally applied to almost any one updating HTML.
  • Marketing for Geeks: Lots of great discussion regarding how to go about marketing.
There are more I'm sure, but I haven't worked my through them all yet.

Latest Month

January 2008
S M T W T F S
  12345
6789101112
13141516171819
20212223242526
2728293031  

Tags

Syndicate

RSS Atom
Powered by LiveJournal.com