Get an integer from IV * in Perl

Is there a macro or function in the Perl API to get the actual integer field IV* ? I can find a ton of information to get almost any other value in perlguts and perlapi , but this one seems to elude me.

+4
source share
2 answers

From perlguts SvIV(SV*) should do the trick.

 #!/usr/bin/env perl use strict; use warnings; use Inline C => <<'END'; void print_iv (SV* input) { if (! SvIOK(input)) croak("Not an integer"); printf("Printing integer %d\n", SvIV(input)); } END print_iv(3); 
+4
source
 use Inline C => <<'__EOC__'; void print_iv(SV* input) { SvGETMAGIC(input); printf("Printing integer %"IVdf"\n", SvIV(input)); } __EOC__ print_iv(3); 

This fixes three errors in the previous answer:

  • You need to call SvGETMAGIC before accessing the scalar if it is magic (special variable).
  • SvIOK should not be checked. How the number is stored, it does not matter.
  • %d not always suitable for IV . Failure to use the correct template may result in segfault.
+3
source

Source: https://habr.com/ru/post/1483502/


All Articles