The next thing I wanted was the ability to dynamically load other Perl extensions such as the Tk extension, database extensions or anything else that might prove useful. This requires an xs_init function in place of the NULL in perl_parse as shown below.
perl_parse(MyPerl, xs_init, 2, my_argv, env);
To create the xs_init, I used the following code:
perl -MExtUtils::Embed -e xsinit -- -o - >xs_init.cThe function of xs_init is to initialize the statically linked extension modules. The only module I statically linked is the DynaLoader module. With this module, we can dynamically load everything else. When I initially did this, I had numerous problems. They turned out to be linked to the version of Perl I was using (5.003_07). After I installed 5.004_04, everything worked fine.
One of the first problems I ran into was the fact that Perl redefined yypars to be Perl_yypars. I fixed this by putting new #define statements around places where I used sc's yypars. This created a lot of compiler warnings, but did allow the code to compile correctly.
The other problem I encountered was with the SvIOK and SvNOK macros. These check an SV for a number or an integer, or more precisely, they check to see if the double-value portion of an SV is valid at that point in the code.
Originally, I had the SvIOK and SvNOK macros around any code to which I was expecting to send an integer. The problem is this will not accept code like the following,
sc_put_num_val("34.3"); # this is in perl
because it is being passed a string value and the number part of the SV was not valid at that time. The SvIV and SvNV macros will convert this to a number even if it is not a valid string. I was parsing strings from a file using regular expressions, and the value I would get in $1 would be a string, even though it was numeric. Once I realized SvNV would produce a number for me, my test script started working.
This example is not the cleanest implementation of embedding Perl into an application. It was meant as a quick solution to a problem. With an embedded Perl interpreter, sc is quite a bit more powerful than before. One example included with the source is a mortgage calculator that grabs the interest rates form the CNN Financial News web site. With all the Perl modules available, the possibilities are endless.
- Readers' Choice Awards 2013
- Linux Kernel News - November 2013
- December 2013 Issue of Linux Journal: Readers' Choice
- Mars Needs Women
- Sublime Text: One Editor to Rule Them All?
- Raspberry Pi: the Perfect Home Server
- RSS Feeds
- Advanced Hard Drive Caching Techniques
- Web Administration Scripts
- New Products
- thanks for share, great
13 hours 43 sec ago
- There are factors which are
18 hours 24 sec ago
- Gnome 3 ?
18 hours 45 min ago
- Reply to comment | Linux Journal
22 hours 52 min ago
- "Redis RethinkDB 4.5%" on Best NoSQL Databases
1 day 8 hours ago
- on the ground
1 day 15 hours ago
- I was able to read the whole
1 day 16 hours ago
- since i have read the title i
1 day 20 hours ago
- Belanja Online Cari Voucher Diskon
1 day 20 hours ago
- The kernel doesn't really
2 days 8 hours ago