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

Re: undef loses it magicness when assigned to a variable?

Thread Previous | Thread Next
Stas Bekman
May 27, 2004 01:22
Re: undef loses it magicness when assigned to a variable?
Message ID:
Tony Cook wrote:
> On Thu, May 27, 2004 at 12:45:02AM -0700, Stas Bekman wrote:
>>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 
>>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.
>>From perldoc perlguts:
>        If you want to know if this variable (or any other SV) is
>        actually "defined", you can call:
>            SvOK(SV*)
> I've always used this, but the documentation in perlapi:
>        SvOK    Returns a boolean indicating whether the value is
>                an SV.
>                        bool    SvOK(SV* sv)
> doesn't really lead you to it.

Why pp_defined doesn't use it then?

But I guess both doc places could be fixed.

Moreover the original problem came from the autogenerated xs => C process, for 
converting a standard char * argument:

#line 451 "URI.c"
	char *	RETVAL;


	if (items < 2)
	    val = NULL;
	else {
	val = (char *)SvPV(ST(1), val_len);

not only it's messed up formatting-wise, it should probably have the SvOK in 
it and not issue:

   Use of uninitialized value in subroutine entry at

or may be it's a goodness that it does it. I suppose in order to allow undefs 
to be passed as arguments, one has to change the prototype of the xs function 
to accept SV* instead of char* and do the conversion manually.

If it's not a goodness I'd rather see it do:

	if (items < 2 || (items == 2 && !SvOK(ST(1))))
	    val = NULL;
	else {
	    val = (char *)SvPV(ST(1), val_len);

(in my example it's the second argument that's char *, but can be undef as 
well, therefore it's 2 items and ST(1)), but that may cause segfaults when an 
author weren't expecting users to pass undefined value as a char * variable.

Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker     mod_perl Guide --->

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About