develooper Front page | perl.perl5.porters | Postings from August 2013

Re: Perl 5.18 and Regexp::Grammars

Thread Previous | Thread Next
From:
Damian Conway
Date:
August 13, 2013 12:20
Subject:
Re: Perl 5.18 and Regexp::Grammars
Message ID:
CAATtAp61kKm8ME8zWBf7mYKsRm6eqbVMxxGn27jyr3XTRvej1Q@mail.gmail.com
Well, if I'm being offered a fully "pre-parsed" API for 'source'
constant overloadings, all I can say is: yes, please!

Though, I'd suggest that the constant components should be passed in the
same order they appear in the source. That is:

    overload::constant source = sub {
        my ($keyword, $leftdelim1, $content1, $rightdelim1,
                      $leftdelim2, $content2, $rightdelim2, $flags) = @_;

        ...
    }

That way, if you need to, you can reconstruct the original source with
just "@_". For example:

    overload::constant source = sub {
        my ($keyword) = @_;

        if ($keyword =~ /qq?/ && $SPECIAL_STRING_MAGIC_REQUESTED) {
            return apply_special_string_magic_to(@_);
        }
        else { # No transformation required so return original source...
            return "@_";
        }
    }


Specifically:

    Constant       ( $kw,   $l1,  $c1,     $r1,  $l2,  $c2,   $r2,
$fg  )  =  @_;

    'foo'            ''     '\''  'foo'    '\''  ''    ''     ''    ''
    "foo"            ''     '"'   'foo'    '"'   ''    ''     ''    ''
    q"foo"           'q'    '"'   'foo'    '"'   ''    ''     ''    ''
    q/foo/           'q'    '/'   'foo'    '/'   ''    ''     ''    ''
    qq{foo}          'qq'   '{'   'foo'    '}'   ''    ''     ''    ''
    qr/foo/xi        'qr'   '/'   'foo'    '/'   ''    ''     ''    'xi'
    m/foo/xi         'm'    '/'   'foo'    '/'   ''    ''     ''    'xi'
    s/foo/bar/gx     's'    '/'   'foo'    '/'   ''    'bar'  '/'   'gx'
    s{foo}{bar}gx    's'    '{'   'foo'    '}'   '{'   'bar'  '}'   'gx'
    s(foo)/bar/gx    's'    '{'   'foo'    '}'   '/'   'bar'  '/'   'gx'
    tr/a-z/A-J/rd    'tr'   '/'   'a-z'    '/'   ''    'A-J'  '/'   'rd'
    y{a-z}{A-Z}      'y'    '{'   'a-z'    '}'   '{'   'A-Z'  '}'   ''
    123              ''     ''    '123'    ''    ''    ''     ''    ''
    1.23             ''     ''    '1.23'   ''    ''    ''     ''    ''
    1.2e3            ''     ''    '1.2e3'  ''    ''    ''     ''    ''
    077              '0'    ''    '77'     ''    ''    ''     ''    ''
    0x7A             '0x'   ''    '7A'     ''    ''    ''     ''    ''
    0b101            '0b'   ''    '101'    ''    ''    ''     ''    ''


In particular, note that in an s/// or tr/// the second left delimiter
is reported as an empty string, so that "@_" reconstructs the original
source of the constant correctly.

Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About