<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Engwar &#187; embedding</title>
	<atom:link href="http://engwar.com/tags/embedding/feed" rel="self" type="application/rss+xml" />
	<link>http://engwar.com</link>
	<description>Chintana Wilamuna&#039;s weblog</description>
	<lastBuildDate>Tue, 06 Mar 2012 07:32:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Perl embedding example</title>
		<link>http://engwar.com/post/42?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=perl-embedding-example</link>
		<comments>http://engwar.com/post/42#comments</comments>
		<pubDate>Wed, 09 Jul 2008 05:56:02 +0000</pubDate>
		<dc:creator>Chintana</dc:creator>
				<category><![CDATA[embedding]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://engwar.com/?p=42</guid>
		<description><![CDATA[Since there was no progress from my last experiment, writing a rudimentary program without any WSF/C code seemed like a logical next step. For those of you who don&#8217;t know what I tried to do, here&#8217;s a brief explanation. I &#8230; <a href="http://engwar.com/post/42">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Since there was no progress from my <a href="http://engwar.com/post/40">last experiment</a>, writing a rudimentary program without any WSF/C code seemed like a logical next step.  For those of you who don&#8217;t know what I tried to do, here&#8217;s a brief explanation.  I want to let other people provide Web services in Perl.  My objective is to enable people to just copy their Perl scripts to, say, a folder and it&#8217;ll automatically become a Web service.  Let me explain that with an example.  Say you have the following Perl script,</p>
<pre lang="perl">
#!/usr/bin/env perl

sub getRandomNumber {
    return 4;
}
</pre>
<p>I&#8217;m going to assuming you have configured your web server to execute Perl scripts ending with .pl via ModPerl for a directory called perl.  You copy the file and it&#8217;s accessible from http://localhost/perl/random.pl.  And it&#8217;s a web service now.  So you can send an XML payload like,</p>
<pre lang="xml">
<getRandomNumber />
</pre>
<p>And get back something like,</p>
<pre lang="xml">
<number>4</number>
</pre>
<p>Since I&#8217;m using <a href="http://wso2.org/projects/wsf/c">WSF/C</a> as the underlying Web service engine and it provides various other facilities like WS-Security, WS-ReliableMessaging etc&#8230; at blazing speed, I wanna use it.</p>
<p>Here&#8217;s a simplified graphical version of the story,</p>
<p><img src="http://engwar.com/wp-content/uploads/2008/07/wsf-perl-service-arch.png" alt="" title="WSF/Perl service architecture" width="570" height="406" class="aligncenter size-full wp-image-43" /></p>
<p>1. Request comes to rand.pl via ModPerl and it gets executed.<br />
2. rand.pl then calls some methods in Service.pm module which in turn calls to some C functions.<br />
3. From one of those C functions we need to call a Perl function residing in the script the request came into.</p>
<p>If you&#8217;re wondering how the graphic relates to the story of copying your Perl script and making it a service (&#8216;cos there&#8217;s a Service.pm in the picture), please note that I&#8217;ve not yet figured out every detail in the system <img src='http://engwar.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Perl is segfaulting at all the bizzare places when this is executed. When things get this bad it&#8217;s time to write a very simple application that does the same thing without any WSF/C code.  Here&#8217;s how I did it.</p>
<p>I started by creating a new Perl extension in C.</p>
<pre>
$ h2xs -A -n ICanHaz
</pre>
<p>This&#8217;ll create the skeleton and all I have to do is edit the .xs file and put my code there.  Here&#8217;s my ICanHaz.xs file,</p>
<pre lang="c">
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <string.h>

#include "ppport.h"

static	PerlInterpreter *my_perl;

void cheeseburger( void )
{
    char *embedding[] = {"/usr/local/apache2/perl/exec-this.pl"};

    my_perl = perl_alloc();
    perl_construct(my_perl);
    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
    perl_parse(my_perl, NULL, 1, embedding, NULL);
    perl_run(my_perl);
    perl_destruct(my_perl);
    perl_free(my_perl);
    PERL_SYS_TERM();
}

MODULE = ICanHaz		PACKAGE = ICanHaz		

void
cheeseburger()
</pre>
<p>Now you have to edit Makefile.PL and give the output of, <code>perl -MExtUtils::Embed -e ccopts -e ldopts</code> to the compiler flags.  After that you can compile and install the module.  Here&#8217;s what&#8217;s inside exec-this.pl,</p>
<pre lang="perl">
sub keele_teh_urth {
    print "oh noez. i iz gud kitteh";
}
</pre>
<p>Just a simple function.  Then I wrote another Perl script to invoke the method in my C extension.  Here&#8217;s the program,</p>
<pre lang="perl">
#!/usr/local/bin/perl

use ICanHaz;

ICanHaz::cheeseburger();

print "Content-type: text/html\n\n";
print "dis segfaults, srsly.\n";
</pre>
<p>Executing this file via ModPerl results in a segmentation fault.  As you can see even the simplest case of embedding will not execute through ModPerl.  Switched to CGI and got the same results.  Possible alternatives to try is 1) move all the embedding stuff to it&#8217;s own executable, when you need to call the Perl function from C do a fork followed by exec, pass the payload as a parameter, store the result in a shared memory and once done read from it (this .. um .. a bit ugly to say the least).  Second alternative is to use <a href="http://search.cpan.org/~adamk/PPI-1.203/lib/PPI.pm">PPI</a>.  I haven&#8217;t looked into PPI yet so don&#8217;t know for sure whether it&#8217;s possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://engwar.com/post/42/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

