Let us consider the following code to loop over the keys from a dictionary in Python:
In Python you have to allocate a temporary variable, sort it, then loop over the result:
[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):
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):
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.
* 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.
- Mood:
accomplished
my $foo = {};
sprintf("%s(0x%x)",ref $foo,$foo+0) eq "$foo" # TrueI hacked together a little module today that will let you keep some of your Perl modules in a database. You "use" them as normal but instead of loading them off the file system they get loaded from the database.
It's really easy to write but potentially quite useful when you want to get the same set of code distributed around to a bunch of different database using Perl apps. I find it some what mysterious that such a thing does not already exist.
http://turner.mikomi.org/DBIx-INC-0.01.t ar.gz
( Read more... )
It's really easy to write but potentially quite useful when you want to get the same set of code distributed around to a bunch of different database using Perl apps. I find it some what mysterious that such a thing does not already exist.
http://turner.mikomi.org/DBIx-INC-0.01.t
( Read more... )
Say we have a string...
Sometimes the .html will be there, sometimes it won't. For some reason I was thinking that I couldn't do this by using a non-greedy match. As it turns out, you can:
So instead I wrote the same thing using zero-width forward and reverse assertions:
Certainly using a non-greedy match is a lot simpler and easier to read. It turns out that the zero-width assertions are a bit faster. Even so, using it would be dumb, considering how convoluted it is and the fact that it's only faster over huge numbers of iterations:
Cross posted to =perl
$foo = "this is a test.html";Sometimes the .html will be there, sometimes it won't. For some reason I was thinking that I couldn't do this by using a non-greedy match. As it turns out, you can:
$re = qr/^(.*?)(\.html)?$/;So instead I wrote the same thing using zero-width forward and reverse assertions:
$re1 = qr/(.*)(?<!\.html)(?<!\.htm(?=l))(?<!\.h t(?=ml))(?<!\.h(?=tml))(?<!\.(?=html))/;Certainly using a non-greedy match is a lot simpler and easier to read. It turns out that the zero-width assertions are a bit faster. Even so, using it would be dumb, considering how convoluted it is and the fact that it's only faster over huge numbers of iterations:
Benchmark: timing 50000 iterations of nongreedy, zerowidth...
nongreedy: 1 wallclock secs ( 0.89 usr + 0.00 sys = 0.89 CPU) @ 56179.78/s (n=50000)
zerowidth: 1 wallclock secs ( 0.67 usr + 0.00 sys = 0.67 CPU) @ 74626.87/s (n=50000)
Cross posted to =perl
- Mood:
amused
I figured out how to do tail recursion in Perl 5. I've managed to find very meager results on Google. It doesn't look like it's a very common topic. It deserves some as there are defintely times that the the resource usage of Perl's normal recursion is just not acceptable and an iterative solution is too ugly.
Interestingly, the most obvious solution is actually slower (though it does still use much less memory). It's most effecient to use package globals, but at that point you may as well simulate it in a while loop using lexicals and get the 3x speed up that not using goto gives you.
I'm probably going to post this to the LJ Perl community too, so let me appoligize in advance for the dupe to those of you subscribed to that.
( Read more... )
Interestingly, the most obvious solution is actually slower (though it does still use much less memory). It's most effecient to use package globals, but at that point you may as well simulate it in a while loop using lexicals and get the 3x speed up that not using goto gives you.
I'm probably going to post this to the LJ Perl community too, so let me appoligize in advance for the dupe to those of you subscribed to that.
( Read more... )
Catching up on my email I see that a new DBI has been released. It has a modest set of in additions, including the long awaited:
Added $dbh->last_insert_id method.
Added $dbh->last_insert_id method.
Came across this today in the Perl 6 summary. It's lovely.
It was coined by Bryan C. Warnock in a post to a Perl mailing list:
http://archive.develooper.com/bootstrap@p erl.org/msg01126.html
It's the dilemma that everyone has faced when posting a message to a public forum and hearing only silence.
It is described concisely here:
http://mar.anomy.net/entry/2003/07/20/21.1 8.32/
It was coined by Bryan C. Warnock in a post to a Perl mailing list:
http://archive.develooper.com/bootstrap@p
It's the dilemma that everyone has faced when posting a message to a public forum and hearing only silence.
It is described concisely here:
http://mar.anomy.net/entry/2003/07/20/21.1
I've setup an external version of my journal here:
http://journal.mikomi.org/
I did this by creating a custom style that outputs XML. Then I just used my XSLT Mason component to transform it back into HTML. I caches the result for an hour.
You can view other people's journals (if they have a paid account). Try viewing your own, if you'd like:
Edit:This no longer works for other users. It refuses to load the style. Boo!
http://journal.mikomi.org/
I did this by creating a custom style that outputs XML. Then I just used my XSLT Mason component to transform it back into HTML. I caches the result for an hour.
You can view other people's journals (if they have a paid account). Try viewing your own, if you'd like:
Edit:This no longer works for other users. It refuses to load the style. Boo!
So I hacked together a simple Live Journal mail gateway. That is, an email address that forwards the messages it receives to a LJ blog. It's based on a simple LJ module that I wrote a little while back using XML-RPC. It is through this feature that I am posting this entry.
If you'd like to see details you can:
( Read more... )
If you'd like to see details you can:
( Read more... )
The couple of weeks has shown discussion on adding "hash seed randomization" on the Perl 5 Porters list, or at least, the summaries on use Perl tell me this. Anyway, it seems that there is a good bit of code out there that depends on hash results being in a specific order. Expect much breakage.
Read More
Read More
I've been feeding my fascination with language lately. A Slashdot post led to my discovery of the American Dialect Society mailing lists (ADS-L), which I've joined. They're the most fun I've had reading a mailing list in a couple years. Mostly it involves antedating older American words, though there are a nice selection of neologisms sprinkled in.
On the other hand, reading Perl lists is starting to feel like work. This makes me sad.
Anyway, the reason I bring up the ADS-L is that it is from it that I learned of the Modern Language Association. I joined them... now I get to find out if this is something I'll find worthwhile. And they have a radio show called What's the Word? Of the shows that I listened to all were interesting. For instance, the show from 1997 on censorship is well worth a listen. (No direct link because their site doesn't lend itself to that.)
On the other hand, reading Perl lists is starting to feel like work. This makes me sad.
Anyway, the reason I bring up the ADS-L is that it is from it that I learned of the Modern Language Association. I joined them... now I get to find out if this is something I'll find worthwhile. And they have a radio show called What's the Word? Of the shows that I listened to all were interesting. For instance, the show from 1997 on censorship is well worth a listen. (No direct link because their site doesn't lend itself to that.)
