[pm-h] Perl Code

John Ellyson jellyson at gmail.com
Thu Jun 18 22:19:19 PDT 2020


Mike,

The behavior that you are describing is covered in Perl regular expressions
tutorial at
https://metacpan.org/pod/distribution/perl/pod/perlretut.pod#Extracting-matches
.

I personally leverage this a lot in the code that I write - especially to
capture all occurrences of something into an array.  In fact, I have gotten
into the habit of trying to write my code in such a way to try to avoid
using the regex variables like $1 and $2.

And Uri has a very good point about needing to include some way to verify
that the regex actually matched and returned values.  Not having that has
caused me headaches with trying to figure out what went wrong with my code.

John Ellyson

On Wed, Jun 17, 2020 at 6:04 PM Mike Flannigan <mikeflan at att.net> wrote:

>
> Recently I found this bit of code that I wrote
> long ago.  I am a little surprised this works
> ($1 and $2 get transferred to the scalars).
> I don't write that way anymore, but I am
> leaving it that way in this program.
>
>
> use strict;
> use warnings;
>
> my $test = 'abcdefgh';
> my ($latm, $longm) = $test =~ /(ab).*(fg)/i;
> print "$latm, $longm\n";
>
>
> Prints this:
> ab, fg
>
>
>
> Mike
> _______________________________________________
> Houston mailing list
> Houston at pm.org
> https://mail.pm.org/mailman/listinfo/houston
> Website: http://houston.pm.org/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/houston/attachments/20200619/ac3b9dc5/attachment.html>


More information about the Houston mailing list