Perldoc Search: "$`" perl-5.20.1

search official POD

10 PODs, 28 LINEs found.
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
421 :         $`; $&; "abcdefgh" =~ /d/
461 :     $`      The string preceding whatever was matched by the last successful pattern match, not counting any matches hidden within a BLOCK or "eval" enclosed by the current BLOCK.
470 :             This is similar to $` ($PREMATCH) except that it does not incur the performance penalty associated with that variable.
485 :                 print "$`:$&:$'\n";         # prints abc:def:ghi
557 :             $` is the same as "substr($var, 0, $-[0])"
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'
489 :     In the second match, $` equals '' because the regexp matched at the first character position in the string and stopped; it never saw the second 'the'.
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:
493 :         $` is the same as substr( $x, 0, $-[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.
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.
241 :        $`    Everything prior 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, ...).
209 :         $`            RX_BUFF_IDX_PREMATCH
460 :         The matched-pattern variables, $&, $` (pre-match), $' (post-match), and $+ (last match) also are tainted.
289 :       Omitting $` $& $' support.
1286 :                 1 while s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
518 :             Omitting $` $& $' support.
67 :               RSTART    length($`)
<< Back to Perldoc Search