Mailing List Archive

Re: Modifying the pos() of a shared variable...
Cool! Amazing to see that it's OK to modify the pos without unsharing the string, also that read-only lexicals are properly shared.

Stas, thanks for this brilliant example.

-c

>> >> Does modifying the pos() of a shared variable make the variable unshared?

---------
Here is the example of how to test your considerations:

Shared.pm (preloaded at the server startup)
---------
package Shared;
use Apache::Peek;
my $lexical = "Chris";
sub match{ $lexical =~ /\w/g; }
sub print_pos{ print "pos: ", pos($lexical), "\n"; }
sub dump { Dump($lexical); }
1;
--------

test.pl
-------
use Shared;
print "Content-type: text/plain\n\n";
print "PID: $$\n";
Shared::match();
Shared::print_pos();
Shared::dump();
--------------

Set :
MaxClients 2
for easier tracking.

after several execution, where pos() returns different values for the 2
processes we see:

PID: 27040
pos: 1
SV = PVMG(0x853db20) at 0x8250e8c
REFCNT = 3
FLAGS = (PADBUSY,PADMY,SMG,POK,pPOK)
IV = 0
NV = 0
PV = 0x8271af0 "Chris"\0
CUR = 5
LEN = 6
MAGIC = 0x853dd80
MG_VIRTUAL = &vtbl_mglob
MG_TYPE = 'g'
MG_LEN = 1

PID: 27041
pos: 2
SV = PVMG(0x853db20) at 0x8250e8c
REFCNT = 3
FLAGS = (PADBUSY,PADMY,SMG,POK,pPOK)
IV = 0
NV = 0
PV = 0x8271af0 "Chris"\0
CUR = 5
LEN = 6
MAGIC = 0x853dd80
MG_VIRTUAL = &vtbl_mglob
MG_TYPE = 'g'
MG_LEN = 2

All the addresses are the same, therefore the variable is still shared.
The only difference is in SV.MAGIC.MG_LEN, which is not shared. I don't
know how properly to call the state of the variable, but at large it's
non-shared, while the biggest part of it is shared.
---------


------------------------------------------------------------------------
870 Market Street #1270 (415) 394-9818
San Francisco, CA 94102 (413) 473-0853 fax
------------------------------------------------------------------------