• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 736
  • Last Modified:

Perl to C++

I have a perl script that i need converting to C++, am not that good a programmer, does anyone know of a programm that comverts or would do it them selves.
0
frankle
Asked:
frankle
1 Solution
 
bebonhamCommented:
well post the perl script :)

I don't believe there is such a tool...

however

1. you can compile perl to exe

2. you can compile perl to byte code.

but if you actually want it in c++, I think that would have to be done by hand.
0
 
jkrCommented:
Check http://webmaster.indiana.edu/perl56/pod/perlcompile.html - that's not a converter, but something that comes close:

"Because its original purpose was to be a way to produce C code corresponding to a Perl program, and in turn a native executable, the B module and its associated back ends are known as ``the compiler'', even though they don't really compile anything. Different parts of the compiler are more accurately a ``translator'', or an ``inspector'', but people want Perl to have a ``compiler option'' not an ``inspector gadget''. What can you do?"
0
 
AxterCommented:
Please post the code.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
frankleAuthor Commented:
The code is:

#!/usr/bin/perl
# liste skal laves til en t?ller og s? skal "markers" laves med en t?ller og
#med en nline der bliver talt op.
#use strict;

$MCL = 1;
$MCH = 0;
@MA[0] = ("0");
@line[0]=(9999999);
$nline = 0;
$numb = 0;
$var = 0;
$nosem = 0;
$in_pos = 0;

# Slurp whole file into an array of bytes.
# and then copy to the string $fs
open(IN, "mission.sqm")  or die "Can't open mission.sqm : $!";
binmode IN;
$/ = undef;
my @bytes = split //, <IN>;
close IN;
$lenread = ($#bytes +1);
# my bit taken from "unpack.pl" also removed other file stuff. <|:)

while($in_pos < @bytes) {
      #print $bytes[$in_pos];
      $fs = $fs . $bytes[$in_pos++];
}
#print "fs ", $fs,"\n";

$fp = (index $fs,".sqm") +7;

#print "lenread ", $lenread,"\n";
do
{
      MSWITCH: {
            $t = ord(substr ($fs,$fp,1));
            #print "\n nl",$nline;
            if ($numb > 0)
                  {
                        if ($numb == 1)
                              { printf "%f",unpack("f",substr($fs,$fp,4)); $fp=$fp+4;}
                        if ($numb == 2)
                              { printf "%lu",unpack("L",substr($fs,$fp,4)); $fp=$fp+4;}
                        $var--;
                        if ($var <=0)
                        {
                              $var=0;
                              if ($nosem == 0)
                                    { print ";"; }
                              $list=0;$arr=0;$numb=0;$var=0;last MSWITCH;
                        }
                        else
                        {
                              print ",";
                              $nosem =1;
                        }
                        $numb = ord(substr($fs,$fp++,1));
                        if ($numb >2)
                              { print " #nL",$number;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                        if ($numb == 0)
                        {
                                    $list = $var;$nosem=0;$arr=0;$numb=0;$var=0;last MSWITCH;
                        }
                        else
                        {
                                    $arr=0;$list=0;last MSWITCH;
                        }
                  }

            if ($line[$nline] <= 0 and $nline >0 and $list ==0)
            {
                  if ($nosem == 0)
                  {
                        print "\n";
                        for ($i=0;$i < ($nline-1);$i++)
                              { print "\t"; }
                  }
                  $nosem=0;
                  print "};";
                  $nline--;
                  if ($line[$nline] <= 0)
                        { last MSWITCH; }
            }
                  
            if ($t == 1 and $var == 0)
                  {
                        $fp++;
                        if (ord(substr ($fs,$fp,1)) > 2)
                              { print " #V",$t;$nosem=0;$list=0;$numb=0;$var=0;$arr=0;last MSWITCH }
                        $var = 1;$arr=0;$list=0;$nosem=0;
                        #print "\nset var";
                        $fp++;
                        last MSWITCH;
                  }
                  
            if ($t == 2 and $arr == 0 and $MCL != 2)
                  {
                        $fp++;
                        $arr = -1;
                        #print "\nset arr",$MCL;
                        last MSWITCH;
                  }
                  
            if (($t == $MCL and $MCH == 0)
                  or ($t == $MCL and $MCH > 0 and $MCH == ord(substr($fs,$fp+1,1))))
                  {      
                        $fp++;
                        if ($MCH > 0)
                              { $fp++; }
                        #print "\n in learn ",$list;
                        if ($list > 0)
                        {
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #lL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              $MA[($MCL + ($MCH*256))] =~s/"/""/g;
                              print "\"",$MA[($MCL + ($MCH*256))],"\"";
                              $fp=$ep+1;
                              if (++$MCL == 128)
                                    { $MCH++; }
                              if ($MCL == 256)
                              {
                                    $MCL = 128;
                                    $MCH++;
                              }                  
                              $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                              }
                              $numb=0;$var=0;$arr=0;last MSWITCH;
                        }
                        
                        if ($arr != 0)
                        {
                              #print "\nl arr";
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #aL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($MCL + ($MCH*256))],"[]={";
                              if (++$MCL == 128)
                                    { $MCH++; }
                              if ($MCL == 256)
                              {
                                    $MCL = 128;
                                    $MCH++;
                              }
                              $fp=$ep+1;
                              # skal erstates!
                              $th = ord(substr($fs,$fp++,1));
                              $line[++$nline]=0;
                              if ($th == 0)
                                    { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                              $numb = ord(substr($fs,$fp++,1));
                              if ($numb >2)
                                    { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              if ($numb == 0)
                              {
                                          $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                              }
                              else
                              {
                                          $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                              }                              
                        }
                        
                        if ($var == 1)
                        {
                              $numb = ord(substr ($fs,$fp-2,1));
                              if ($MCH > 0)
                                    { $numb = ord(substr ($fs,$fp-3,1)); }
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #vL",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($MCL + ($MCH*256))],"=";
                              $ep++;
                              $list = 0;
                              if ($numb == 0)
                              {
                                    $list = 1;
                              }
                              $fp=$ep;
                        }
                        else
                        {
                              $ep = index ( $fs,"\0",$fp);
                              if ($ep <= $fp)
                                    { print " #cL",$t;$list=0;$numb=0;$nosem=0;$var=0;$arr=0;last MSWITCH; }      
                              $MA[($MCL + ($MCH*256))] = substr ($fs,$fp,($ep - $fp));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "class ",$MA[($MCL + ($MCH*256))],"\n";
                              $fp=$ep +2;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "{";
                              $line[++$nline] = ord(substr($fs,$fp++,1));
                              if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                    { $fp++ };
                              $numb = 0;
                        }
                        if (++$MCL == 128)
                              { $MCH++; }
                        if ($MCL == 256)
                        {
                              $MCL = 128;
                              $MCH++;
                        }                                    
                  $var = 0;
                  last MSWITCH;                                                      
                  }
            if ($t > 0 and $t < $MCL and $t < 128 )      
                  {      
                        $fp++;
                        #print " in ";
                        if ($list > 0)
                        {
                              print "\"",$MA[($t)],"\"";
                              $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                                 }                  
                              $numb=0;$var=0;$arr=0;last MSWITCH;
                        }
                        if ($arr != 0)
                        {
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($t)],"[]={";
                              # skal erstates!
                              $th = ord(substr($fs,$fp++,1));
                              $line[++$nline]=0;
                              if ($th == 0)
                                    { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                              $numb = ord(substr($fs,$fp++,1));
                              if ($numb >2)
                                    { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                              if ($numb == 0)
                              {
                                          $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                              }
                              else
                              {
                                          $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                              }                              
                        }
                        if ($var == 1)
                        {
                              $numb = ord(substr ($fs,$fp-2,1));
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print $MA[($t)],"=";
                              $list = 0;
                              if ($numb == 0)
                              {
                                    $list = 1;
                              }
                        }
                        else
                        {       
                              print "\n";
                              $line[$nline]--;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "class ",$MA[($t)],"\n";
                              $fp++;
                              for ($i=0;$i < $nline;$i++)
                                    { print "\t"; }
                              print "{";
                              $line[++$nline] = ord(substr($fs,$fp++,1));
                              if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                    { $fp++ };
                              $numb = 0;
                        }
                        $var = 0;
                        last MSWITCH;                                                      
                   }
            if (($t > 127 and $MCH > 0))
                  {      
                        $fp++;
                        $th=ord(substr ($fs,$fp,1));
                        #print "\n th ",$th;
                           if($MCH >= $th)
                        {
                              $fp++;
                              if ($list > 0)
                              {
                                    print "\"",$MA[($t+($th*256))],"\"";
                                    $list--;
                              if ($list <=0)
                              {
                                    $list=0;
                                    if ($nosem == 0)
                                          { print ";"; }
                                    $nosem = 0;
                              }
                              else
                              {
                                    print ",";
                                    $nosem =1;
                              }
                                    $numb=0;$var=0;$arr=0;last MSWITCH;
                              }
                              if ($arr != 0)
                              {
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print $MA[($t)],"[]={";
                                    # skal erstates!
                                    $th = ord(substr($fs,$fp++,1));
                                    $line[++$nline]=0;
                                    if ($th == 0)
                                          { $numb=0;$var = 0;$nosem=1;$arr=0;$list=0;last MSWITCH;}
                                    $numb = ord(substr($fs,$fp++,1));
                                    if ($numb >2)
                                          { print " #,L",$t;$list=0;$nosem=0;$numb=0;$var=0;$arr=0;last MSWITCH; }      
                                    if ($numb == 0)
                                    {
                                                $list = $th;$nosem=1;$arr=0;$numb=0;$var=0;last MSWITCH;
                                    }
                                    else
                                    {
                                                $var = $th;$nosem=1;$arr=0;$list=0;last MSWITCH;
                                    }                              
                              }
                              if ($var == 1)
                              {
                                    $numb = ord(substr ($fs,$fp-3,1));
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print $MA[($t+($th*256))],"=";
                                    $list = 0;
                                    if ($numb == 0)
                                    {
                                          $list = 1;       
                                    }
                              }
                              else
                              {
                                    print "\n";
                                    $line[$nline]--;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print "class ",$MA[($t+($th*256))],"\n";
                                    $fp++;
                                    for ($i=0;$i < $nline;$i++)
                                          { print "\t"; }
                                    print "{";
                                    $line[++$nline] = ord(substr($fs,$fp++,1));
                                    if ($line[$nline] > 127) #ignore next byte "I'm lazy"
                                          { $fp++ };
                                    $numb = 0;
                              }
                        }
                        else
                        {
                              print " #P",$t;$list=0;$nosem=0;
                        }
                        $var = 0;
                        last MSWITCH;                                                      
                   }
            $fp++;
            #print " #",$t;
            if ($t > 0)
            {
                  print " #",$t;
                  $list = 0;
                  $numb = 0;
                  $var = 0;
                  $nosem = 0;
            }
            last MSWITCH;                                                      
      }
} until ($fp > $lenread);                              
print "\n";
0
 
frankleAuthor Commented:
Is it possible to convert to a DLL, it would be much easyier, all i want to do is stick a C++ front end on for windows. There is probably a much easyier way but i like C++.
0
 
frankleAuthor Commented:
By the way it is not my code. It is German so don't ask about the comments
0
 
bebonhamCommented:
well after reading through that pretty quickly (quite a lot of code!!) I can tell you a few things for sure.

--that script is an excellent candidate for conversion....all it does is translate a binary file using arrays -- ! it doesn't even use associate arrays, so it should be ..in one way.. very easy to port to c.

jkr's link seems well worth investigating in my opinion.  Although the code isn't beginner perl, it is very low-level and basic, so it should be pretty easy to translate with that tool.

As far as rewriting it... geez it's awfuly long, and it would take someone very familiar with bit level manipulation, which is not me in perl, and certainly not in c :)

my book tells me not to focus on that stuff ?
0
 
griesshCommented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to accept "jkr" comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points.
The link to the Community Support area is: http://www.experts-exchange.com/commspt

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
NetminderCommented:
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now