Kyle R . Burton on Tue, 9 Apr 2002 09:45:52 -0400 |
> mjd-perl-pm@plover.com writes: > [...] > > > > So for example the string "BLARF" probably takes about 30 bytes. (24 > > overhead plus 6 for "BLARF\0") > > Perl uses null-terminated strings internally? How odd. How, then, > is a string with embedded NUL characters represented? I beleive the null terminator is maintained by Perl so the string can easily be passed to C funcitons that expect it. Perl also tracks the length of the scalar seperatly from the char* data (see SvCUR and SvCUR_set for more about that). In that example, SvCUR would be 5 even though there is 6 bytes of storage used by "BLARF\0". You can toy with the difference between C and Perl easily enough with Inline: use strict; use warnings; my $a = "BLARF"; c_testFunction($a); print "Perl: $a\n"; c_testFunction2($a); use Inline C => <<'EOC'; void c_testFunction( SV* var ) { char* p; STRLEN len; p = SvPV(var,len); printf("C1: string: %d:%s\n",len,p); SvCUR_set(var,3); } void c_testFunction2( SV* var ) { char* p; STRLEN len; p = SvPV(var,len); printf("C2: string: %d:%s\n",len,p); } EOC You should get ouptut like: C1: string: 5:BLARF Perl: BLA C2: string: 3:BLARF The remainder of BLARF is still there even though Perl sees it as only being 3 characters. To be more correct, we should have put a null at *(p+3) as to not confuse subsequent C functions. Or we could have used the safer format of %s for printf: printf("C2: string: %d:%.*%s\n",len,len,p); Kyle -- ------------------------------------------------------------------------------ Wisdom and Compassion are inseparable. -- Christmas Humphreys mortis@voicenet.com http://www.voicenet.com/~mortis ------------------------------------------------------------------------------ **Majordomo list services provided by PANIX <URL:http://www.panix.com>** **To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**
|
|