[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl5-porters
Subject:    Re: undef $var vs $var = undef [was Re: [perl #67838] ...]
From:       Eric Brine <ikegami () adaelis ! com>
Date:       2009-11-30 23:00:25
Message-ID: f86994700911301500j78975532g123160eb9c0cc2f1 () mail ! gmail ! com
[Download RAW message or body]


On Mon, Nov 30, 2009 at 3:58 PM, H.Merijn Brand <h.m.brand@xs4all.nl> wrote:

> I'm not pushing anymore, but /I/ don't see *any* use here. If I want to
> preserve the allocated memory, I use "", not undef.
>

The empty string is not the same thing as undef. You can't assign the empty
string to variable you want to undefine.


> And jdb, I'm not propagating people to undef all their values
> themselves. Out-of-scope is way nicer


Variables going out of scope are not freed (if there are no external
reference to them), and neither are their buffers.

$ perl -MDevel::Peek -e'sub f { my $x; Dump $x; $x=$_[0]; Dump $x; } f
"abcdef"; f "xyz";'
SV = NULL(0x0) at 0x966c900
  REFCNT = 1
  FLAGS = (PADMY)
SV = PV(0x96506d0) at 0x966c900
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x9667ed0 "abcdef"\0
  CUR = 6
  LEN = 8
SV = PV(0x96506d0) at 0x966c900
  REFCNT = 1
  FLAGS = (PADMY)
  PV = 0x9667ed0 "abcdef"\0
  CUR = 6
  LEN = 8
SV = PV(0x96506d0) at 0x966c900
  REFCNT = 1
  FLAGS = (PADMY,POK,pPOK)
  PV = 0x9667ed0 "xyz"\0
  CUR = 3
  LEN = 8

but I have seen places where using undef $sth would force

a DESTROY that otherwise would have been too late.
>

$sth=undef; and even $sth=123; would have worked just as well. Aside from
the fact that the reference in $sth probably has no string buffer to free in
the first place, if wouldn't affect anything's refcount if it did.

- ELB


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic