adler187 has asked for the wisdom of the Perl Monks concerning the following question:
Do I really have to do something like this:void foo(SV * sv) { if(sv) { printf("%s\n", SvPV_nolen(sv)); } } void bar(SV * sv) { printf ("%i\n", SvPOK(sv)); } Mytest2::foo('asdf'); # prints asdf Mytest2::bar(1); # prints 0 Mytest2::foo(1); # Segmentation Fault
This is on perl 5.14.2, linux x86-64void foo(SV * sv) { char buf[1024]; switch(SvTYPE(sv)) { case SVt_IV: snprintf(buf, sizeof(buf), "%s\n", SvPV(sv)); break; case SVt_PV: snprintf(buf, sizeof(buf), "%d\n", SvIV(sv)); break; case SVt_NV: snprintf(buf, sizeof(buf), "%f\n", SvNV(sv)); break; ... handle scalar refs here ... } printf("%s\n", buf); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: SvPV Segmentation Fault
by BrowserUk (Patriarch) on Apr 25, 2012 at 20:52 UTC | |
by adler187 (Initiate) on Apr 25, 2012 at 21:19 UTC | |
by dave_the_m (Monsignor) on Apr 25, 2012 at 22:21 UTC | |
by adler187 (Initiate) on Apr 25, 2012 at 22:39 UTC | |
by dave_the_m (Monsignor) on Apr 25, 2012 at 23:14 UTC | |
| |
by BrowserUk (Patriarch) on Apr 25, 2012 at 21:47 UTC | |
|
Re: SvPV Segmentation Fault
by Khen1950fx (Canon) on Apr 25, 2012 at 22:35 UTC |