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

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.


Comment on Re: No questions. Just a "thank you for named captures" !!
Re^2: No questions. Just a "thank you for named captures" !!
by Arunbear (Parson) on Feb 06, 2013 at 17:26 UTC
    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
Re^2: No questions. Just a "thank you for named captures" !!
by perl514 (Pilgrim) on Feb 06, 2013 at 17:44 UTC

    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.

Re^2: No questions. Just a "thank you for named captures" !!
by blue_cowdawg (Monsignor) on Feb 06, 2013 at 18:15 UTC
        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