Hi,
Here is a problem on initialization of globals while embedding Perl inside C.
Consider the following code:
=======================
package Test;
require Exporter;
@ISA =Exporter;
use Config::Inifiles;
our $my_config = new Config::Inifiles(-file => "config.ini");
sub my_test
{
print "[ key = ".$my_config->val("section", "key")."]\n";
}
===============
This piece of code works fine when I invoke my_test function from a Perl file. The correct value for a given key is printed.
But this doesnt work when I invoke my_test through C code.
However, if I move the global initialization code to another Perl module (say globals.pm), and then invoke the function my_test through C, it works.
The new Perl code would be:
====================
Globals.pm
package globals;
require Exporter;
@ISA = (Exporter);
@EXPORT = qw(init, $my_config);
use Config::Inifiles;
sub init
{
$my_config = new Config::Inifiles(- file => "config.ini");
}
1;
and Test.pm is as follows:
package Test;
require Exporter;
@ISA = Exporter;
use globals;
sub my_test
{
globals::init();
print "[ key = ".$globals::my_config->val("section", "key")."]\n";
}
Can someone explain this behavior? Why does globals initialization not take place when Perl function is invoked through C code?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.