excalibor's Journal http://use.perl.org/~excalibor/journal/ excalibor's use Perl Journal en-us use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners. 2012-01-25T02:34:42+00:00 pudge pudge@perl.org Technology hourly 1 1970-01-01T00:00+00:00 excalibor's Journal http://use.perl.org/images/topics/useperl.gif http://use.perl.org/~excalibor/journal/ Promises, promises... http://use.perl.org/~excalibor/journal/31128?from=rss <p>I've been thinking in lazy evaluation, and so, as of late, due to some healthy readings. </p><p>One thing that I've always liked about some languages (and which Perl 6 will have for free) is the ability to have lazy arrays, and list comprehensions (Haskell is the brightest example, but Python's gotten them for a good while now, and they have been proposed in a Scheme SRFI which is pretty cool). </p><p>The nice thing about Scheme's (or Python's) comprehensions is that they are eager, and that rings closer to Perl's world view than Haskell's. </p><p>Of course, we've always have something similar to a list comprehension in Perl, namely <i>map</i>. </p><p>For example, Haskell's simple example:</p><blockquote><div><p> <tt>Prelude&gt; let a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]<br>Prelude&gt; a<br>[0,1,2,3,4,5,6,7,8,9]<br>Prelude&gt; let b = [ x `mod` 2 == 0 | x &lt;- a ]<br>Prelude&gt; b<br>[True,False,True,False,True,False,True,False,True,False]</tt></p></div> </blockquote><p>where 'Prelude&gt;' is GHCi's prompt, can be easily written in Perl as</p><blockquote><div><p> <tt>$ perl -we '@a=0..9;@b=map { $_%2 == 0 ? 'True' : 'False' } @a; print "@b\n"'<br>False True False True False True False True False True</tt></p></div> </blockquote><p>Of course, Perl's thruth and falsehood vallues are different, we write 0 (or '', or ()) for 'False' and anything else for 'True' (for example, 0 and 1).</p><p>The <b>real</b> difference in both cases is which terms are calculated. If we limited this get just the first 3 elements of the comprehension, Haskell will only calculate those, and forget to test the rest of the elements. And if it's a calculated list, it will even skip the generation of the rest of the list. Perll will make all of it before slicing the result. A lot of unnecessary work. </p><p>We may have a hard time to make lazy list comprehensions in Perl, but we can make eager list comprehensions of sort pretty easily. This is <b>part I</b>. </p><p>And the trick is to resort to promises, like Scheme does. (There are probably other possibilities, but this is the one I know of<nobr> <wbr></nobr>:-P ): </p><p>We are going to implement a new type, a Promise, that, when created, will return the promise of a future evaluation. Thus:</p><blockquote><div><p> <tt>my $promise = delay(q[foo(2+4)]);</tt></p></div> </blockquote><p>is a promise to, eventually, evaluate foo(2+4), if forcefully asked for:</p><blockquote><div><p> <tt>my $result = force($promise);</tt></p></div> </blockquote><p>We are striving for a simple implementation to see how this works, the details are for CPAN, if I ever upload this over there. </p><p>Scheme's <i>delay</i> and <i>force</i> work <b>as if</b> defined this way:</p><blockquote><div><p> <tt>(define (delay x) (lambda () x))<br>(define (force p) (p))</tt></p></div> </blockquote><p>They are actually defined as macros, so the evaluation of the arguments is delayed until the macro is expanded. We can simulate this with Perl's ability to eval expressions. And thanks to Perl's closures, we can implement this straightforward:</p><blockquote><div><p> <tt>package Promise;<br> <br># a bit of debugging info, and we name the argument of delay() so we can close over it (lexically speaking)<br>sub delay { print STDERR "&lt;@_&gt;\n"; my $code = $_[0]; return sub { eval $code } }<br>sub force { $_[0]-&gt;() }<br> <br>1;</tt></p></div> </blockquote><p>and test it:</p><blockquote><div><p> <tt>use Promise;<br> <br>sub foo { $_[0] + 1 }<br> <br>my $p = Promise::delay(q[foo( 3 + 4 + foo(5) )]);<br>my $r = Promise::force($p);<br>print "* $r\n";</tt></p></div> </blockquote><p>Executing it, we get:</p><blockquote><div><p> <tt>$ perl test.pl<br>&lt;foo( 3 + 4 + foo(5) )&gt;<br>*</tt></p></div> </blockquote><p>Uh? The debugging sentence printed what we wanted to execute correctly, what went wrong? Well, if we had done our homework we would be capturing return value of the eval block, stored in $@, and we would then be able to raise the exception 'Undefined subroutine &amp;Promise::foo called at (eval 1) line 1.'. Let's do it:</p><blockquote><div><p> <tt>(in Promise.pm):<br>package Promise;<br> <br>sub delay { print STDERR "&lt;@_&gt;\n"; my $code = $_[0]; return sub { eval $code; die $@ if $@ } }<br>sub force { $_[0]-&gt;() }<br> <br>1;<br>(in test.pl):<br>use Promise;<br> <br>sub foo { $_[0] + 1 }<br> <br>my $p = Promise::delay(q[foo( 3 + 4 + foo(5) )]);<br>my $r = Promise::force($p);<br>print "* $r\n";<br>__END__</tt></p></div> </blockquote><p>And now we get:</p><blockquote><div><p> <tt>$ perl test.pl<br>&lt;foo( 3 + 4 + foo(5) )&gt;<br>Undefined subroutine &amp;Promise::foo called at (eval 1) line 1.</tt></p></div> </blockquote><p>as expected. Why is this failing? Because the symbol 'foo' is not defined in the Promise:: package, we have defined it in main::<nobr> <wbr></nobr>... There's surely a way to 'upval' <i>foo</i> from main:: to Promise:: (like Tcl does), but we don't have to, for the time being, because we can fully qualify our code-to-be.</p><blockquote><div><p> <tt>(test.pl):<br>use Promise;<br> <br>sub foo { $_[0] + 1 }<br> <br>my $p = Promise::delay(q[::foo( 3 + 4 +<nobr> <wbr></nobr>::foo(5) )]);<br>my $r = Promise::force($p);<br>print "* $r\n";<br>__END__</tt></p></div> </blockquote><p>No exception now, but it doesn't work. Before the error check, we were returning the eval, now we aren't. Let's fix it:</p><blockquote><div><p> <tt>package Promise;<br> <br>sub delay { print STDERR "&lt;@_&gt;\n"; my $code = $_[0]; return sub { my $ret = eval $code; die $@ if $@; $ret } }<br>sub force { $_[0]-&gt;() }<br> <br>1;<br>(in the shell):<br>$ perl test.pl<br>&lt;::foo( 3 + 4 +<nobr> <wbr></nobr>::foo(5) )&gt;<br>* 14</tt></p></div> </blockquote><p>Now it works! </p><p>Of course, this is a really na&#239;ve implementation, and it may be particularly awful, as Perl doen't optimize its tail calls, but it's a beginning. Another problem is that we may be recalculating a promise again and again, which defeats its very purpose of easying calculations; in Scheme promises are fulfilled once, and the next time you ask for it, you get the result instead of a promise object. Memoize can help in here and save us a lot of cruft, or not, depending on how we do construct our code to be evaluated. Actually evaluating a string is suboptimal, but it's the easier way to avoid Perl's eager evaluation of function parameters. There surely are other ways. </p><p>Anyway, to check that this is really working, let's try something a little bit more involved. Let's calculate numbers. A (potentially) infinite chain of numbers:</p><blockquote><div><p> <tt>(in test_naturals.pl):<br>use Promise;<br># returns a number and a promise to calculate the next one later<br>sub nat {<br>&nbsp; &nbsp; my $N = shift;<br>&nbsp; &nbsp; return [ $N, Promise::delay(qq{::nat($N+1)}) ];<br>};<br> <br># getters<br>sub get_N { $_[0]-&gt;[0] }<br>sub get_next { $_[0]-&gt;[1] }<br># pretty-printer<br>sub show_N { print get_N($_[0]), "\n" }<br> <br># the number one<br>my $one = nat(1);<br>show_N($one);<br># the next Natural is, of course, the number two<br>my $two = Promise::force(get_next($one));<br>show_N($two);<br> <br># let's get some of them...<br>my @naturals;<br>my $number = nat(1);<br>my $num = get_N($number);<br>while ( $num &lt;= 10 )<br>{<br>&nbsp; &nbsp; push @naturals, $num;<br>&nbsp; &nbsp; $number = Promise::force(get_next($number));<br>&nbsp; &nbsp; $num = get_N($number);<br>}<br> <br>print "@naturals\n";</tt></p></div> </blockquote><p>We create some convenience functions but don't let 'em fool you. nat() defines a function that takes a number, and returns it alongside a promise to calculate the next one. </p><p>We check that it works, and then we calculate the first ten Natural numbers. Note that after calling nat(1) to assign it to $number right before the loop, we don't use the fuction never again explicitely. It's implicitely called by each promise we force to be fulfilled. </p><p>The popular name for these 'objects' is Generators. I'm sure there are pretty good implementations of these in books (like in MJD's <i>Higher Order...</i>); but this is the basis, and I didn't find any in CPAN. Hope you find this idea useful, because it is more powerful than it may appear at first (deceptively simple, uh?<nobr> <wbr></nobr>;-) </p><p>Comments and discussion welcome! Laters!</p> excalibor 2006-09-26T14:19:23+00:00 journal Catalized http://use.perl.org/~excalibor/journal/31049?from=rss <p>OK, I have finally managed to install Catalyst. </p><p>It's been kind of nightmarish, and I will recall in this post the whole process, so we have enough background to back up our musings and conclusions. </p><p>First, as I have commented before on this journal, CPAN and Ubuntu packaging (apt) don't like each other very much. I guess it would take some tinckering of perl packages so the post-scripts update the <i>perllocal.pod</i> file or something like that, at least for every package installed. Using `dh-make-perl' won't do unless someone finds a way to debianizing all the accompanying dependencies found by CPAN-pm in the process; otherwise it becomes too cumbersome. </p><p>Following advice, I've made a parallel Perl installation, version 5.8.8 from scratch. Compiling and installing was easy, then came the surprise: LWP was not installed, and CPAN.pm wouldn't deal with my authenticating proxy (!!!). It kept dropping my <i>proxy_user</i> and <i>proxy_pass</i> variables, and not doing what I supposed it had to do. As we have to put our realm when authenticating against the proxy (username@realm) if furthers confuses some tools parsers when dealing with the <i>http://username@realm:password@server:port/</i>; which is frustrating. </p><p>Finally, using privoxy on my local machine so it would provide user and password to the real proxy, I managed to confugure curl to move through the whole process, and then some waiting until CPAN managed to get the files, etc. </p><p>Using the <i>cat-install</i> script was certainly painless, I simply had to install by hand a couple of modules I wasn't able to automatically download because they were blocked by my local proxy (argh!), and then installing Catalyst::Devel was simple enough as well. </p><p>In the end I only had to install some 234 CPAN packages, and it was working. As an aside I think it's a big bunch of packages, really. I still think a <i>batteries-included</i> Catalyst distribution with all those packages nicely tarballed so you only install those still missing on the system would be a nice idea. </p><p>Now I have a <tt>Test</tt> application available in my local port 3000, and I'll be toying around for a while until I get the gist of it. I'll probably rant about it on this blog, anyway...<nobr> <wbr></nobr>:-) </p><p>In the meantime, the Revision 6 (almost!) of the Scheme Report (R6RS, at <a href="http://www.r6rs.org/">http://www.r6rs.org</a>) has hit the stores, with lots of nice things to ponder about and to drool a bit... Go and check it by yourself! </p><p>laters!</p> excalibor 2006-09-19T11:08:43+00:00 journal Concurrency http://use.perl.org/~excalibor/journal/30972?from=rss <p>While I wait for my freshly installed-from-sources Perl 5.8.8 to run the <tt>cat-install</tt> script to install <a href="http://www.catalystframework.org/">Catalyst</a>, I'll mumble about concurrency in Perl, now that I am trying to learn Erlang. </p><p>Concurrency can be doen at the level of process (fork(), which is fork(2) and friends on UNIXen) or at the level of thread. </p><p>Since Perl 5.8.x we have threads, although a bit shakey. However, threads mean semaphores, locks, etc... Ugly. </p><p>CPAN to the rescue: </p><p> <a href="http://search.cpan.org/~nilsonsfj/subs-parallel-0.07/lib/subs/parallel.pm">subs::parallel</a> looks very nice. I will have to stress it to see if it's really usable, but it puts perl threads to the use in a very simple way, I like it. </p><p> <a href="http://search.cpan.org/~odigity/Parallel-Simple-0.01/lib/Parallel/Simple.pm">Parallel::Simple</a> also looks very nice, although it uses fork(). This has also problems, but nowadays fork() is very fast and cheap in UNIXen. </p><p>If you have to run the same thing many times, and it can run in parallel, <a href="http://search.cpan.org/~blhotsky/Parallel-ForkControl-0.02/lib/Parallel/ForkControl.pm">Parallel::ForkControl</a> is very nice. I've used it in the past to run control code on a <b>big</b> subnet, limiting the amount of bandwidth I sucked up, and it improved my partner's program by 3 orders of magnitude (yeah, that's right, 3 hours--counting the sleep we added on purpose--instead of 3 months) by improving the use of the IO bottleneck...<nobr> <wbr></nobr>:-) </p><p>There are other CPAN modules so you don't have to <tt>use threads</tt> or <tt>fork()</tt> yourself. Have a look on the module namespaces Proc::*, Parallel::* or forks::*, for example, and see which one suites your needs the best. </p><p>BTW, nothing of this is similar to Erlangs lightweight processess, which use message passing to work, and I don't think we could make a similar thing that worked easily enough. </p><p> <a href="http://toute.ca/">Termite</a> is a Scheme implementation of Erlang's concurrency model, and a couple of changes to the compiler/interpreter were necessary to implement it in Scheme, so I guess some changes would be needed in perl itself (i.e. in the compiler/interpreter). </p><p>Simulating Erlang's ability in perl is a different story, which I may tackle one of these days if I have time enough...<nobr> <wbr></nobr>:-P </p><p>In the meantime, I'm back to fighting with CPAN.pm and my proxy, which is stupid enough to be really annoying (argh!). BTW, any way to let CPAN know it has to use the HTTP methods instead of FTP? Using the fully qualified HTTP URI of my proxy doesn't seem to work...<nobr> <wbr></nobr>:-/ </p><p>Best regards!</p> excalibor 2006-09-12T16:33:18+00:00 journal Packaging complexity (huge!) http://use.perl.org/~excalibor/journal/30620?from=rss <p>The other day, the fine folks at Hiveminder announced his new site, made with Perl, using Jifty (<a href="http://use.perl.org/article.pl?sid=06/08/08/0249201">http://use.perl.org/article.pl?sid=06/08/08/0249201</a>). </p><p>I thought, let's have a look, and went to go for Jifty. It looks really cool! Maybe even as cool as Catalyst looks, yeah. </p><p>Holy cow! After three days fighting with CPAN modules, dependencies, failed tests, etc, I have given up. </p><p>It probably has to do with the packaging system of my GNU/Linux box and CPAN, which don't go along very well (I mean, there's a difference between doing <i>aptitude install libtest-pod-perl</i> and <i>perl -MCPAN -e 'install Test::Pod'</i>, for example). The difference is that in the first case, <b>apt</b> is dealing with dependencies, paths, etc; in the former, it's <b>CPAN</b>, and they don't think the same way. </p><p>This is, of course, very frustrating. I had the same experience with Catalyst, and I also gave up (that's why I said that Jifty looks as cool as Catalyst looks, because I haven't been able to try none of them! </p><p>My rant is that these framworks are cool, and use a host of standard modules (in the CPAN sense) but they should be available 'batteries included'. I mean: you should not be so fragile as to be useless without all CPAN at hand. </p><p>I work behind a firewall, and CPAN access is, let's say, unconvenient. This may be the best of both worlds way of doing this: you offer the option of downloading a tarball with everything that's needed to get the framework going on, and then Makefile.PL (or Makefile, or another program) checks which of the CPAN packages it has downloaded within the tarball it really needs to install on the system. </p><p>This is the best of both worlds: I can make a Debian package from the tarball (carefully defining dependencies and so) and use <i>apt</i> to install the packages I am missing (so my system has track of paths, versions, and dependencies) or use CPAN from the local repository. </p><p>But I was not offer such a thing. Either it's available on debian packages or in CPAN tarballs, but I have to go fetching dependencies, and some fail (because I have something the way that package doesn't like) and I'm screwed. </p><p>So, what possibilities are there to simplify all these complexity so huge frameworks don't need a fresh system (maybe even compiled from the sources) to live happily in production boxes? </p><p>Will CPANPLUS solve this? Because if Perl 6 won't be able to benefit from CPAN easily (and I cannot, always, benefit from it by using Perl 5!) then neither Perl 6 nor these frameworks will have an easy path to success in the real world. Specially the web frameworks, with Ruby on Rails, Django, webpy, and all the Java frameworks out there... </p><p>(enough ranting, back to work)</p> excalibor 2006-08-14T08:46:50+00:00 journal Back! http://use.perl.org/~excalibor/journal/30514?from=rss <p>Well, after a long, long time, I'm back.</p><p>Not that it's such great news, actually, but, well, it's what's here to be (sorta).</p><p>I'll link this blog to my main blogging site (about the historical fiction writing I am doing) which is also in ENglish, so you can check over there as well.</p><p>As I'm still being paid to program in Perl, I find myself happy and longing job offers from my homeland, as I'd love to move back there.</p><p>Thus, I'll rant about all those Java offers, and about the wonderful things of daily Perl programming, and some things I'd love to find fixed (so I don't have to fix them myself<nobr> <wbr></nobr>;-)</p><p>keep tuned if you want, and laters!</p> excalibor 2006-08-02T13:09:02+00:00 journal