Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: can't use string refs while "strict refs"

by tobyink (Canon)
on Jul 30, 2012 at 07:16 UTC ( [id://984373]=note: print w/replies, xml ) Need Help??


in reply to can't use string refs while "strict refs"

Perhaps something like this:

use strict; use warnings; my $x = "Mmm...donut, thought Homer"; if (my @matches = $x =~ /^(Mmm|Yech)\.\.\.(donut|peas)/) { unshift @matches => undef; foreach my $expr (1..$#-) { print "Match $expr: '$matches[$expr]' at position ($-[$expr],$ ++[$expr])\n"; } }

Note the trick is to capture the return value of the =~ into an array (which I've called @matches above).

Trick number two is to unshift a dummy value into $matches[0]. This is because arrays are indexed from 0 and regex matches are numbered from 1. Unshifting the dummy value makes @matches line up better with $- and $+.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://984373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-23 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found