Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

No questions. Just a "thank you for named captures" !!

by perl514 (Pilgrim)
on Feb 06, 2013 at 12:29 UTC ( #1017403=perlmeditation: print w/ replies, xml ) Need Help??

Hi,

 

Not sure where this should go, so kindly move to the appropriate section.

Whoever came up with the Named Captures in Perl, Thank you. You are a Godsend. It's made things a hell a lot simpler and more clear. I just finished writing a small script and while I went from trying the basic capture to the named capture, I could not help but thank the people who came up with this.

Another note of thanks to the authors of Learning Perl 6th Edition for explaining it so nicely!! Guess one of the authors would be surely involved in the implementation of the Named Capture.

Now, time to go back to the script and add some more improvements...Perl is addictive. Very addictive...

Perlpetually Indebted To PerlMonks

use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
http://dwimperl.com/windows.html is a boon for Windows.

Comment on No questions. Just a "thank you for named captures" !!
Select or Download Code
Re: No questions. Just a "thank you for named captures" !!
by sedusedan (Monk) on Feb 06, 2013 at 16:50 UTC

    Two things to note:

    1) It's a shame that perl did not come up with named captures. It was already available at least in .NET (and Ruby, I believe?) before perl 5.10 was released.

    2) %+ is rather slow.

    That said, named capture *is* a godsend. This feature belong to the "how did I manage to get by without this" category.

      Python also had named captures before Perl, but why does it matter who had it first? Or do you mean It's a shame that Perl did not support them sooner than it did?
        Perhaps shame is too strong a word. How about 'it would be cool if Perl came up with named capture instead of other languages, remembering that Perl was/is kind of the state-of-the-art for regex stuffs.'

        Strangely PCRE (the Perl Compatible Regular Expression engine) also added support for named captures several years before Perl.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Hi,

       

      I am not entirely sure about %+ being slow. I tried running the script with numbered captures like $1 and $2 and then with named captures and quite frankly didnt notice much difference. May be using named captures makes some script/program run slightly slower, but I am not the kinds who would really worry about fractional speed.

      Speaking of speed, I have tried other scripting languages and as of now, perl is atleast twice as fast. I tried parsing an XML File in Perl and Python and Ruby, and Perl seems to do it much faster. Not dissing Python/Ruby here, just stating what I saw.

      As for "borrowing" stuff from other languages, I think it's about time that other languages give back something to Perl. :D

      Perlpetually Indebted To PerlMonks

      use Learning::Perl; use Beginning::Perl::Ovid; print "Awesome Books";
      http://dwimperl.com/windows.html is a boon for Windows.

        I think it's about time that other languages give back something to Perl. :D

        Smiley noted; but just so we're all clear, Perl has already borrowed tons of features from other languages.
        In some sense, it was originally invented as a synthesis of certain other languages.
        This is even stated in the original Perl man page, which is now entitled simply "perl":

        Perl combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges of csh, Pascal, and even BASIC-PLUS.)
        Also see this clpm discussion from 1999, ancestry of perl features.

        I reckon we are the only monastery ever to have a dungeon stuffed with 16,000 zombies.

        $ perl -MBench -e'$s = "a b"; /(\w+) (\w+)/; bench sub {$a=$1; $b=$2}, + -1' 3666664 calls (3339959/s), 1.098s (0.0003ms/call) $ perl -MBench -e'$s = "a b"; /(?<a>\w+) (?<a>\w+)/; bench sub {$a=$+{ +a}; $b=$+{b}}, -1' 636364 calls (560973/s), 1.134s (0.0018ms/call) $ perl -MBench -e'$s = "a b"; /(?<a>\w+) (?<a>\w+)/; %h=(a=>$+{a}, b=> +$+{b}); bench sub {$a=$h{a}; $b=$h{b}}, -1' 3333331 calls (3102372/s), 1.074s (0.0003ms/call)

        Accessing %+ elements is slower due to some tie magic. In general, yeah, you don't need to worry about it. But I managed to make Org::Parser and Text::sprintfn around twice as fast by avoiding named capture or copying %+ first to a temporary hash instead of accessing invididual %+ elements repeatedly.

          It's a shame that perl did not come up with named captures.

      If you want to get technical about it, most modern languages (including Perl) have a debt to C. In fact most compilers, translators, interpolators and such are written in C, C++ or a combination of both.


      Peter L. Berghold -- Unix Professional
      Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1017403]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2014-08-28 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (259 votes), past polls