develooper Front page | perl.perl5.porters | Postings from May 2004

undef loses it magicness when assigned to a variable?

Thread Next
From:
Stas Bekman
Date:
May 27, 2004 00:45
Subject:
undef loses it magicness when assigned to a variable?
Message ID:
40B59C7E.6090902@stason.org
Part of one of XS API (let's say foo()) goes:

  if (items == 1 && ST(0) == &PL_sv_undef)) { ... }

if that API is called as:

   foo(undef);

then the if condition is true. However if I do:

   $x = undef;
   foo($x);

it's false, i.e. ST(0) is no longer == &PL_sv_undef. I did a quick check with
Devel::Peek:

% perl -le 'use Devel::Peek; Dump undef; $x = undef; Dump $x'
SV = NULL(0x0) at 0x804c6e4
   REFCNT = 2147483624
   FLAGS = (READONLY)
SV = NULL(0x0) at 0x8065690
   REFCNT = 1
   FLAGS = ()

which are different. And I've checked the C addresses, passing undef, gives 
the same address as &PL_sv_undef, passing $x = undef, gives a different 
address. It makes sense to me since in the latter case we have a real sv with 
its own address, in the previous case we have a shared sv which always has the 
same address.

So how do I check in C whether the passed variable (or a random sv) is undef 
or not if comparing to &PL_sv_undef doesn't work? am I missing some obvious 
RTFM section? I've read: 'perlguts:=head2 AVs, HVs and undefined values'
but it doesn't seem to be related. There were many other references to undef 
in the perl manpages, but I didn't find anything useful.

Thanks

-- 
__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About