Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Silencing warnings inside C/XS

by aufflick (Deacon)
on Oct 08, 2012 at 04:33 UTC ( #997737=perlquestion: print w/ replies, xml ) Need Help??
aufflick has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

Inside XS calls, things like calling methods with undefined values causes very loud (and usually instructive) warnings. It's not uncommon, though, for Perl APIs to have undef as a valid and useful argument - in such cases I don't want my call_method() to fill my error log with noise. I tried a naive approach of:

ENTER; (void)call_pv("use no 'undefined';", 1); // do some stuff with undefined values LEAVE;
But it had no impact. Can anyone suggest how I should do this? I guess I could play with $^W but I'd rather not...

Comment on Silencing warnings inside C/XS
Download Code
Re: Silencing warnings inside C/XS
by Anonymous Monk on Oct 08, 2012 at 07:31 UTC

    use no 'undefined'; But it had no impact.

    Syntax errors rarely do -- try "no warnings 'uninitialized';"

      I doubt that would make it have any impact. "no warnings ..." impacts lexical scope which is likely restricted to the call_pv() call. But even if call_pv() doesn't define (and destroy) a new lexical scope, I still doubt it would have any impact. "no warnings ..." works by tagging the Perl op-codes that are generated. XS code isn't running Perl op-codes, especially not ones compiled after the call to call_pv().

      My main advice is to stop messing with Perl data structures in XS code. This is just one of the problems that result from such. The others tend to be worse.

      - tye        

        My main advice is to stop messing with Perl data structures in XS code.

        Sure, if you want to do it for all your functions you could probably use

        no warnings 'uninitialized'; XSLoader::load();

        Problem with that, its all-or-nothing, if you want to isolate a single sub , you have to set $^WARNING_BITS, because the alternative is this wrapper crap

        sub notFun { no warnings 'uninitialized'; goto &_notFunXS; }
Re: Silencing warnings inside C/XS
by Tanktalus (Canon) on Oct 10, 2012 at 16:36 UTC

    First off, just a disclaimer: I run my production code with use warnings FATAL => 'all'; (or an equivalent). A bit of a masochist, I suppose I am.

    Now, with that said, how do I deal with valid undefs? I check if they're defined before I use them. So, my recommendation for your XS code? Check if they're defined before you use them ;-) I'm sure there's some sort of SvOK() or something that will tell you this.

    Ok, so let's say you want another work around. My experience with just such an annoying undefined message coming from JSON::XS is that you need to say "no warnings 'uninitialized';" outside the function call. So I have code like this:

    { no warnings 'uninitialized'; $text = $json->encode($object); }
    That wrapper alluded to by the anonymous monk would work here. But it's not nearly so ugly as that because this is perl, and we can do wonderful things. Wonderful, horrible things. Well, they'd be horrible in other languages, but I use them a lot in perl.
    use Sub::Name; for my $f (@wrapper_funcs) { my $xs = $f . 'XS'; *$f = subname $f => sub { no warnings 'uninitialized'; &$xs; # @_ gets passed along automatically }; }
    (The "subname $f =>" bit is optional, but can help with backtraces.)

Re: Silencing warnings inside C/XS
by aufflick (Deacon) on Nov 29, 2012 at 01:35 UTC
    Thanks all - in fact it was as simple as you all said and that I thought it wasn't! Turns out there was a place I simply needed to wrap in an SvOK test.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997737]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2014-07-13 05:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (246 votes), past polls