Assigning to Substr

Assigning to Substr

am 06.09.2011 14:04:29 von Emeka

--001517448060347eac04ac44a1eb
Content-Type: text/plain; charset=ISO-8859-1

Hello All,

Could someone explain what Perl does behind here?

Assign to Substring..
substr($string, 0 , 5) = 'Greetings';


Regards,
Emeka
--
*Satajanus Nig. Ltd


*

--001517448060347eac04ac44a1eb--

Re: Assigning to Substr

am 06.09.2011 14:19:53 von Shawn H Corey

On 11-09-06 08:04 AM, Emeka wrote:
> Hello All,
>
> Could someone explain what Perl does behind here?
>
> Assign to Substring..
> substr($string, 0 , 5) = 'Greetings';
>
>
> Regards,
> Emeka

It replaces the first 5 characters in $string with 'Greetings'.

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'Hello, world';
print "$string\n";

substr($string, 0 , 5) = 'Greetings';
print "$string\n";

__END__



Can also be written as:

#!/usr/bin/env perl

use strict;
use warnings;

my $string = 'Hello, world';
print "$string\n";

substr($string, 0 , 5, 'Greetings' );
print "$string\n";

__END__



--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Assigning to Substr

am 06.09.2011 14:32:32 von Emeka

--001517402bfa870c5d04ac4505dc
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Sep 6, 2011 at 1:19 PM, Shawn H Corey wrote:

> On 11-09-06 08:04 AM, Emeka wrote:
>
>> Hello All,
>>
>> Could someone explain what Perl does behind here?
>>
>> Assign to Substring..
>> substr($string, 0 , 5) = 'Greetings';
>>
>>
>> Regards,
>> Emeka
>>
>
> It replaces the first 5 characters in $string with 'Greetings'.
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my $string = 'Hello, world';
> print "$string\n";
>
>
> substr($string, 0 , 5) = 'Greetings';
> print "$string\n"
>


I asked what Perl does behind.. Is it an alias or what? I want internal
details....or something technical.

Emeka

> __END__
>
>
>
> Can also be written as:
>
> #!/usr/bin/env perl
>
> use strict;
> use warnings;
>
> my $string = 'Hello, world';
> print "$string\n";
>
> substr($string, 0 , 5, 'Greetings' );
> print "$string\n";
>
> __END__
>
>
>
> --
> Just my 0.00000002 million dollars worth,
> Shawn
>
> Confusion is the first step of understanding.
>
> Programming is as much about organization and communication
> as it is about coding.
>
> The secret to great software: Fail early & often.
>
> Eliminate software piracy: use only FLOSS.
>
> "Make something worthwhile." -- Dear Hunter
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>


--
*Satajanus Nig. Ltd


*

--001517402bfa870c5d04ac4505dc--

Re: Assigning to Substr

am 06.09.2011 14:39:08 von Shawn H Corey

On 11-09-06 08:32 AM, Emeka wrote:
> I asked what Perl does behind.. Is it an alias or what? I want internal
> details....or something technical.

See `perldoc -f substr` and search for /lvalue/.


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Assigning to Substr

am 07.09.2011 21:00:43 von Rob Dixon

On 06/09/2011 13:04, Emeka wrote:
>
> Could someone explain what Perl does behind here?
>
> Assign to Substring..
> substr($string, 0 , 5) = 'Greetings';

What do you want to know Emeka?

There is no copy of the 'old' string, and substr behaves as documented.

I have the C code of substr in front of me, but there is little else to
say.

Rob

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Assigning to Substr

am 07.09.2011 21:08:20 von Emeka

--001517402bfadd964904ac5eaaf2
Content-Type: text/plain; charset=ISO-8859-1

Rob,

I want to check out the C code. Kindly mail it to my box.
Have a great day!

Emeka

On Wed, Sep 7, 2011 at 8:00 PM, Rob Dixon wrote:

> On 06/09/2011 13:04, Emeka wrote:
>
>>
>> Could someone explain what Perl does behind here?
>>
>> Assign to Substring..
>> substr($string, 0 , 5) = 'Greetings';
>>
>
> What do you want to know Emeka?
>
> There is no copy of the 'old' string, and substr behaves as documented.
>
> I have the C code of substr in front of me, but there is little else to
> say.
>
> Rob
>



--
*Satajanus Nig. Ltd


*

--001517402bfadd964904ac5eaaf2--

Re: Assigning to Substr

am 07.09.2011 21:49:44 von Emeka

--001517447a2ef41d1a04ac5f3e5c
Content-Type: text/plain; charset=ISO-8859-1

Rob,

Which C file should check out?

Emeka

On Wed, Sep 7, 2011 at 8:08 PM, Emeka wrote:

> Rob,
>
> I want to check out the C code. Kindly mail it to my box.
> Have a great day!
>
> Emeka
>
>
> On Wed, Sep 7, 2011 at 8:00 PM, Rob Dixon wrote:
>
>> On 06/09/2011 13:04, Emeka wrote:
>>
>>>
>>> Could someone explain what Perl does behind here?
>>>
>>> Assign to Substring..
>>> substr($string, 0 , 5) = 'Greetings';
>>>
>>
>> What do you want to know Emeka?
>>
>> There is no copy of the 'old' string, and substr behaves as documented.
>>
>> I have the C code of substr in front of me, but there is little else to
>> say.
>>
>> Rob
>>
>
>
>
> --
> *Satajanus Nig. Ltd
>
>
> *
>



--
*Satajanus Nig. Ltd


*

--001517447a2ef41d1a04ac5f3e5c--

Re: Assigning to Substr

am 08.09.2011 08:15:55 von Shlomi Fish

Hi Emeka,

On Wed, 7 Sep 2011 20:49:44 +0100
Emeka wrote:

> Rob,
>=20
> Which C file should check out?
>=20
> Emeka
>=20

Please look at the function definition starting from «PP(pp_substr)=C2=
=BB in pp.c in
the Perl 5 distribution:

https://github.com/mirrors/perl/blob/blead/pp.c

It seems pretty complicated, and I did not try to understand it yet.

Regards,

Shlomi Fish

--=20
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
My Favourite FOSS - http://www.shlomifish.org/open-source/favourite/

Do not meddle in the affairs of Dragons, for you are crunchy and taste good
with ketchup. â€=94 Unknown sourc=
e.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Assigning to Substr

am 08.09.2011 13:56:05 von Rob Dixon

On 07/09/2011 20:49, Emeka wrote:
> On Wed, Sep 7, 2011 at 8:00 PM, Rob Dixon wrote:
>> On Wed, Sep 7, 2011 at 8:08 PM, Emeka wrote:
>>> On 06/09/2011 13:04, Emeka wrote:
>>>
>>>>
>>>> Could someone explain what Perl does behind here?
>>>>
>>>> Assign to Substring..
>>>> substr($string, 0 , 5) = 'Greetings';
>>>>
>>>
>>> What do you want to know Emeka?
>>>
>>> There is no copy of the 'old' string, and substr behaves as documented.
>>>
>>> I have the C code of substr in front of me, but there is little else to
>>> say.
>>>
>>
>> I want to check out the C code. Kindly mail it to my box.
>> Have a great day!
>>
>
> Which C file should check out?

Hi Emeka.

It would help a lot if you would start putting your responses at the end
of the post you are replying to. That way extended threads are made much
more readable.

As Shlomi said, the code for the substr op is "PP(pp_substr)" pp.c. Most
of the code there is for validating the parameters and deciding which of
several different things should be done. It doesn't actually do any
substring extraction, it just sets up the return value TARG and pushes
it onto the stack.

The code that deals specifically with lvalues is the block starting

else if (lvalue) { /* it's an lvalue! */

at line 3253 in the version I'm looking at, 5.12.4. In particular you
will see that it assigns

LvTARGOFF(TARG) = pos;
LvTARGLEN(TARG) = len;

to indicate the position and length of the substring that TARG specifies.

The assignment to the substring is done by function
'Perl_magic_setsubstr' in mg.c. Which ultimately calls 'sv_insert',
#defined in sv.h as a call to Perl_sv_insert_flags' which is in sv.c.

I hope that helps. The internals of Perl are complex and not for the
feint-hearted.

Rob


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/