Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Can we make $& better?

by ikegami (Patriarch)
on Sep 06, 2007 at 18:20 UTC ( [id://637494]=note: print w/replies, xml ) Need Help??


in reply to Can we make $& better?

There is a difference:

$\ = "\n"; my $s = 'abcdefghij'; $s =~ /def/; print $&; # def print substr($s, $-[0], $+[0] - $-[0]); # def $s = '1234567890'; print $&; # def print substr($s, $-[0], $+[0] - $-[0]); # 456

Does it matter? Maybe. Switching could break existing code.

Replies are listed 'Best First'.
Re^2: Can we make $& better?
by Anno (Deacon) on Sep 06, 2007 at 19:40 UTC
    The difference you point out is even more blatant in this example:
    $\ = "\n"; my $s = 'abcdefghij'; $s =~ s/def/123/; print $&; # def print substr($s, $-[0], $+[0] - $-[0]); # 123
    Some code is surely using that.

    The problem with $& and friends isn't the location of the partial strings but the semantics that require copying the parts. Thus for every match, the entire string must be copied.

    Anecdote:

    A poster on clpmisc complained that his program "stopped working" after he introduced use English. He was matching a short pattern against some monster genome string (one or two GB) with a reasonable number of matches (some 10_000). Copying killed the cat, to coin a phrase.

    Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-04-19 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found