So for the last few day's I've been mucking around with mojolicious, and there is a lot I like about it, and well a lot I don't... primarily in that it allows for some rather spaghetti mess looking templating. (like PHP)

So I just wrote this silly little thing I call Slang!

It's pretty fast too... (I think)

#!/usr/bin/perl use Modern::Perl; use Slang qw ( mask ); use Bench; Bench::Start(); local $_; $_->{'mask'} = join ('', <DATA> ); $_->{'stash'} = { lang => "en", title => { en => "Bob's Bit's Emporium" }, content => { en => "<s>greetings.<s>lang</></>" }, greetings => { en => "Welcome!" } }; say mask($_->{'mask'},$_->{'stash'}); Bench::MileStone(); say mask($_->{'mask'},$_->{'stash'}); say mask($_->{'mask'},$_->{'stash'}); say mask($_->{'mask'},$_->{'stash'}); say mask($_->{'mask'},$_->{'stash'}); say mask($_->{'mask'},$_->{'stash'}); Bench::End(); Bench::Report(); __DATA__ <html lang="<s>lang</>"> <head><title><s>title.<s>lang</></></title></head> <body><s>content.<s>lang</></></body> </html>
package Slang; use Modern::Perl; use String::CRC32; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(mask); our $compiled_masks; my $debug = 0; sub mask { my $crc32 = crc32($_[0]); return $compiled_masks->{$crc32} if (defined $compiled_masks->{$cr +c32}); my $stash = $_[1]; my $mask = $_[0]; say "compiling from scratch" if $debug; while ($mask =~ m/<s>([^<]*?)<\/>/gs) { my $key = $1; if ($key =~ m@\.@s) { say "dot operator detected" if $debug; my $value; my @keys = split /\./,$key; my $first = 1; foreach my $new_key (@keys) { say $new_key if $debug; if ($first) { $value = $stash->{$new_key}; $first += 0; } else { $value = $value->{$new_key}; } } $mask =~ s/<s>$key<\/>/$value/gs; } else { say "edit : $key = $stash->{$key}" if $debug; $mask =~ s/<s>$key<\/>/$stash->{$key}/gs; } } $compiled_masks->{$crc32} = $mask; return $mask; }
package Bench; use Modern::Perl; use Time::HiRes qw ( gettimeofday tv_interval ); my ($start,$end,$elapsed,$fps); sub MileStone { End(); Report(); Start(); } sub Start { $start = [ gettimeofday ]; } sub End { $end = [ gettimeofday ]; } sub Report { $end = [ gettimeofday ]; $elapsed = tv_interval($start,$end); $fps = int(1 / $elapsed); say "$elapsed = $fps p/s"; } 1;

In reply to Meet Slang! by Logicus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.