|
 |
|
 |
11-13-2006, 09:23 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 9
|
perl to c++
hi all,
i wanna ask is there a good way to translate perl code into c code? doing it line by line doesn't sound good, so anyone have a better way plz post it. thx
|
|
|
11-13-2006, 09:49 AM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,693
|
Quote:
|
doing it line by line doesn't sound good
|
How would you do it then ??
You need to read the lines of Perl, understand what they do, and translate that into the appropriate C/C++ handling. This might circumvent the true line by line tranlastion, since you'd be looking at portions of the orriginal code and translate that, but all in all it will basicaly be a line by line translation.
|
|
|
11-13-2006, 04:31 PM
|
#3 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
I won't say there's no way to do it, but there's no easy way to do it. Your best option is to find a perl programmer that's also a C programmer. Your next option is to just write something new that does what the old code was supposed to do (presumably working off the spec, rather than the code). Your final option is to ask yourself why you want to do this in the first place.
__________________
Stop intellectual property from infringing on me
|
|
|
11-14-2006, 04:43 PM
|
#4 (permalink)
|
|
Recruit
Join Date: Nov 2006
Posts: 9
|
thx for replying, i am stuck with this assignment. can some one help me figure things out? I translated derived class from perl to c, but i can't do the abstract base class, and also i am supposed to find bug for it too, but i can't find any.
so help me out if anyone is familiar with perl and c++.
thx
Quote:
# The Carp.pm package provides "carp" and "croak",
use Carp;
use Carp "cluck";
=head1 NAME
Pet - Abstract base class for pets.
=head1 SYNOPSIS
use Pet;
=head2 Class Methods
$pet_object = Pet::*->new($name);
Pet: og, Pet::Monkey, Pet::Bird
=head2 Object Methods
$pet_object->speak;
$pet_object->feed($food_type);
=head1 DESCRIPTION
=cut
# Method prototypes
sub new($); # one scalar argument
sub speak(); # no scalar arguments
sub feed($); # one scalar argument
# Creator method for pets.
# Called by Pet::*->new($name);
sub new ($) {
# When we say "Pet::*->new(...)", the perl passes the calling class as the
# first argument. The second argument is the name of the pet.
my ($class, $name) = @_;
# An alternative way to get the function arguments:
# my $class = $_[0];
# my $name = $_[1];
# or
# my $class = shift;
# my $name = shift;
# We can't create a generic Pet, since this is an abstract base class.
if ($class eq __PACKAGE__) {
carp "Can't create a generic pet";
return;
}
# You must supply a name.
unless ($name) {
carp "Can't create a $class without a name";
return;
}
# Create a hash reference that will hold our
# abstract data type.
my $pet = {
'name' => $name
};
# Bless our hash reference as belonging to the calling class.
bless ($pet, $class);
# We have now created an object. The only effect of turning
# a reference into an object is that we can now call methods on
# it, as in "$pet_object->speak;"
print "Created a $class named $name.\n";
# Return the object we just created.
return $pet;
}
# Pets make noises
sub speak () {
# When we say $pet->speak, $pet is the first argument passed to speak().
# In this case, we don't need any other arguments, since all the information
# we need is stored in $self.
my ($self) = @_;
# Since this is an abstract base class, this method is
# only called if not overridden by a derived class that
# actually knows what to do, so all we do is figure out what
# class we were called from and print a warning.
# the ref() function returns the type of reference. Since we
# have blessed our reference and made it an object, ref returns
# the name of the class it belongs to.
# We could put this in a local variable, or we could just use
# the results directly.
# Warn the user.
cluck "Don't know what noise a ", ref ($self), " named $self->{'name'} should make"
}
# Pets like food, but they are picky eaters.
sub feed ($) {
# When we say $pet->feed(...), $pet is the first argument passed to feed().
# If this were a derived class, we would care what kind of food
# it was being offered, but in this case, we ignore the type of food
# because we're just printing a warning.
my ($self) = @_;
cluck "Don't know about the dietary habits of a ", ref ($self), " named $self->{'name'}"
}
# Perl always returns a value from any subroutine call, as it is built
# as an expression-oriented (applicative, function, remember?) language.
# Therefore, the last statement in a package must be a true value to
# indicate it loaded correctly.
1;
|
|
|
|
11-15-2006, 12:05 AM
|
#5 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
If you translated the derived classes can't you just take the three methods from one of the derived classes and declare them virtual?
I don't think you need to do anything like the ocntortions perl puts itself throught o generate and bless a hash to be the object, just code the object/class the way you would a normal C++ class. For the abstract class it seems you could just work from the comments since the majority of the "code" present is just error reporting and book keeping code.
__________________
Stop intellectual property from infringing on me
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
|
Kaat a talking bot in c
|
nvictor |
Platform/API C++ |
10 |
05-19-2005 01:16 PM |
|
PERL to C#
|
Draqos |
All Other Coding Languages |
2 |
08-20-2004 04:45 AM |
|
edit?
|
anon |
Lounge |
10 |
11-21-2002 03:02 PM |
All times are GMT -8. The time now is 02:01 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|