Programming Perl in C
variables without worrying about when exactly the current scope will end; the
answer is usually soon enough, which is usually good enough!
The Perl Environment
Often when you're writing a C module for Perl you'll need to interact with the Perl
environment. The most basic means for this interaction providing functions
written in C that can be called from Perl will be described later in Chapters 9 and
10. This section is about going the other way calling back into Perl from C.
Accessing Variables
The simplest way to get information from Perl is to access global and package vari
ables. The Perl API supports this with the get_ family of calls:
SV *sv = get_sv("Data::Dumper::Purity", 0); // access $Data::Dumper::Purity
AV *av = get_av("main::DATA", 1); // create/access the global @DATA
HV *av = get_hv("main::VALUES", 1); // create/access the global %VALUES
Each of these calls take two parameters the fully qualified name of the variable
and a Boolean indicating whether to create the variable if it doesn't yet exist. By
using a get_ function with the second argument set to true, you can create new
variable in Perl space from C. If you set the second parameter to false, the calls will
return NULL if the variable cannot be found.
Calling Perl Subroutines from C
The subroutine calling convention is probably the Perl API's most complicated
feature. Fortunately for you, it's also its best documented one. Perl comes with an
excellent manual page on the subject perlcall. I'll demonstrate some simple
examples here; you can find all the gritty details in perlcall.
Example 1: No Parameters, No Return Value
The simplest type of call is one that passes no parameters and accepts no return
values. Let's say I've defined a subroutine in the package Hello called say_hello:
sub say_hello {
print "Hello, world.\n";
}
19
197
7
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved