Page 1 of 1

Inline Code with Perl Rate Topic: -----

#1 trixt.er  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 46
  • View blog
  • Posts: 419
  • Joined: 28-September 08

Posted 10 August 2012 - 06:22 AM

The following is a tutorial for embedding inline C into a Perl script.
Hypothetically speaking, the Inline Perl module library provides support
for other famous languages such as Java. Since I use Perl in CGI,
I have found it beneficial to intermix other languages for file processing (I/O).

Prerequisites:
1. Download the Inline modules from CPAN. See http://search.cpan.o...ine-Support.pod
2. Perl translator must be installed
3. GCC GNU compiler




The header of your file should pull in the Inline C modules.
Make sure the inline library comes before the strict library.

#!/usr/bin/perl 
#
# Some fun with C and assembly in Perl.
# 
use Inline C;
use strict;



Pass in a string to an inline C function:

my $returnedVal = someCcode("trixt.er");

print "Looks like C returned => $returnedVal\n";



The inline section is defined as:
__END__
__C__

struct CStruct
{
    int num;
    char* cstring;
};

char* someCcode(char* name)
{
   struct CStruct cstruct;
   cstruct.num = 1986;
   cstruct.cstring = "I was born in... ";
   printf("Hello friends, my alias name is %s\n", name);
   printf("%s%d\n", cstruct.cstring, cstruct.num);
   return "Shoot for the moon!";
}



It's important to use the __END__ here. The END block, tells the Perl translator
to wait as long as possible to initiate the compilation and execution of the C code.
It's also important to define the C code below the __C__ flag.

The following is the final product, intermixed with some enhance inline assembly.
In this example I'm using __volatile__ with the assembly, so the compiler doesn't
try to super optimize my assembly code. I did this on purpose, since the assembly code
is trivial and functional. This code is free for your personal use. I developed it
on the following system:

The code:
#!/usr/bin/perl 
#
# Some fun with C and assembly in Perl.
#  
use Inline C;
use strict;

my $returnedVal = someCcode("trixt.er");

print "Looks like C returned => $returnedVal\n";

print "Type a number -> ";
my $num1 = <STDIN>;
chomp($num1);

print "Type a number to add to $num1-> ";
my $num2 = <STDIN>;
chomp($num2);
my $result = cWithAsm($num1, $num2);

print "Hot of the assembly press in C --> $num1 + $num2 = $result\n";

__END__
__C__

struct CStruct
{
    int num;
    char* cstring;
};

char* someCcode(char* name)
{
   struct CStruct cstruct;
   cstruct.num = 1986;
   cstruct.cstring = "I was born in... ";
   printf("Hello friends, my alias name is %s\n", name);
   printf("%s%d\n", cstruct.cstring, cstruct.num);
   return "Shoot for the moon!";
}

int cWithAsm(int num1, int num2)
{
    char msg[] = "Hello, world";

    /* Add num1 and num2. Store result into result variable. */
    int result = 0;
    __asm__ __volatile__
    (
         "addl %%ebx, %%eax;" // reference values in parameters (0) and (1)
       : "=a" (result) // store the result
       : "a" (num1), "b" (num2) // assign parameters to registers
    );
    return result;
}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1