On Wed, 6 Sep 2000, Perrin Harkins wrote:

> On Wed, 6 Sep 2000, Stas Bekman wrote:
> > Just a small correction: 
> > 
> > You can cause pages to become unshared in perl just by writing a variable,
> >                                                        ^^^^^^^
> > so it's almost certain to happen sooner or later.
> > 
> > Or for example calling pos() which modifies the variable internals:
> > http://perl.apache.org/guide/performance.html#Are_My_Variables_Shared_
> 
> If you read a variable in a way that causes it to be converted between a
> numerical value and a string and it hasn't happened before, that will
> change the internal structure and unshare the memory on one or more
> pages.  I'm no perlguts hacker, but I think this is correct.

You are right. I've looked into it with the help of all mighty
Devel::Peek. So what actually happens is this:

Consider this script:
  
  use Devel::Peek;
  my $numerical = 10;
  my $string    = "10";
  $|=1;
  
  dump_numerical();
  read_numerical_as_numerical();
  dump_numerical();
  read_numerical_as_string();
  dump_numerical();
  
  dump_string();
  read_string_as_numerical();
  dump_string();
  read_string_as_string();
  dump_string();
  
  sub read_numerical_as_numerical {
      print "\nReading numerical as numerical: ",
          int($numerical), "\n";
  }
  sub read_numerical_as_string {
      print "\nReading numerical as string: ",
          $numerical, "\n";
  }
  sub read_string_as_numerical {
      print "\nReading string as numerical: ",
          int($string), "\n";
  }
  sub read_string_as_string {
      print "\nReading string as string: ",
          $string, "\n";
  }
  sub dump_numerical {
      print "\nDumping a numerical variable\n";
      Dump($numerical);
  }
  sub dump_string {
      print "\nDumping a string variable\n";
      Dump($string);
  }

When running it:

  Dumping a numerical variable
  SV = IV(0x80e74c0) at 0x80e482c
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,IOK,pIOK)
    IV = 10
  
  Reading numerical as numerical: 10
  
  Dumping a numerical variable
  SV = PVNV(0x810f960) at 0x80e482c
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,IOK,NOK,pIOK,pNOK)
    IV = 10
    NV = 10
    PV = 0
  
  Reading numerical as string: 10
  
  Dumping a numerical variable
  SV = PVNV(0x810f960) at 0x80e482c
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
    IV = 10
    NV = 10
    PV = 0x80e78b0 "10"\0
    CUR = 2
    LEN = 28
  
  Dumping a string variable
  SV = PV(0x80cb87c) at 0x80e8190
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,POK,pPOK)
    PV = 0x810f518 "10"\0
    CUR = 2
    LEN = 3
  
  Reading string as numerical: 10
  
  Dumping a string variable
  SV = PVNV(0x80e78d0) at 0x80e8190
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK)
    IV = 0
    NV = 10
    PV = 0x810f518 "10"\0
    CUR = 2
    LEN = 3
  
  Reading string as string: 10
  
  Dumping a string variable
  SV = PVNV(0x80e78d0) at 0x80e8190
    REFCNT = 4
    FLAGS = (PADBUSY,PADMY,NOK,POK,pNOK,pPOK)
    IV = 0
    NV = 10
    PV = 0x810f518 "10"\0
    CUR = 2
    LEN = 3

So you can clearly see that if you want the data to be shared (unless
some other variable happen to change its value, and thus cause the
whole page to get dirty) and there is a chance that the same variable
will be acccessed both as a string and numerical value, you have to
access to this variable in both value as in the above example, before
the fork happens. 

_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide 
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://jazzvalley.com
http://singlesheaven.com http://perlmonth.com   perl.org   apache.org


Reply via email to