Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: [substr] anomaly or mine?

by sauoq (Abbot)
on Aug 19, 2002 at 23:53 UTC ( [id://191346]=note: print w/replies, xml ) Need Help??


in reply to [substr] anomaly or mine?

The behaviour makes sense to me but I don't think the docs are as clear as they might be. They do say:
An alternative to using substr() as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice().

The key phrase in the above being "one operation." When used as an lvalue it does it in two operations, the assignment and then the substr and you get the result of the substr().

Update: I was becoming more and more convinced that my guess was the correct one. Then I discovered this:

$ perl -le '$,="|";$s="foobar"; print substr($s,-2,2)="z",substr($s,-2 +,2)' z|bz

given that the substring is different in the two cases, it isn't just doing the substr() after the replacement. I'd say that is a bug and I think I have to agree with tye. It seems that the "right" behavior would be to return the string assigned as in any assignment.

I wonder how many obfus changing that will ruin...

-sauoq
"My two cents aren't worth a dime.";

Log In?
Username:
Password:

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

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

    No recent polls found