Stories
Slash Boxes
Comments
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)

broquaint
  (email not shown publicly)

Journal of broquaint (2964)

Tuesday April 23, 2002
02:15 PM

I'm in two minds

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 bug and 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.

Now I'm off home to enjoy the marvel that is all-day-breakfast-in-a-can while trying to read (and hopefully understand) Engineering Mathematics (I'm shooting for one out of two ;-).

broquaint out

Monday April 22, 2002
02:39 PM

If only ...

... 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 bad stuff going on. How can anyone stay $bad_moods[rand @bad_moods] 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!

On a more perl-related note (well it at least relates to perl to some degree) I'm finally 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. <remark tone="mildly ironic">Testing - I can hardly see what all the fuss is about.</remark>
Many thanks go out to demerphq for giving me the chance to get involved in the development process. So far so good ...

broquaint out

Saturday April 20, 2002
12:56 PM

Lexically scoped subs'R'Us

After finally having realised that perl really does't do nested subs (lamented here) I decided to take the next obvious step and write a module that gives the illusion of having nested subs

#!/usr/bin/perl

package MySub;

use strict;

use Regexp::Common;

my $brackets_re     = $RE{balanced}{-parens => '{}'};
my $paren_re        = $RE{balanced}{-parens => '()'};

my $sub_name_re     = qr/[_a-zA-Z](?:\w+)?/;
my $sub_match_re    = qr/my\s+sub\s+($sub_name_re)\s*($brackets_re)\s*;?/x;

                      # my sub foobar { "code" }
                      # my               # 'my'
                      # \s+              # 1> space
                      # sub              # 'sub'
                      # \s+              # 1> space
                      # ($sub_name_re)   # '$subname'
                      # \s*              # 0> space
                      # ($brackets_re)   # balanced {}
                      # \s*              # 0> space
                      # ;?               # optional ';'

use Filter::Simple;

my @subs;
# FILTER_ONLY code => sub {
FILTER {
    my $code = $_;
    study $code;

    while(my($subname, $subcode) = $code =~ /$sub_match_re/s) {
        push @subs, {
            subname     => $subname,
            code        => $subcode
        };

        # 'my sub name {}' => 'my $name = sub {};'
        $code =~ s/$sub_match_re/my \$$1 = sub $2;\n/s;

        # '&name();' => '$name->();'
        $code =~ s/
                    &?               # optional &
                    $subname         # '$subname'
                    \s*              # 0> whitespace
                    (                # group $1
                        $paren_re    # balanced parens
                    )?               # optional group $1
                    \s*              # 0> whitespace
                    ;                # ';'
                  /"\$$subname->" . ($1 || '()') . ';'/sex;
    }

    $_ = $code;
};

qw(package activated);

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.

broquaint out