broquaint's Journal http://use.perl.org/~broquaint/journal/ broquaint'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:21:33+00:00 pudge pudge@perl.org Technology hourly 1 1970-01-01T00:00+00:00 broquaint's Journal http://use.perl.org/images/topics/useperl.gif http://use.perl.org/~broquaint/journal/ Params::Named - As requested http://use.perl.org/~broquaint/journal/27115?from=rss So I wrapped up the previously mentioned named parameter binding code into a module called <tt> <a href="http://search.cpan.org/perldoc?Params::Named">Params::Named</a> </tt> which should now be available on the CPAN. It's now more robust and deals with arrays and hashes to boot. Go crazy. broquaint 2005-10-11T15:13:41+00:00 journal Named arguments on Perl5. Sorta. http://use.perl.org/~broquaint/journal/27013?from=rss Here's a little bit of madness I cooked up for using named arguments natively in perl5:<blockquote><div><p> <tt>use strict;<br>use warnings;<br> <br>use PadWalker 'peek_sub';<br>use List::Util 'first';<br> <br>## Map named arguments to variables of those names.<br>sub getargs {<br>&nbsp; my %args = do { package DB; () = caller 1; @DB::args };<br>&nbsp; my $vals = peek_sub \&amp;{(caller 1)[3]};<br>&nbsp; my %res&nbsp; = map { $_ =&gt; $args{substr($_, 1)} }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;grep exists $args{substr($_, 1)}, keys %$vals;<br>&nbsp; my %map&nbsp; = map {<br>&nbsp; &nbsp; my $orig_arg&nbsp; = \$_;<br>&nbsp; &nbsp; my($ref_name) = first { $orig_arg == $vals-&gt;{$_} } keys %$vals;<br>&nbsp; &nbsp; $ref_name =&gt; $orig_arg;<br>&nbsp; } @_;<br> <br>&nbsp; ${ $map{$_} } = $res{$_}<br>&nbsp; &nbsp; for keys %res;<br> <br>&nbsp; return \%res;<br>}<br> <br>## Basic test of getargs();<br>sub foo {<br>&nbsp; getargs my($this, $that, $theother);<br>&nbsp; print "this is: $this\nthat is: $that\nand the other is: $theother\n";<br>}<br> <br>## Have these pairs mapped to variables of the same name.<br>foo this&nbsp; &nbsp; &nbsp;=&gt; "an argument",<br>&nbsp; &nbsp; that&nbsp; &nbsp; &nbsp;=&gt; "used to refer to things",<br>&nbsp; &nbsp; theother =&gt; "is something else altogether";</tt></p></div> </blockquote><p>This will output:</p><blockquote><div><p> <tt>this is: an argument<br>that is: used to refer to things<br>and the other is: is something else altogether</tt></p></div> </blockquote><p>So, as you can see, given a list of pairs the variable names matching the keys will be automagically assigned to the corresponding values. </p><p> I'd had it in the back of my mind that this could be achieved fairly simply, as demonstrated above, but as you can see it's not the most robust of methods. I'll CPAN-ify it if anyone wants a rather more sensible version of the code, and I'm sure documentation and tests wouldn't go amiss too.</p> broquaint 2005-10-04T14:16:39+00:00 journal Because you asked for it http://use.perl.org/~broquaint/journal/24891?from=rss For the folks who wanted <tt>C</tt> style switching behaviour in <tt> <a href="http://search.cpan.org/perldoc?Switch::Perlish">Switch::Perlish</a> </tt> (version 1.4):<blockquote><div><p> <tt>use Switch::Perlish 'C';<br> <br>my $food = shift;<br>switch $food, sub {<br>&nbsp; case 'pizza';<br>&nbsp; case 'burger';<br>&nbsp; case 'fried-chicken';<br>&nbsp; case 'tub-o-lard', sub {<br>&nbsp; &nbsp; print "Your food is probably delicious\n";<br>&nbsp; &nbsp; stop;<br>&nbsp; };<br>&nbsp; case 'celery';<br>&nbsp; case 'rice-cracker';<br>&nbsp; case 'plaster';<br>&nbsp; case 'grass', sub {<br>&nbsp; &nbsp; print "Your food is probably bland\n";<br>&nbsp; &nbsp; stop;<br>&nbsp; };<br>&nbsp; default sub {<br>&nbsp; &nbsp; print "Your food is without compare\n";<br>&nbsp; };<br>};</tt></p></div> </blockquote><p>You can also match mutliple things in the normal style using an anonymous array. i.e</p><blockquote><div><p> <tt>case [qw/ pizza burger fried-chicken tub-o-lard<nobr> <wbr></nobr>/], sub {<nobr> <wbr></nobr>... }</tt></p></div> </blockquote><p>Different keystrokes for different folks<nobr> <wbr></nobr>...</p> broquaint 2005-05-26T18:47:28+00:00 journal A shiny new switch() http://use.perl.org/~broquaint/journal/24805?from=rss I've just finished up a new <tt>switch</tt> module which should be coming to a CPAN mirror near you soon (and can be found <a href="http://www.broquaint.com/misc/Switch-Perlish-1.0.0.tar.gz">here</a> presently). It goes by the name of <tt> <a href="http://search.cpan.org/perldoc?Switch::Perlish">Switch::Perlish</a> </tt> and can be pointed to when foreigners ask where <tt>switch</tt> is in perl. Here's some obligatory example code as taken from the documentation:<blockquote><div><p> <tt>use Switch::Perlish;<br> <br>my $num = $ARGV[0];<br> <br>switch $num, sub {<br>&nbsp; case undef,<br>&nbsp; &nbsp; sub { die "Usage: $0 NUM\n" };<br>&nbsp; case [0<nobr> <wbr></nobr>.. 10],<br>&nbsp; &nbsp; sub { print "Your number was between 0 and 10" };<br>&nbsp; case [11<nobr> <wbr></nobr>.. 100],<br>&nbsp; &nbsp; sub { print "Your number was between 11 and 100" };<br>&nbsp; case [101<nobr> <wbr></nobr>.. 1000],<br>&nbsp; &nbsp; sub { print "Your number was between 101 and 1000" };<br>&nbsp; default<br>&nbsp; &nbsp; sub { print "Your number was less than 0 or greater than 1000" };<br>};</tt></p></div> </blockquote><p> <i>broquaint out</i></p> broquaint 2005-05-21T03:17:06+00:00 journal Not your usual perl http://use.perl.org/~broquaint/journal/9154?from=rss <a href="http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2002-11/msg00751.html">I kid you not</a><blockquote><div><p> <tt>$foo = "a string";<br>print "${";<br>foo} or is it?<br> <br>__output__<br> <br>a string or is it?</tt></p></div> </blockquote><p> <i>broquaint out</i></p> broquaint 2002-11-26T17:03:44+00:00 journal Y.A.T http://use.perl.org/~broquaint/journal/9059?from=rss Finally got <a href="http://www.perlmonks.org/index.pl?node_id=213855">Lexical scoping like a fox</a> cleared up and hopefully it'll provide the necessary information to give people 'clue' about lexical scoping and the like. <p> Now it's just a matter of time before I write the big<nobr> <wbr></nobr>.... <i>closure tutorial</i> (cue. lightning and a distant rumble of thunder). </p><p> <i>broquaint out</i></p> broquaint 2002-11-21T14:21:00+00:00 journal Stuff for learning http://use.perl.org/~broquaint/journal/8935?from=rss I wrote a nice little <a href="http://www.perlmonks.org/index.pl?node_id=211441">tutorial for Perl Monks</a> on symbol tables and globs. If I get around to it, I'll tidy it up a bit more and submit to p5p as YAMP. After that it'll be a tutorial for lexical scoping, and finally a tutorial for closures (that'll be fun<nobr> <wbr></nobr>...), then I shall sleep easier at night (or maybe that should be: I'll sleep at night<nobr> <wbr></nobr>;). <p> And I did figure out how to use <tt>extract_quotelike()</tt> in the end, and may CPAN-ify it some time in the future as it is awful handy if your source munging. </p><p> <i>broquaint out</i></p> broquaint 2002-11-13T18:10:45+00:00 journal Maybe the guards are off duty http://use.perl.org/~broquaint/journal/4960?from=rss Hmmm, I appear to be a <a href="http://www.perlmonks.org/index.pl?node=Saints+in+our+Book">saint</a> on Perl Monks. Interesting<nobr> <wbr></nobr>... <p> In other news I posted <a href="http://www.perlmonks.org/index.pl?node_id=166825">Sub::Lexical</a> on Perl Monks and realised there aren't as many bugs as I initially thought (although I imagine there are few a still nasties lurking). My main annoyance at the moment is that function calls are being 'interpolated' within strings so I need to figure out a way of applying <tt>extract_quotelike</tt> from Mr Conway's <tt>Text::Quotelike</tt> or do some more funky regex (I don't think P::RD will be necessary here, I think<nobr> <wbr></nobr>...). </p><p> Oh and I have my name on CPAN, in the albeit indirect way that demerphq finally put up <tt>Data::BFDump</tt> and <tt>Text::Quote</tt> for all the public to examine and scrutinise (you can thank me for a bunch of tests there, and even more that aren't<nobr> <wbr></nobr>;-). </p><p> <i>broquaint out</i></p> broquaint 2002-05-16T14:19:29+00:00 journal Fully scoped and ready to evaluate http://use.perl.org/~broquaint/journal/4498?from=rss My little pet project mentioned in my first entry is almost complete. It now supports all manner of funky things that no one ever uses (see. protoypes and attributes). Well that's not strictly true, but it's only in the more sophisticated perl projects that one will see the use of attributes (usually doing crazy stuff like encapsulation and pre/post handlers). It even manages to avoid variable name collision by wrapping __ around the sub names (but this is all filtered so unless someone has a sub called <tt>foo</tt> and a var called <tt>$__foo__</tt> all will be well). <p> There are however a couple caveats that I hope to overcome before it's possible release. The most grating one has to be not being able to have what looks like a function call in a string as the regex doesn't deal with such trickery yet. And because of the hack to avoid name collision you can't do symbolic dereferencing of the sub (which if you're doing, then probably won't notice in your delirious state of madness<nobr> <wbr></nobr>;-) </p><p> See isn't that better? Now you can sleep easily at night/day knowing that this wonderous module is being developed. You can now finally reach that inner calm you've always been searching for. Your whites will be whiter and your brights will be brighter! </p><p> <i>broquaint out</i></p> broquaint 2002-04-27T18:27:18+00:00 journal You learn something new about perl every day http://use.perl.org/~broquaint/journal/4434?from=rss Apparently <tt>qr//</tt> returns a blessed reference, and a brief inspection with <tt>blessed()</tt> reveals that it is of the type <tt>Regexp</tt><blockquote><div><p> <tt>shell&gt; perl -MScalar::Util=blessed -e 'print blessed(qr/^\s*$/), $/'<br>Regexp</tt></p></div> </blockquote><p>Huh. I imagine a good dig at the perl source might explain how and why this is (something to do with the<nobr> <wbr></nobr><tt>/o</tt> flag apparently). </p><p> I also had it driven into me (once more) as to why <a href="/~pdcawley">pdcawley</a> glows with <a href="http://www.penny-arcade.com/view.php3?date=2001-03-12">evil light</a> when he <a href="http://www.perlmonks.org/index.pl?node_id=161611">demonstrated</a> how to do tail call optimization in perl for his upcoming Scheme interpreter. If this is productive coding (which it most certainly is) then I fear to see what he could do when wields his power with the intent of doing "evil things"<nobr> <wbr></nobr>... </p><p> <i>broquaint out</i></p> broquaint 2002-04-24T19:59:40+00:00 journal I'm in two minds http://use.perl.org/~broquaint/journal/4405?from=rss I'm currently being simlutaneously frightened and delighted by demerphq's upcoming dumper module Data::BFDump (BF = Breadth First). It does essentially the same thing as Data::Dumper, but does it better. Firstly it can deal with merlyn's frightening Data::Dumper <a href="http://archive.develooper.com/perl5-porters@perl.org/msg60273.html">bug</a> <b>and</b> it deals with coderefs properly (i.e it dumps the code *within* the coderef)! If anyone knows of any data structures in perl that are fiendishly difficult to 'dump' please leave a comment. <p> Now I'm off home to enjoy the marvel that is all-day-breakfast-in-a-can while trying to read (and hopefully understand) <a href="http://www.amazon.co.uk/exec/obidos/ASIN/0333919394/ref=pd_ir_imp/026-6659753-4102859">Engineering Mathematics</a> (I'm shooting for one out of two<nobr> <wbr></nobr>;-). </p><p> <i>broquaint out</i></p> broquaint 2002-04-23T19:15:31+00:00 journal If only ... http://use.perl.org/~broquaint/journal/4380?from=rss ... the people of Earth listened to J-Pop more it would be a happier place. More specifically if more people listened to Morning Musume there'd be less <i>bad stuff</i> going on. How can anyone stay <tt>$bad_moods[rand @bad_moods]</tt> after listening to 10 or so young Japanese girls singing with gay abandon for a 'Love Revolution' in their native language? Not very long I tell you! <p> On a more perl-related note (well it at least relates to perl to some degree) I'm <b>finally</b> getting round to learning how to write tests for modules great and small. Much to my pleasant surprise it's not as tricky as I may have been lead to believe. <tt>&lt;remark tone="mildly ironic"&gt;</tt>Testing - I can hardly see what all the fuss is about.<tt>&lt;/remark&gt;</tt> <br> Many thanks go out to <a href="http://use.perl.org/~demerphq/">demerphq</a> for giving me the chance to get involved in the development process. So far so good<nobr> <wbr></nobr>... </p><p> <i>broquaint out</i></p> broquaint 2002-04-22T19:39:43+00:00 journal Lexically scoped subs'R'Us http://use.perl.org/~broquaint/journal/4338?from=rss After finally having realised that perl really does't do nested subs (lamented <a href="http://www.perlmonks.org/index.pl?node_id=158614">here</a>) I decided to take the next obvious step and write a module that gives the illusion of having nested subs<blockquote><div><p> <tt>#!/usr/bin/perl<br> <br>package MySub;<br> <br>use strict;<br> <br>use Regexp::Common;<br> <br>my $brackets_re&nbsp; &nbsp; &nbsp;= $RE{balanced}{-parens =&gt; '{}'};<br>my $paren_re&nbsp; &nbsp; &nbsp; &nbsp; = $RE{balanced}{-parens =&gt; '()'};<br> <br>my $sub_name_re&nbsp; &nbsp; &nbsp;= qr/[_a-zA-Z](?:\w+)?/;<br>my $sub_match_re&nbsp; &nbsp; = qr/my\s+sub\s+($sub_name_re)\s*($brackets_re)\s*;?/x;<br> <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # my sub foobar { "code" }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # my&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# 'my'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \s+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 1&gt; space<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # sub&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 'sub'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \s+&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 1&gt; space<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ($sub_name_re)&nbsp; &nbsp;# '$subname'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \s*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 0&gt; space<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ($brackets_re)&nbsp; &nbsp;# balanced {}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \s*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 0&gt; space<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #<nobr> <wbr></nobr>;?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# optional ';'<br> <br>use Filter::Simple;<br> <br>my @subs;<br># FILTER_ONLY code =&gt; sub {<br>FILTER {<br>&nbsp; &nbsp; my $code = $_;<br>&nbsp; &nbsp; study $code;<br> <br>&nbsp; &nbsp; while(my($subname, $subcode) = $code =~<nobr> <wbr></nobr>/$sub_match_re/s) {<br>&nbsp; &nbsp; &nbsp; &nbsp; push @subs, {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subname&nbsp; &nbsp; &nbsp;=&gt; $subname,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code&nbsp; &nbsp; &nbsp; &nbsp; =&gt; $subcode<br>&nbsp; &nbsp; &nbsp; &nbsp; };<br> <br>&nbsp; &nbsp; &nbsp; &nbsp; # 'my sub name {}' =&gt; 'my $name = sub {};'<br>&nbsp; &nbsp; &nbsp; &nbsp; $code =~ s/$sub_match_re/my \$$1 = sub $2;\n/s;<br> <br>&nbsp; &nbsp; &nbsp; &nbsp; # '&amp;name();' =&gt; '$name-&gt;();'<br>&nbsp; &nbsp; &nbsp; &nbsp; $code =~ s/<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# optional &amp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $subname&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# '$subname'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \s*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 0&gt; whitespace<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # group $1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $paren_re&nbsp; &nbsp; # balanced parens<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# optional group $1<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \s*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # 0&gt; whitespace<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<nobr> <wbr></nobr>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ';'<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<nobr> <wbr></nobr>/"\$$subname-&gt;" . ($1 || '()') . ';'/sex;<br>&nbsp; &nbsp; }<br> <br>&nbsp; &nbsp; $_ = $code;<br>};<br> <br>qw(package activated);</tt></p></div> </blockquote><p>Although the code is a little rough, it seems to DWIM so far. I haven't done any extensive testing (note to self - learn how to use test suites) but I havent found any problems as of yet. Once it's tidyed up a bit, I might even stick it on CPAN depending on the peoples' need for such an extension. </p><p> <i>broquaint out</i></p> broquaint 2002-04-20T17:56:48+00:00 journal