| 1 | package XS::Tutorial::Three; | ||
| 2 | 1 | require XSLoader; | |
| 3 | |||
| 4 | 1 | XSLoader::load(); | |
| 5 | 1 | 1; | |
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | XS::Tutorial::Three - utility routines that are good to know | ||
| 10 | |||
| 11 | =head2 Introduction | ||
| 12 | |||
| 13 | In L<XS::Tutorial::Two>, we learned how to write our own functions in XS, how to | ||
| 14 | process multiple arguments, and return different values, including C<undef>. | ||
| 15 | |||
| 16 | In this tutorial I'm going to cover some useful routines for common cases you'll | ||
| 17 | encounter when programming in XS. One that you've already seen is C<SvOK> which can tell you if a scalar is defined or not. | ||
| 18 | |||
| 19 | =over 2 | ||
| 20 | |||
| 21 | =item * Scheduling XS code to run at startup | ||
| 22 | |||
| 23 | =item * Handling tied variables | ||
| 24 | |||
| 25 | =item * Unicode tools | ||
| 26 | |||
| 27 | =back | ||
| 28 | |||
| 29 | When writing XS code, these are things you'll often want to be aware of, and | ||
| 30 | know how to handle. | ||
| 31 | |||
| 32 | =head2 Module Code | ||
| 33 | |||
| 34 | As before, we'll define the module code to load our XS. This is all that's | ||
| 35 | required: | ||
| 36 | |||
| 37 | package XS::Tutorial::Three; | ||
| 38 | require XSLoader; | ||
| 39 | |||
| 40 | XSLoader::load(); | ||
| 41 | 1; | ||
| 42 | |||
| 43 | |||
| 44 | That should be saved as C<lib/XS/Tutorial/Three.pm>. | ||
| 45 | |||
| 46 | =head2 XS Code | ||
| 47 | |||
| 48 | The top of the XS file will look similar to the previous chapter: | ||
| 49 | |||
| 50 | #define PERL_NO_GET_CONTEXT // we'll define thread context if necessary (faster) | ||
| 51 | #include "EXTERN.h" // globals/constant import locations | ||
| 52 | #include "perl.h" // Perl symbols, structures and constants definition | ||
| 53 | #include "XSUB.h" // xsubpp functions and macros | ||
| 54 | |||
| 55 | MODULE = XS::Tutorial::Three PACKAGE = XS::Tutorial::Three | ||
| 56 | PROTOTYPES: ENABLE | ||
| 57 | |||
| 58 | Remember to append any XS code after the C<PROTOTYPES> line. This should be saved | ||
| 59 | as C<lib/XS/Tutorial/Three.xs>. | ||
| 60 | |||
| 61 | =head2 Scheduling XS code to run at startup | ||
| 62 | |||
| 63 | Sometimes you'll need to run some code before your XS functions can work. For | ||
| 64 | example, L<libpostal|https://github.com/openvenues/libpostal> has startup routines which populate data structures that | ||
| 65 | must be called before the library can be used. | ||
| 66 | |||
| 67 | You could code this in a "lazy" way, that is, inside the XS function, | ||
| 68 | check to see if the init code has been run, and if not, run it before | ||
| 69 | executing the rest of the function code. | ||
| 70 | |||
| 71 | However XS offers another way to do it by using the C<BOOT> keyword. Any C code | ||
| 72 | included below the keyword, will be executed during the startup process: | ||
| 73 | |||
| 74 | BOOT: | ||
| 75 | printf("We're starting up!\n"); | ||
| 76 | |||
| 77 | |||
| 78 | The boot section is terminated by the first empty line after the keyword. | ||
| 79 | |||
| 80 | =head2 Handling tied variables | ||
| 81 | |||
| 82 | Tied variables are special variables that execute custom code when they | ||
| 83 | are interacted with. But you never use them, so why worry about them? The | ||
| 84 | thing is if you're writing code to be used by others, you can't be sure that a | ||
| 85 | caller won't pass a tied variable to one of your XS functions. And unlike | ||
| 86 | regular Perl, XS does B<not> execute tied code automatically. | ||
| 87 | |||
| 88 | XS does provide L<functions|https://perldoc.perl.org/perlapi.html#Magical-Functions> for working with tied variables though. One you'll | ||
| 89 | see in a lot of XS code is C<SvGETMAGIC>. Imagine your function is passed a | ||
| 90 | tied variable; it's value will be undefined in XS, until you call C<mg_get> | ||
| 91 | ("magic get") on it, which calls C<FETCH>. | ||
| 92 | |||
| 93 | Unfortunately, C<mg_get> can only be called on tied scalars so you don't want to | ||
| 94 | call it on a regular scalar. That's where C<SvGETMAGIC> comes in: if the scalar is | ||
| 95 | tied, it will call C<mg_get>, if not, nothing will happen. | ||
| 96 | |||
| 97 | Here's how you might use it: | ||
| 98 | |||
| 99 | SV* | ||
| 100 | get_tied_value(SV *foo) | ||
| 101 | |||
| 102 | PPCODE: | ||
| 103 | /* call FETCH() if it's a tied variable to populate the sv */ | ||
| 104 | SvGETMAGIC(foo); | ||
| 105 | PUSHs(sv_2mortal(foo)); | ||
| 106 | |||
| 107 | This code declares an XS function called C<get_tied_value>, which accepts a | ||
| 108 | scalar variable, and calls C<SvGETMAGIC> on it, returning the value, by | ||
| 109 | pushing it onto the stack. | ||
| 110 | |||
| 111 | =head3 Magic? | ||
| 112 | |||
| 113 | You might be wondering why functions dealing with tied variables are named | ||
| 114 | "magic" or "mg". The reason is that tied behavior for each variable is | ||
| 115 | implemented via a pointer to a L<magic virtual table|https://perldoc.perl.org/perlguts.html#Magic-Virtual-Tables> which is a structure | ||
| 116 | containing function pointers to the tied behavior. | ||
| 117 | |||
| 118 | Often the Perl C API will provide C<mg> ("magic") and C<nomg> ("non magic") | ||
| 119 | variants of functions, so you can decide if you want to trigger the tied | ||
| 120 | behavior. | ||
| 121 | |||
| 122 | =head2 UTF-8 tools | ||
| 123 | |||
| 124 | Perl has loads of tools for managing UTF-8 encoded text, but with XS you're | ||
| 125 | working in C, which does not. Start thinking about basic types like C<char> | ||
| 126 | and common assumptions in C code, and you'll realize that multibyte characters | ||
| 127 | can wreak havoc unless you handle them correctly. | ||
| 128 | |||
| 129 | Fortunately, the Perl C API does provide L<functions|https://perldoc.perl.org/perlapi.html#Unicode-Support> for managing UTF-8 data that | ||
| 130 | can help. Here are a couple of examples. | ||
| 131 | |||
| 132 | Perl scalars have a UTF-8 flag, which is turned on when the scalar contains | ||
| 133 | UTF-8 data. We can detect it with C<SvUTF8>: | ||
| 134 | |||
| 135 | SV* | ||
| 136 | is_utf8(SV *foo) | ||
| 137 | PPCODE: | ||
| 138 | /* if the UTF-8 flag is set return 1 "true" */ | ||
| 139 | if (SvUTF8(foo)) { | ||
| 140 | PUSHs(sv_2mortal(newSViv(1))); | ||
| 141 | } | ||
| 142 | /* else return undef "false" */ | ||
| 143 | else { | ||
| 144 | PUSHs(sv_newmortal()); | ||
| 145 | } | ||
| 146 | |||
| 147 | This declares an XS function called C<is_utf8> which accepts a scalar and returns | ||
| 148 | true if the UTF-8 flag is set, or false if it isn't. | ||
| 149 | |||
| 150 | Imagine you have some C code that only works with ASCII text, that is, single byte | ||
| 151 | characters. You can detect incoming scalars that have the UTF-8 flag turned on | ||
| 152 | with C<SvUTF8>, but what do you do about it ones that have the flag? | ||
| 153 | |||
| 154 | You could C<croak> immediately, throwing an exception. Or you could try to I<downgrade> | ||
| 155 | the scalar to be non UTF-8 as the string may be marked as UTF-8 but only contain ASCII | ||
| 156 | compatible characters (decimal values 0-127). | ||
| 157 | |||
| 158 | SV* | ||
| 159 | is_downgradeable(SV *foo) | ||
| 160 | PPCODE: | ||
| 161 | /* if the UTF-8 flag is set and the scalar is not downgrade-able return | ||
| 162 | undef */ | ||
| 163 | if (SvUTF8(foo) && !sv_utf8_downgrade(foo, TRUE)) { | ||
| 164 | PUSHs(sv_newmortal()); | ||
| 165 | } | ||
| 166 | /* else return 1 */ | ||
| 167 | else { | ||
| 168 | PUSHs(sv_2mortal(newSViv(1))); | ||
| 169 | } | ||
| 170 | |||
| 171 | This function returns true if the scalar contains UTF-8 data I<and> it is | ||
| 172 | downgrade-able to ASCII. It does that by using the C<sv_utf8_downgrade> | ||
| 173 | function, which accepts the scalar and a boolean value indicating if it's | ||
| 174 | ok to fail. As the second argument is C<TRUE>, the function simply returns | ||
| 175 | false if the scalar is not downgrade-able (otherwise it would C<croak>). | ||
| 176 | |||
| 177 | |||
| 178 | =head2 References | ||
| 179 | |||
| 180 | =over 4 | ||
| 181 | |||
| 182 | =item * L<XS::Tutorial::One> and L<XS::Tutorial::Two> contain the background information necessary to understand this tutorial | ||
| 183 | |||
| 184 | =item * The L<BOOT|https://perldoc.perl.org/perlxs.html#The-BOOT%3a-Keyword> keyword | ||
| 185 | |||
| 186 | =item * Tied variable L<functions|https://perldoc.perl.org/perlapi.html#Magical-Functions> and the L<magic virtual table|https://perldoc.perl.org/perlguts.html#Magic-Virtual-Tables> | ||
| 187 | |||
| 188 | =item * L<Perl UTF-8 functions|https://perldoc.perl.org/perlapi.html#Unicode-Support> | ||
| 189 | |||
| 190 | =back | ||
| 191 | |||
| 192 | =cut |