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 ]

Aristotle (5147)

Aristotle
  pagaltzis@gmx.de
http://plasmasturm.org/

Blah blah blah blah blah [technorati.com]

Journal of Aristotle (5147)

Friday December 04, 2009
05:35 AM

Applying ideas from new languages in your old standbys

[ #39975 ]

I just wrote the following Perl 5 code. Consider the bolded bits, and think about what the code would have looked like if I had tried to write it in any equally DRY way using map.

sub new {
    my $class = shift;
    my $self = bless { @_ }, $class;
    my $builder = $self->builder;
    my $content = $self->content;

    my ( @gather, @take );

    for my $r ( XML::Builder::Util::is_raw_array( $content ) ? @$content : $content ) {
        @take = $r;


        if ( not Scalar::Util::blessed $r ) {
            @take = $builder->render( @_ ) if XML::Builder::Util::is_raw_array $r;
            next;
        }

        if ( not $r->isa( $builder->fragment_class ) ) {
            @take = $builder->stringify( $r );
            next;
        }

        next if $builder == $r->builder;

        Carp::croak( 'Cannot merge XML::Builder fragments built with different namespace maps' )
            if $r->depends_ns_scope;

        @take = $r->flatten;

        my ( $self_enc, $r_enc ) = map { lc $_->encoding } $builder, $r->builder;
        next
            if $self_enc eq $r_enc
            # be more permissive: ASCII is one-way compatible with UTF-8 and Latin-1
            or 'us-ascii' eq $r_enc and grep { $_ eq $self_enc } 'utf-8', 'iso-8859-1';

        Carp::croak(
            'Cannot merge XML::Builder fragments with incompatible encodings'
            . " (have $self_enc, fragment has $r_enc)"
        );
    }
    continue {
        push @gather, @take;
    }


    $self->{'content'} = \@gather;

    return $self;
}

I don’t know whether this way of writing the code would have occurred to me if I hadn’t seen the construct in Perl 6. I suspect not.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.