Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Meet Slang!

by Logicus (Initiate)
on Aug 19, 2011 at 15:36 UTC ( [id://921274]=perlmeditation: print w/replies, xml ) Need Help??

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;

Replies are listed 'Best First'.
Re: Meet Slang!
by moritz (Cardinal) on Aug 19, 2011 at 17:24 UTC
    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.

    You can teach Mojolicious to use a different template system than the default (and even if your own if you want to), and in fact there are a lot of plugins on CPAN that enable most well-known template system to work with Mojolicious.

    If that's your main gripe with Mojolicious, I'd just change template system instead of throwing it all away.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Meet Slang!
by jdrago999 (Pilgrim) on Aug 21, 2011 at 21:58 UTC

    From OP:

    __DATA__ <html lang="<s>lang</>"> <head><title><s>title.<s>lang</></></title></head> <body><s>content.<s>lang</></></body> </html>

    String Interpolation is not Hard

    What is it with you and these funky </></></> things? Do you find it that difficult to use built-ins like string interpolation, or, when that's too clunky/weird, why not use the @{[ ... ]} notation like this?: print "Foo @{[ $obj->foo->bar('baz') ]}"

    XML is Already Invented

    Reinventing the XML wheel as something with empty closing tags doesn't actually solve anything. Perl (and the web in general) has plenty of templating frameworks and markup styles already. A quick browse through http://search.cpan.org/ will reveal this to you.

    How is this an Improvement?

    What problems does Slang help solve? How does it solve those problems better/differently than the dozens of existing tools out there already? Don't know? Perhaps learn about what is already out there - and then - if it doesn't quite scratch your itch, write something that *does*.

    Why Does Everyone Pick on You?

    You're not getting the love here anytime you post the result of your last 24-hour's coding marathon because, frankly, we see a lot of code. A _LOT_ of code. We *read* a lot of code almost everyday. And we're simply not impressed with what you're posting.

    I can't imagine handing the template (above) to a designer/UI-team-member who is just going to screw it up in DreamWeaver. So...they have to count the number of <s> tags and then correctly count the number of </> tags?

    Obvious Ignorance

    Have you ever heard of JSP? ColdFusion? ASP.Net? XSLT? Have you ever *used* any of them to try and get something done - something more complex than simple if/else branching or while/foreach loops?

    (I doubt you have, but I'll continue anyway)

    Nothing is gained - for us - by using them. We already have tools which are far superior to them, built-in to the core language itself. What you post here day after day is like a constant barrage of crap - something sent to 2011 from a period in the late 1990's which we'd all like to forget. A period which you simply weren't around for, don't have the background on, and obviously don't care to read-up on.

    What you Missed Out on these Last 10 Years

    There was a time in when Mr. Clinton was in office when everyone wrote their own cookie-handling library. They wrote their own web-page templating library. They wrote their own upload-handling methods. They wrote their own http request parsing library. They wrote their own SQL data layer. Everybody did it different - in their own way - and nothing was compatible. SURE you could write Perl but you probably couldn't quickly understand anything anyone else wrote.

    When CPAN finally started picking up some steam, all that was finally able to change. It was easy for Perl programmers to share code. It became easy to install other people's libraries. People began to collaborate on modules and we ended up with some really, really great ways of doing things. We gained Consensus. CGI::Application and Template::Toolkit did pretty much what you needed, and if you wanted good performance then mod_perl would be used as well. Everything was good...for a while...

    Then the rest of the world started using Microsoft ASP, ADO.Net, JSP, ColdFusion, ASP.Net, PHP, Django, Ruby on Rails, etc. Some Perl hackers also used those other tools. Upon their return to Perl, we got Mojolicious, Dancer, Plack, Catalyst, DBIx::Class and others. These were not written by inexperienced newbs who had finally figured out how to run DOS commands on their mom's PC and wanted a web page to impress their friends. These projects are the result of the combined efforts of many experienced, seasoned programmers who are familiar with (and may even have experience using) the other popular web technologies listed. Plack was written in Perl because Ruby's "Rack" is a really great idea - and it *works great*. Catalyst opened many Perl users' eyes to MVC (Model-View-Controller) and provided a good, *working* implementation of it for Perl. DBIx::Class came from a disagreement about the way plugins/mixins were implemented in Class::DBI (among other things) and ended up becoming something much, much larger and more complete - not because Mr. Trout had never *used* Class::DBI or any other ORM (Object-Relational-Mapp(ing|er)) but because he *had used* other ORM's and knew some things could be done *better*.

    Which Brings me to my Point

    You hack this stuff out and post it on PerlMonks, astonished at your own cleverness. Then you get insulted when others point out that what you posted just isn't the greatest thing since sliced bread. Sit down, shut up, and have some humble pie.

    You just had a dozen of the best Perl hackers on the planet look at your code and give you their honest opinion. For free, of their own volition. If you can't take the heat, stay out of the kitchen and simply use what is served up for free on http://search.cpan.org/ until you have a better idea of what's possible, what's already been done and what has not yet been solved.

      Sit down, shut up, and have some humble pie.

      That's really not helpful.

      It's obvious that Logicus has generated some friction recently, but for all of the bad faith ascribed to him, he's posting code (and it's getting better) and, for better or worse, he's experimenting.

      I can't defend abusiveness pro or contra his project or his character, and I'm growing more tired of attacks against him and his project.

      I might not ever use aXML or Slang, and I've pointed out a security hole (now fixed) and some further research opportunities for writing a more efficient implementation, but even if I never use the projects, there's no reason for continued abuse against him.

      Critique the implementation. Suggest other options. By all means, encourage Logicus to write better code—but please let the rest of it go. He's posting code. He's demonstrating improvements and research. This is behavior to encourage.

            Sit down, shut up, and have some humble pie.

        That's really not helpful.

        I can't defend abusiveness pro or contra his project or his character, and I'm growing more tired of attacks against him and his project.

        Critique the implementation. Suggest other options. By all means, encourage Logicus to write better code—but please let the rest of it go. He's posting code. He's demonstrating improvements and research. This is behavior to encourage.

        And the other behaviour, is behaviour to be discouraged.

        If you want help, all it costs you is an honest and civil tongue.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Meet Slang!
by JavaFan (Canon) on Aug 22, 2011 at 00:52 UTC
    use 5.010; use strict; use warnings; sub foo { say "This is foo <@_>" } sub bar { say "This is bar <@_>" } sub call { my $sub = shift; goto &$sub; } $_ = "foo 123 foo bar bar 456"; while (/(\w+) (\w+)/g) { call $1, $2; } __END__ This is foo <123> This is foo <bar> This is bar <456>
    The above avoids no strict 'refs', but it doesn't actually buy you anything.

    Adapting it to your own templating system (which neither seems useful, working or fast to me) is left as an exercise to the reader.

    P.S. I graduated from university long before you flunked college. (Not that anyone should care).

    A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://921274]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-24 05:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found