Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Meet Slang!

by Logicus
on Aug 19, 2011 at 15:36 UTC ( #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;

Comment on Meet Slang!
Select or Download Code
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.

      The way things are looking the next version of my main program will be mojo based, and it would be cool if I could offload all the templating needs to it but I'm not so sure it can handle the schema. I dunno I need to study it more.
        In the dark of no textual response, I can only assume from the -4 score of the comment, that basing on Mojo is a bad idea. Thanks for the "constructive feedback".
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.

      String interpolation is not hard...

      Those </> things exist as an optimisation, they remove the need for backtracking and give a huge performance boost within the method I'm currently using.



      XML is already invented

      Sure it's been around for a long time, I just personally believe it's properties have not yet been fully explored or utilised, and attempts to do so in such things as Microsoft XaML and horror of horrors Coldfusion, have so far been lame, constrictive, unimaginative and overly verbose.

      I believe the approache itself has merit and that a much better result can be attained. I bet Microsoft had a team of dozens and spent millions on R&D for XaML, and it still sucks... I've spent £0.00p on aXML and I'm working alone. My solution isn't perfect, it's not fast enough yet but I'm just one guy and I have limits.

      As for looking on CPAN about templating engines I can name a number of them without stopping to look. I just don't like the idea that you have to build an interpreted templating mini-language system on top of the interpreted fully fledged language which is Perl itself. I'm all for separation of concerns, but I'm also for Keep it Simple.



      How is this an Improvement?

      Why does it have to be an improvement, why can't it just be an odd little tangent? I'm sensing a real culture of competitiveness here which I am neither interested in nor currently capable of being part of.

      Said the Tortoise to the Hare, your solution is very fast... but I will arrive at that destination at the end.

      Besides, improvements are relative concepts, and improvements in one direction can often be detriments to other directions. If you compare aXML with XaML it's nearest cousin I've been able to find, then you see aXML is vastly superior. That's comparing apples and apples. If you want to know how much better of an Orange aXML is compared to say catalyst, then I'm sorry but it's not a fair comparison!

      Slang there for instance is great for applying small markup masks to hash value stashes, it's very fast and the new version which I haven't released yet allows you store and reference the result with what I'm calling a glue argument.

      Perhaps given your huge and vastly superior knowledge of CPAN, you can direct me to an equally simple and powerful little tool that does the same thing, preferably one whose source code has no dependencies and is less than 5KB in size.



      Why Does Everyone Pick on You?

      There you just admitted it! QUOTE : we're simply not impressed with what you're posting END-QUOTE ... in other words, we are high and mighty on a high horse of superiority and unless you do things the way we do them, then your sub-human and worthy of scorn derision, begone.. untermensch, troll, I'm just going to anonymously click you down without even so much as talking to you as your so far beneath _ME_...

      That my friend is called BIGOTRY. One of the things I like most about Perl and the world of Perl is the core concept of TIMTOWTDI, a concept which unfortunately is touted far more frequently than it is followed.



      Obvious Ignorance

      There you go with your quick to judge, we know best and are perfect narcissistic attitude.

      I have programmed systems in the following languages :

      • x86 ASM
      • C
      • Pascal
      • Prolog
      • Lisp
      • Ada
      • Fortran
      • Cobol
      • Perl
      • PHP
      • ASP
      • Java

      Of that list only 3 of them have been any fun. Specifically, C ASM and PERL.

      I've been around a lot longer than you realise, and I suspect I'm quite a bit older than you realise. But then making assumptions and attacks based on them is something you high and mighty can get away with whilst simultaneously condemning in others. That my dear colleague, is called Hypocrisy.



      What you missed out on in the last 10 years

      President clinton, and who was it, Monika Lewinsky? At that specific point in time I was flunking college by doing far too many all night caffeine powered sessions competing with an student friend of mine to see who could make the fastest texture mapper / gourad shader. (I won).

      Whilst my head was in the clouds of fixed point maths, negotiating with multidimensional arrays and counting clock ticks for ASM commands in the inner loops, my COBOL assignment was forgotten about (and with it went my career).

      Still it was a blast, and personally I blame Denthor of Asphyxia for setting me off!

      Mmmmm polygons line by line... write a word not a byte, double the speed!

      Coming back on topic, why would I waste my time by studying things that are obsolete? This was the same gripe I had with COBOL, in 1996 it was already obsolete, so why on earth would I want to study it?

      Most of the computing world thinks Perl is obsolete, and won't study it. Now personally I think there is room for someone to write some sort of rosetta stone book for all the various approaches which exist in Perl. The mantra is supposed to be easy things easy, hard things possible... but lately with all the additions that have been made and the complete cluster-frack of ways to do it, easy things are not easy anymore. How many times have you had to fight CPAN when it won't install without force? How many times have you typed install "foo", only to have to wait half an hour while all the dependancies are installed... this is not simple.

      It might seem simple when you have all the requisite knowledge and know how to fix any errors that occur during installation, but for the rest of us mere mortals, learning Perl and the "kosher" methods prescribed by the gods, is an uphill experience with constant battles and frustrations and digging for nuggets of information. Against that backdrop its no wonder Perl's marketshare continues to decline.

      Now don't get me wrong, I don't blame Perl at all for this. There was a time when Perl was entirely a procedural language, simple, elegant and the best way to get results quick. Overtime all the fads like OOP have been bolted on layer after layer, and it's only recently that things have started to get better with things like Moose.

      Do you really think that if Perl as it stands is so perfect, that Larry is mistaken in wanting to break nearly everything and remove all the layers of "cruft" that have grown around it? Do you think he's doing that for the sake of his health? Wake UP and smell the coffee. Perl is being slowly choked to death by creeping vines of complexity, and I'm sorry but as far as I'm concerned your lovely Perl5 based frameworks and solutions are on the verge of becoming obsolete, as Perl 6 looms closer, and a complete rethink of EVERYTHING is not only warranted at this point but absolutely vital to the future of the language.

      Just read that bit about inexperienced noobs, f*** you and the high horse your riding.



      Which brings me to my point

      QUOTE: astonished at your own cleverness END-QUOTE.

      Well that bring's me to my own point.

      Your projecting your own high and mighty superiority all over what I'm doing without having a clue as to my situation, background, reasons or motives. Your making assumption after assumption, and your showing off the fact that outside of your mighty knowledge of Perl and the "kosher" methods, you are infact extremely narrow-minded, bigoted and hypocritical.

      Take your humble pie, and insert it in your meatspace waste fuel ejection pipe. Good day.

        I suspect I'm quite a bit older than you realise.

        I think you'd better write "I'm quite a bit older than I behave." But then I guess some things are hard to learn. Social skills are a good example. Humility is another.

        You'd feel better if you returned to your tiny pond in which you can be the big fish as you used to.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

      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.

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

      quote : (Not that anyone should care) end-quote

      But they do... very much so, infact having to accumulate all I know by myself on my own volition instead of having it handed too me, and thus having notable gaps in my ability to describe certain problems and systems, is more than enough cause for the majority of people to inform me, forcefully in some cases, that I ought to "know my place" and that I should not have ideas "above my station", or that I need a portion of "humble pie" before their glory.

      Well f*** that. I know what I want to do, why I want to do it, and what I have in mind. I know there are plenty of resources written by very intelligent people, that I could use, and if I was on a deadline I probably would... but I'm not on any sort of deadline, I don't answer to a boss who want's results, yesterday or preferably sooner, and I don't care if I have to hack away in the dark for another 10 years to do things they way I want to do them. Neither do I care if anyone else ever wants to use what I have done or finds it in the slightest bit interesting or useful. Not bothered.

      I come here for information, not grandiosity. I couldn't give a rat's arse where I sit on the totem pole of Perl programming prowess, nor what those who feel they have the right to belittle, chastise, or otherwise denegrate people just because they don't fit in with the status quo, and refuse to be coerced into so doing.

      I'm a renegade and a heretic to a language and way of doing things whose very own creator acknowledges is far from ideal, and has spent some 11 years working hard to fix.

      So my ideas don't fit in the mainstream, so what? Are you a communist? Must we all wear the exact same uniform? My way or the highway? TIO1WTDI really, your way....(according to er... you)

        quote : (Not that anyone should care) end-quote

        But they do... very much so, infact having to accumulate all I know by myself on my own volition instead of having it handed too me, and thus having notable gaps in my ability to describe certain problems and systems, is more than enough cause for the majority of people to inform me, forcefully in some cases, that I ought to "know my place" and that I should not have ideas "above my station", or that I need a portion of "humble pie" before their glory.

        Well f*** that. I know what I want to do, why I want to do it, and what I have in mind. I know there are plenty of resources written by very intelligent people, that I could use, and if I was on a deadline I probably would... but I'm not on any sort of deadline, I don't answer to a boss who want's results, yesterday or preferably sooner, and I don't care if I have to hack away in the dark for another 10 years to do things they way I want to do them. Neither do I care if anyone else ever wants to use what I have done or finds it in the slightest bit interesting or useful. Not bothered.

        I come here for information, not grandiosity. I couldn't give a rat's arse where I sit on the totem pole of Perl programming prowess, nor what those who feel they have the right to belittle, chastise, or otherwise denegrate people just because they don't fit in with the status quo, and refuse to be coerced into so doing.

        I'm a renegade and a heretic to a language and way of doing things whose very own creator acknowledges is far from ideal, and has spent some 11 years working hard to fix.

        So my ideas don't fit in the mainstream, so what? Are you a communist? Must we all wear the exact same uniform? My way or the highway? TIO1WTDI really, your way....(according to er... you)

        Are you posting drunk again?

Re: Meet Slang!
by sundialsvc4 (Monsignor) on Aug 22, 2011 at 12:44 UTC

    Humble Monks, I just spent half of my “dog votes” today voting-down this entire thread, except for the original post, which I did not down-vote.   Maybe it was the bit about “stuff it up your solid-waste ejection pipe,” or maybe it was the responses that came in like manner thereafter.

    What does any of this have to do with Perl?

    Answer:   it doesn’t.

    This is a civilized bar.   You can feel any way that you like about anyone that you like, but when it starts to get personal, it also gets off topic, and that is what /msg is for.   The Delete Key Is Your Friend.   You are speaking to literally thousands of people.   Every one of them is your esteemed colleague.   Kindly treat them that way, even if you vehemently disagree with them.   At the very least, kindly do not expose the rest of us to this kind of garbage off-topic irrelevance.

Log In?
Username:
Password:

What's my password?
Create A New User
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? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (10)
As of 2014-09-18 09:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (109 votes), past polls