Solved

Perl to C++

Posted on 2001-08-16
9
707 Views
Last Modified: 2011-09-20
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
Comment
Question by:frankle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 8

Expert Comment

by:bebonham
ID: 6394392
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
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 6394522
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
 
LVL 30

Expert Comment

by:Axter
ID: 6394539
Please post the code.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:frankle
ID: 6394890
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
 

Author Comment

by:frankle
ID: 6394898
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
 

Author Comment

by:frankle
ID: 6394902
By the way it is not my code. It is German so don't ask about the comments
0
 
LVL 8

Expert Comment

by:bebonham
ID: 6395121
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
 
LVL 11

Expert Comment

by:griessh
ID: 6858004
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
 
LVL 5

Expert Comment

by:Netminder
ID: 6883628
Per recommendation, force-accepted by
Netminder
Community Support Moderator
Experts Exchange
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question