Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

broquaint (2964)

  (email not shown publicly)

Journal of broquaint (2964)

Tuesday October 11, 2005
10:13 AM

Params::Named - As requested

So I wrapped up the previously mentioned named parameter binding code into a module called Params::Named which should now be available on the CPAN. It's now more robust and deals with arrays and hashes to boot. Go crazy.
Tuesday October 04, 2005
09:16 AM

Named arguments on Perl5. Sorta.

Here's a little bit of madness I cooked up for using named arguments natively in perl5:

use strict;
use warnings;

use PadWalker 'peek_sub';
use List::Util 'first';

## Map named arguments to variables of those names.
sub getargs {
  my %args = do { package DB; () = caller 1; @DB::args };
  my $vals = peek_sub \&{(caller 1)[3]};
  my %res  = map { $_ => $args{substr($_, 1)} }
             grep exists $args{substr($_, 1)}, keys %$vals;
  my %map  = map {
    my $orig_arg  = \$_;
    my($ref_name) = first { $orig_arg == $vals->{$_} } keys %$vals;
    $ref_name => $orig_arg;
  } @_;

  ${ $map{$_} } = $res{$_}
    for keys %res;

  return \%res;

## Basic test of getargs();
sub foo {
  getargs my($this, $that, $theother);
  print "this is: $this\nthat is: $that\nand the other is: $theother\n";

## Have these pairs mapped to variables of the same name.
foo this     => "an argument",
    that     => "used to refer to things",
    theother => "is something else altogether";

This will output:

this is: an argument
that is: used to refer to things
and the other is: is something else altogether

So, as you can see, given a list of pairs the variable names matching the keys will be automagically assigned to the corresponding values.

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.

Thursday May 26, 2005
01:47 PM

Because you asked for it

For the folks who wanted C style switching behaviour in Switch::Perlish (version 1.4):

use Switch::Perlish 'C';

my $food = shift;
switch $food, sub {
  case 'pizza';
  case 'burger';
  case 'fried-chicken';
  case 'tub-o-lard', sub {
    print "Your food is probably delicious\n";
  case 'celery';
  case 'rice-cracker';
  case 'plaster';
  case 'grass', sub {
    print "Your food is probably bland\n";
  default sub {
    print "Your food is without compare\n";

You can also match mutliple things in the normal style using an anonymous array. i.e

case [qw/ pizza burger fried-chicken tub-o-lard /], sub { ... }

Different keystrokes for different folks ...

Friday May 20, 2005
10:17 PM

A shiny new switch()

I've just finished up a new switch module which should be coming to a CPAN mirror near you soon (and can be found here presently). It goes by the name of Switch::Perlish and can be pointed to when foreigners ask where switch is in perl. Here's some obligatory example code as taken from the documentation:

use Switch::Perlish;

my $num = $ARGV[0];

switch $num, sub {
  case undef,
    sub { die "Usage: $0 NUM\n" };
  case [0 .. 10],
    sub { print "Your number was between 0 and 10" };
  case [11 .. 100],
    sub { print "Your number was between 11 and 100" };
  case [101 .. 1000],
    sub { print "Your number was between 101 and 1000" };
    sub { print "Your number was less than 0 or greater than 1000" };

broquaint out

Tuesday November 26, 2002
12:03 PM

Not your usual perl

I kid you not

$foo = "a string";
print "${";
foo} or is it?


a string or is it?

broquaint out

Thursday November 21, 2002
09:21 AM


Finally got Lexical scoping like a fox cleared up and hopefully it'll provide the necessary information to give people 'clue' about lexical scoping and the like.

Now it's just a matter of time before I write the big .... closure tutorial (cue. lightning and a distant rumble of thunder).

broquaint out

Wednesday November 13, 2002
01:10 PM

Stuff for learning

I wrote a nice little tutorial for Perl Monks 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 ...), then I shall sleep easier at night (or maybe that should be: I'll sleep at night ;).

And I did figure out how to use extract_quotelike() in the end, and may CPAN-ify it some time in the future as it is awful handy if your source munging.

broquaint out

Thursday May 16, 2002
09:19 AM

Maybe the guards are off duty

Hmmm, I appear to be a saint on Perl Monks. Interesting ...

In other news I posted Sub::Lexical 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 extract_quotelike from Mr Conway's Text::Quotelike or do some more funky regex (I don't think P::RD will be necessary here, I think ...).

Oh and I have my name on CPAN, in the albeit indirect way that demerphq finally put up Data::BFDump and Text::Quote for all the public to examine and scrutinise (you can thank me for a bunch of tests there, and even more that aren't ;-).

broquaint out

Saturday April 27, 2002
01:27 PM

Fully scoped and ready to evaluate

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 foo and a var called $__foo__ all will be well).

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 ;-)

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!

broquaint out

Wednesday April 24, 2002
02:59 PM

You learn something new about perl every day

Apparently qr// returns a blessed reference, and a brief inspection with blessed() reveals that it is of the type Regexp

shell> perl -MScalar::Util=blessed -e 'print blessed(qr/^\s*$/), $/'

Huh. I imagine a good dig at the perl source might explain how and why this is (something to do with the /o flag apparently).

I also had it driven into me (once more) as to why pdcawley glows with evil light when he demonstrated 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" ...

broquaint out