develooper Front page | perl.vmsperl | Postings from January 2002

Re: Putting the perl source in with the embedding executable?

Thread Previous | Thread Next
From:
Forrest Cahoon
Date:
January 31, 2002 12:26
Subject:
Re: Putting the perl source in with the embedding executable?
Message ID:
OF7CB96CBA.F059C898-ON86256B52.00562D81@stp.mrll.com

Thanks, Dan, that works great.

Here's my embedding script for anyone who wants it:

#!/usr/bin/perl -w

use ExtUtils::Embed;
use strict;

my ($perl_fname, $perl_contents, $err, $c_fname, $xsi);
$perl_fname = $ARGV[0];
if (!-e $perl_fname) { die "Sorry, $perl_fname doesn't exist\n:" }
undef $/;
open IN, $perl_fname || die "Couldn't open $perl_fname for reading\n";
$perl_contents = <IN>;
close IN;

# Check syntax.
$err = "";
eval "return; " . $perl_contents;
if ($@) {
   ($err = $@) =~ s/\(eval 1\)/$perl_fname/g;
      die "Please fix these errors in $perl_fname and try again:\n$err\n";
}

# ExtUtils::Embed::xsinit was never designed to be called from a program
# quite like this.  Oh well, this works for me.

ExtUtils::Embed::xsinit("perlxsi.c",1);
if (!-e "perlxsi.c") {
   die "Sorry, xsinit couldn't create perlxsi.c\n";
}
open XSI, "perlxsi.c" || die "Couldn't open the perlxsi.c " .
  "file that I just created (weird!)\n";
$xsi = <XSI>;
close XSI;
unlink "perlxsi.c";

# Generate a C file name from the perl one:
# something.pl generates something.c
# something (no extension) generates something.c
if ($perl_fname =~ /^(.*)\./) {
   $c_fname = $1 . ".c";
} else {
   $c_fname = $perl_fname . ".c";
}

# Escape chars special to C in the perl source for embedding.
$perl_contents =~ s/([\"\\])/\\$1/sg;
$perl_contents =~ s/\n/\\n\\\n/g;

open OUT, ">$c_fname" || die "Couldn't open $c_fname for writing\n";
print OUT <<EOF;
/*
** This code was generated by wrap_perl.
**
** You probably want to edit your original source
** $perl_fname and re-run wrap-perl
** instead of editing the C code here.
**
*/

$xsi

static char perl_source[] = "\\
$perl_contents";

static PerlInterpreter *my_perl;
main (int argc, char **argv, char **env)
{
   char fname[256];
   char *perl_argv[] = {argv[0], "-e", perl_source};
   char *sub_argv[argc];
   int i, status;

   memset(sub_argv, 0, sizeof(sub_argv));
   for (i=1 ; i<argc ; i++) {
      sub_argv[i-1] = argv[i];
   }

   my_perl = perl_alloc();
   perl_construct(my_perl);
   perl_parse(my_perl, xs_init, 2, perl_argv, env);
   perl_run(my_perl);
   perl_destruct(my_perl);
   perl_free(my_perl);
}
EOF
close OUT;
__END__

Forrest
not speaking for merrill corporation



                                                                                            
                    Dan Sugalski                                                            
                    <dan@sidhe.org       To:     "Forrest Cahoon"                           
                    >                     <forrest.cahoon@merrillcorp.com>,                 
                                          vmsperl@perl.org                                  
                    01/30/2002           cc:                                                
                    05:25 PM             Subject:     Re: Putting the perl source in with   
                                          the embedding executable?                         
                                                                                            




At 5:14 PM -0600 1/30/02, Forrest Cahoon wrote:
>Is it possible to get my perl source into the same executable file as my
>wrapper?  Some linker trick, maybe?
>Or putting all the perl source into a buffer in my wrapper code? (Uck ...
>need to write a perl tool to do that ... oh, not uck)

Sure, embed it in the args passed to perl. Make one "-e" and the next
the full text of your program.
--
                                         Dan

--------------------------------------"it's like this"-------------------
Dan Sugalski                          even samurai
dan@sidhe.org                         have teddy bears and even
                                       teddy bears get drunk





Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About