What fun!

#!/usr/bin/perl use warnings; use strict; $\ = "\n"; $_ = "123123123"; my %x = ( 9 => "abc", foo => "abc" ); eval '/^$x{9}$/'; print '1. "$x" requires explicit package name' if $@; my $x = "[123]"; print "2. matches" if /^$x{9}$/; print "3. matches" if /^$x{5,12}$/; my $nine = 9; print "4. no match" unless /^$x{foo}$/; print "5. no match" unless /^$x{"9"}$/; print "6. no match" unless /^$x{0+9}$/; print "7. no match" unless /^$x{$nine}$/; print "8. matches" if /^${x}{$nine}$/; print "9. matches" if /^${x}{9}$/; print "10. no match" unless /^${x}{0+9}$/;

Good Day,
    Dean

Replies are listed 'Best First'.
Re: Of scalars, hashes, quantifiers, and regexen
by ikegami (Patriarch) on Sep 24, 2008 at 22:08 UTC

    Complicated!

    patterninterpolatedrepeat expression
    $x{digits[,[digits]]}$x{digits[,[digits]]}
    $x{y}$x{y}once
    ${x}{digits[,[digits]]}$x{digits[,[digits]]}
    ${x}{y}$xonce (The rest will be treated independently)
    ${x{y}}{digits[,[digits]]}$x{y}{digits[,[digits]]}
    ${x{y}}{z}$x{y}once (The rest will be treated independently)

    The rules seem to be

    • {...} is always treated as a repeat expression if it resembles a repeat expression.
    • {...} is never treated as a repeat expression if it doesn't resembles a repeat expression.
    • ${...} is always treated as a complete variable interpolation.

    But it's completely different for [...].

    patterninterpolatedrest
    $x[y]$x[y]
    ${x}[y]$x[y]

    The rules seem to be

    • [...] is always treated as an array index if an array index is acceptable.
    • [...] is never treated as an array index if an array index isn't acceptable.
    • ${...} is always treated as a complete variable interpolation.