Re: [MacPerl] $PREMATCH Question
[email protected] (Jonathan Levi via macperl) Wed, 3 Feb 2016 18:49:09 -0500
| Newsgroups | perl.macperl |
|---|---|
| Message-ID | <[email protected]> |
------=_Part_82373_1828379329.1454543349193 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Ronald and Matthew, many thanks. jl -----Original Message----- From: Ronald J Kimball <[email protected]> To: Jonathan Levi <[email protected]> Cc: macperl <[email protected]> Sent: Wed, Feb 3, 2016 12:55 pm Subject: Re: [MacPerl] $PREMATCH Question On Tue, Feb 02, 2016 at 08:59:44PM -0500, Jonathan Levi via macperl wrote: > Can someone explain why the following: > > echo alphabet | perl -e '$_=<STDIN>; if (m/pha/){print $PREMATCH, "\n"}' > > yields "" instead of "al", as I expected? > > Jonathan If you want the long name, you need to use the English module, as mentioned near the top of perlvar <http://perldoc.perl.org/perlvar.html>. For example: echo alphabet | perl -MEnglish -e '$_=<STDIN>; if (m/pha/){print $PREMATCH, "\n"}' Using /p and ${^PREMATCH}, as Matthew suggested, will avoid performance penalties in longer scripts, however. Ronald ------=_Part_82373_1828379329.1454543349193 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit <font color='black' size='2' face='Arial, Helvetica, sans-serif'>R<font size="2">onald and Matthew, many thanks. jl</font> <div> <br> </div> <div> <br> </div> <div> <br> </div> <div style="font-family:helvetica,arial;font-size:10pt;color:black">-----Original Message-----<br> From: Ronald J Kimball <[email protected]><br> To: Jonathan Levi <[email protected]><br> Cc: macperl <[email protected]><br> Sent: Wed, Feb 3, 2016 12:55 pm<br> Subject: Re: [MacPerl] $PREMATCH Question<br> <br> On Tue, Feb 02, 2016 at 08:59:44PM -0500, Jonathan Levi via macperl wrote:<br> > Can someone explain why the following:<br> > <br> > echo alphabet | perl -e '$_=<STDIN>; if (m/pha/){print $PREMATCH, "\n"}'<br> > <br> > yields "" instead of "al", as I expected?<br> > <br> > Jonathan<br> <br> If you want the long name, you need to use the English module, as<br> mentioned near the top of perlvar <<a href="http://perldoc.perl.org/perlvar.html" target="_blank">http://perldoc.perl.org/perlvar.html</a>>.<br> For example:<br> <br> echo alphabet | perl -MEnglish -e '$_=<STDIN>; if (m/pha/){print $PREMATCH, "\n"}'<br> <br> Using /p and ${^PREMATCH}, as Matthew suggested, will avoid performance<br> penalties in longer scripts, however.<br> <br> Ronald<br> </div> </font> ------=_Part_82373_1828379329.1454543349193--