Perldoc Search: "$'" perl-5.20.1

search official POD

16 PODs, 46 LINEs found.
408 :         % simple_grep '^(\w\w\w\w|\w\w\w|\w\w|\w)\g1$' /usr/dict/words
483 :     Even if there are no groupings in a regexp, it is still possible to find out what exactly matched in a string. If you use them, Perl will set $` to the part of the string before the match, will set $& to the part of the string that matched, and will set $' to the part of the string after the match. An example:
486 :         $x =~ /cat/;  # $` = 'the ', $& = 'cat', $' = ' caught the mouse'
487 :         $x =~ /the/;  # $` = '', $& = 'the', $' = ' cat caught the mouse'
491 :     If your code is to run on Perl versions earlier than 5.20, it is worthwhile to note that using $` and $' slows down regexp matching quite a bit, while $& slows it down to a lesser extent, because if they are used in one regexp in a program, they are generated for *all* regexps in the program. So if raw performance is a goal of your application, they should be avoided. If you need to extract the corresponding substrings, use "@-" and "@+" instead:
495 :         $' is the same as substr( $x, $+[0] )
497 :     As of Perl 5.10, the "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}" variables may be used. These are only set if the "/p" modifier is present. Consequently they do not penalize the rest of the program. In Perl 5.20, "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}" are available whether the "/p" has been used or not (the modifier is ignored), and $`, $' and $& do not cause any speed difference.
547 :         % simple_grep '^(\w+)\g1$' /usr/dict/words   # isn't this easier?
1304 :         % simple_grep '^(\w+)(\w+)?(?(2)\g2\g1|\g1)$' /usr/dict/words
404 :     Traditionally in Perl, any use of any of the three variables $`, $& or $' (or their "use English" equivalents) anywhere in the code, caused all subsequent successful pattern matches to make a copy of the matched string, in case the code might subsequently access one of those variables. This imposed a considerable performance penalty across the whole program, so generally the use of these variables has been discouraged.
410 :         print $`, $&, $'; # bad: perfomance hit
481 :     $'      The string following whatever was matched by the last successful pattern match (not counting any matches hidden within a BLOCK or "eval()" enclosed by the current BLOCK). Example:
485 :                 print "$`:$&:$'\n";         # prints abc:def:ghi
494 :             This is similar to $' ($POSTMATCH) except that it does not incur the performance penalty associated with that variable.
559 :             $' is the same as "substr($var, $+[0])"
447 :     Several special variables also refer back to portions of the previous match. $+ returns whatever the last bracket match matched. $& returns the entire matched string. (At one point $0 did also, but now it returns the name of the program.) $` returns everything before the matched string. $' returns everything after the matched string. And $^N contains whatever was matched by the most-recently closed group (submatch). $^N can be used in extended patterns (see below), for example to assign a submatch to a variable.
453 :     WARNING: If your code is to run on Perl 5.16 or earlier, beware that once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program.
455 :     Perl uses the same mechanism to produce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression "(?: ... )" instead.) But if you never use $&, $` or $', then patterns *without* capturing parentheses will not be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price.
457 :     Perl 5.16 introduced a slightly more efficient mechanism that notes separately whether each of $`, $&, and $' have been seen, and thus may only need to copy part of the string. Perl 5.20 introduced a much more efficient copy-on-write mechanism which eliminates any slowdown.
459 :     As another workaround for this problem, Perl 5.10.0 introduced "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", which are equivalent to $`, $& and $', except that they are only guaranteed to be defined after a successful match that was executed with the "/p" (preserve) modifier. The use of these variables incurs no global performance penalty, unlike their punctuation char equivalents, however at the trade-off that you have to tell perl when you want to use them. As of Perl 5.20, these three variables are equivalent to $`, $& and $', and "/p" is ignored.
12 :     Scalar values are always named with '$', even when referring to a scalar that is part of an array or a hash. The '$' symbol works semantically like the English word "the" in that it indicates a single value is expected.
33 :     Because variable references always start with '$', '@', or '%', the "reserved" words aren't in fact reserved with respect to variable names. They *are* reserved with respect to labels and filehandles, however, which don't have an initial special character. You can't have a filehandle named "log", for instance. Hint: you could say "open(LOG,'logfile')" rather than "open(log,'logfile')". Using uppercase filehandles also improves readability and protects you from conflict with future reserved words. Case *is* significant--"FOO", "Foo", and "foo" are all different names. Names that start with a letter or underscore may also contain digits and underscores.
53 :     There are two package separators in Perl: A double colon ("::") and a single quote ("'"). Normal identifiers can start or end with a double colon, and can contain several parts delimited by double colons. Single quotes have similar rules, but with the exception that they are not legal at the end of an identifier: That is, $'foo and $foo'bar are legal, but $foo'bar' is not.
491 :     If you're confused about why you use an '@' there on a hash slice instead of a '%', think of it like this. The type of bracket (square or curly) governs whether it's an array or a hash being looked at. On the other hand, the leading symbol ('$' or '@') on the array or hash indicates whether you are getting back a singular value (a scalar) or a plural one (a list).
181 :                 $count{$i_LINES}{only} += matches($i_word, $word, '^[^a-zA-Z0-9]+$');
182 :                 $count{$i_LINES}{cons} += matches($i_word, $word, '^[(?i:bcdfghjklmnpqrstvwxyz)]+$');
183 :                 $count{$i_LINES}{vows} += matches($i_word, $word, '^[(?i:aeiou)]+$');
184 :                 $count{$i_LINES}{caps} += matches($i_word, $word, '^[(A-Z)]+$');
261 :       Compiling REx '[bc]d(ef*g)+h[ij]k$'
289 :       Omitting $` $& $' support.
596 :       Matching '[bc]d(ef*g)+h[ij]k$' against 'abcdefg__gh__'
502 :             Compiling REx `^pe(a)*rl$'
518 :             Omitting $` $& $' support.
522 :             Freeing REx: `^pe(a)*rl$'
243 :        $'    Everything after to matched string
249 :     Note to those still using Perl 5.18 or earlier: The use of $`, $& or $' will slow down all regex use within your program. Consult perlvar for "@-" to see equivalent expressions that won't cause slow down. See also Devel::SawAmpersand. Starting with Perl 5.10, you can also use the equivalent variables "${^PREMATCH}", "${^MATCH}" and "${^POSTMATCH}", but for them to be defined, you have to specify the "/p" (preserve) modifier on your regular expression. In Perl 5.20, the use of $`, $& and $' makes no speed difference.
202 :     Called to get/set the value of $`, $', $& and their named equivalents, ${^PREMATCH}, ${^POSTMATCH} and ${^MATCH}, as well as the numbered capture groups ($1, $2, ...).
210 :         $'            RX_BUFF_IDX_POSTMATCH
1341 :             $shell_info = qx'ps $$';            # that's the new shell's $$
1819 :         print 'p N$' ^ " E<H\n";            # prints "Perl\n";
283 :         A call to "exec" will cause Perl to exit, and to invoke the command given as an argument to "exec" via "lib$do_command". If the argument begins with '@' or '$' (other than as part of a filespec), then it is executed as a DCL command. Otherwise, the first token on the command line is treated as the filespec of an image to run, and an attempt is made to invoke it (using .Exe and the process defaults to expand the filespec) and pass the rest of "exec"'s argument to it as parameters. If the token has no file type, and matches a file with null type, then an attempt is made to determine whether the file is an executable image which should be invoked using "MCR" or a text file which should be passed to DCL as a command procedure.
460 :         The matched-pattern variables, $&, $` (pre-match), $' (post-match), and $+ (last match) also are tainted.
97 :         M6]9*<IQCO*XFT"0[PL%%'Y+IG?WN^ZYN-$'J.[.JE$,20/?K=_[>
184 :                       sigil => '$',
569 :         print 'p N$' ^ " E<H\n";                      # prints "Perl\n";
1162 :                       $str,     $%,    '$' . int($num)
<< Back to Perldoc Search