Coding Guidelines/SWIG Gotchas

From The Open Source Backup Wiki (Amanda, MySQL Backup, BackupPC)

Jump to: navigation, search

'undef' in hashes

If you're building a hash for return to Perl, and you need to add an undef, do not use &PL_sv_undef. Instead, use newSV(0):

 hv_store(hv, "template", 8, &newSV(0), 0);

Boolean Values

If you're using boolean values, use &PL_sv_yes or &PL_sv_no, instead of making new IV's.

SWIG doesn't like SWIG's strings

When you access a member of a SWIGged struct directly via hashref syntax:

my $foo = $hdr->{'name'};

you get back a "magic" scalar. This is used to support setting those values:

 $hdr->{'name'} = "jimmy";

but unfortunately, SWIG doesn't like its magic strings. This won't work:

 print Amanda::Util::quote_string($hdr->{'name'});

to work around this, re-stringifiy it with

 print Amanda::Util::quote_string("".$hdr->{'name'}); # SWIG sucks
Personal tools