eweaverp has asked for the wisdom of the Perl Monks concerning the following question:

Hola Monks...

I was looking at the qr// operator the other day, and came across this, from perlop:

The result may be used as a subpattern in a match: $re = qr/$pattern/; $string =~ /foo${re}bar/; # can be interpolated in other patter +ns $string =~ $re; # or used standalone $string =~ /$re/; # or this way

And I came to the conclusion that I had no clue what was going on in the /foo${re}bar/ snip. Why the {}'s in this case? Not repetition, I assume. And why is '$' outside of them? Why is 're' not treated as a fixed string? If someone could spell out what exactly this means, I would appreciate it.

Confusedly...

~evan

Replies are listed 'Best First'.
Re: /foo${re}bar/
by fglock (Vicar) on Jul 27, 2003 at 04:00 UTC

    {}'s are separators. They are used such that Perl knows where the variable name finishes:

    $x = "abc"; $xghi = "def"; print "${x}ghi\n"; # abcghi print "$xghi\n"; # def

      Why not /foo{$re}bar/? Just that {} already is used for repetition, and a number would get interpolated?

      Which usage came first in Perl-time?

        er, no. ${foo} has been the standard way to disambiguate variables for a long time now and was independent of regex. Mostly used for strings, aside from this as in: print "${Foo}bar" and similar, also used for references, so called "soft" refs in the form of ${"foo"} and hard refs in the form of $x=\$foo; ${$x} Also works with the various other sigils, $%@.
Re: /foo${re}bar/
by hossman (Prior) on Jul 27, 2003 at 05:38 UTC
    from perldata...
           As in some shells, you can enclose the variable name in
           braces to disambiguate it from following alphanumerics
           (and underscores).  You must also do this when interpolat-
           ing a variable into a string to separate the variable name
           from a following double-colon or an apostrophe, since
           these would be otherwise treated as a package separator:
    
               $who = "Larry";
               print PASSWD "${who}::0:0:Superuser:/:/bin/perl\n";
               print "We use ${who}speak when ${who}'s here.\n";
    
           Without the braces, Perl would have looked for a $whos-
           peak, a $who::0, and a $who's variable.  The last two
           would be the $0 and the $s variables in the (presumably)
           non-existent package "who".
    
           In fact, an identifier within such curlies is forced to be
           a string, as is any simple identifier within a hash sub-
           script.  Neither need quoting.  Our earlier example,
           $days{'Feb'} can be written as $days{Feb} and the quotes
           will be assumed automatically.  But anything more compli-
           cated in the subscript will be interpreted as an expres-
           sion.
    
Re: /foo${re}bar/
by revdiablo (Prior) on Jul 27, 2003 at 04:00 UTC

    The {} are used to disambiguate the variable name from the surrounding text. If it were simply /foo$rebar/, perl would look for a variable named $rebar instead of $re.