Strange Perl line : Return the result of a function to a function

A

AlexHWGUY

I never done a Perl code before but I done some C, PHP, JAVA, etc...
Peel looked very similar before I failed to understand those lines :

substr($y, 5, 2) = substr($y, 4, 2);
substr($y, 4, 1) = ".";

How that possible ? It is replacing the 2 char at pos 5 with 2 char at
pos 4 ?

Thanks to help me put my eyes back in their hole.
 
J

Josef Moellers

AlexHWGUY said:
I never done a Perl code before but I done some C, PHP, JAVA, etc...
Peel looked very similar before I failed to understand those lines :

substr($y, 5, 2) = substr($y, 4, 2);
substr($y, 4, 1) = ".";

How that possible ? It is replacing the 2 char at pos 5 with 2 char at
pos 4 ?

Thanks to help me put my eyes back in their hole.

It does indeed look strange, but (from "perldoc -f substr"):

"You can use the substr() function as an lvalue, in which case EXPR must
itself be an lvalue."

So it works as designed.
 
T

Tad McClellan

Josef Moellers said:
It does indeed look strange, but (from "perldoc -f substr"):

"You can use the substr() function as an lvalue, in which case EXPR must
itself be an lvalue."

So it works as designed.


And if you use the 4-argument form of substr(), then your eyeballs will
stay in their sockets. :)
 
P

Peter J. Holzer

I never done a Perl code before but I done some C, PHP, JAVA, etc...
Peel looked very similar before I failed to understand those lines :

substr($y, 5, 2) = substr($y, 4, 2);
substr($y, 4, 1) = ".";

How that possible ? It is replacing the 2 char at pos 5 with 2 char at
pos 4 ?

Yes.

That form is now by many considered deprecated, but I wonder whether the
alternative

substr($y, 5, 2, substr($y, 4, 2));
substr($y, 4, 1, ".");

is really more readable. At least the assignment makes it clear what is
replaced (you obviously found the right answer without checking the
manual), while for the 4-argument form you have to look up the manual to
find out that there is a replacement at all.

hp

PS: For replacing parts of a string I prefer the s/// operator, even if
it is somewhat slower.
 
I

Ingo Menger

Peter said:
Yes.

That form is now by many considered deprecated, but I wonder whether the
alternative

substr($y, 5, 2, substr($y, 4, 2));
substr($y, 4, 1, ".");

is really more readable.

Full ACK. After all, people are used to read and write things like

$a[2] = 42;

without ever noticing that the construct on the left hand side means
something totally different when it is used on the right hand side.
 
J

John W. Krahn

AlexHWGUY said:
I never done a Perl code before but I done some C, PHP, JAVA, etc...
Peel looked very similar before I failed to understand those lines :

substr($y, 5, 2) = substr($y, 4, 2);
substr($y, 4, 1) = ".";

How that possible ? It is replacing the 2 char at pos 5 with 2 char at
pos 4 ?

Yes.

That could also be written as:

substr $y, 4, 3, '.' . substr $y, 4, 2;


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top