develooper Front page | perl.macperl | Postings from February 2016

Re: [MacPerl] $PREMATCH Question

Thread Previous | Thread Next
From:
Jonathan Levi via macperl
Date:
February 3, 2016 23:49
Subject:
Re: [MacPerl] $PREMATCH Question
Message ID:
152a98a25cb-399d-fce8@webprd-m53.mail.aol.com
Ronald and Matthew, many thanks. jl
 

 

 

-----Original Message-----
From: Ronald J Kimball <rjk-perl-mac@tamias.net>
To: Jonathan Levi <drjlevi6@aol.com>
Cc: macperl <macperl@perl.org>
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


Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About