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

If the $' in $'var is not at all related to $', how does Perl keep them straight?
#!/usr/bin/perl -wl use strict; $main::var = "a rose by "; #$main::var $main'var = $'var."another name"; #$main::var again use vars q($var); $var =~ s/(\b\w)/uc($1)/ge; #$main::var $var =~ /N/; print $'var; #print $main::var print 'G'.$'." With $'var"; #games with $' use English; print "s$' and s$POSTMATCH";


Replies are listed 'Best First'.
(tye)Re: $'name
by tye (Sage) on Mar 01, 2001 at 01:43 UTC

    See line 5811 of toke.c (for Perl5.6.0):

    else if (*s == '\'' && isIDFIRST_lazy_if(s+1,UTF)) {
    So, in effect, /\$'[a-zA-Z_]/ is treated differently than /\$'[^a-zA-Z_]/ [note the caret (^)].

            - tye (but my friends call me "Tye")
The $'name G'ame.
by tadman (Prior) on Mar 01, 2001 at 01:18 UTC
    I would suppose that the lexer in the perl parser is intelligent enough to recognize the pattern for variables of that sort. It could loosely be described as:      \$'([a-zA-Z_][a-zA-Z0-9_]*)? Which could define a valid variable name.

    Otherwise, how would you distinguish between $x and $xy? Or, $_ and $_x for that matter.