Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

convert c-code into perl-code

by koppi2342 (Initiate)
on Dec 29, 2012 at 11:38 UTC ( #1010810=perlquestion: print w/replies, xml ) Need Help??
koppi2342 has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks, can you help me, converting the following C-Code into Perl-Code?
#include <stdio.h> int main(int argc, char **argv) { if (argc == 1) { printf("usage: cm3_checksum \n"); return 1; } FILE *file; if ((file = fopen(argv[1], "r+")) == NULL) { return 1; } /* The checksum of the exception vector */ unsigned int i, n, checksum=0; for (i=0; i<7; i++) { fread(&n, 4, 1, file); checksum += n; } /* The 2's complement of the checksum */ checksum = -checksum; printf("checksum: 0x%X\n", checksum); /* write back the checksum to location 7 */ fwrite(&checksum, 4, 1, file); fclose(file); return 0; }
Kind regards, Koppi

Replies are listed 'Best First'.
Re: convert c-code into perl-code
by ww (Bishop) on Dec 29, 2012 at 12:44 UTC
    Why convert? (homework?)
    What have you tried? (see On asking for help: we prefer to teach; not to play the role of code-a-matic)
    Have you done anything on your own? (if not, see Tutorials, starting at "Getting Started with Perl")

    We tend to be most responsive to those who try to help themselves

      An XP whore is going to do the HW in 3... 2...
Re: convert c-code into perl-code
by Old_Gray_Bear (Bishop) on Dec 29, 2012 at 19:52 UTC
    Insert the following statements at the top of your C code:
    #! /usr/local/bin/perl use strict; use warnings;
    Add +x to the file permissions.

    Execute the file, correct all of the warnings and errors, and 'Hey Presto' you have Perl code. (Now you will need to read up on the differences between the f-subroutines and their real counterparts, but I have to leave something as an exercise for the Student.)

    I Go Back to Sleep, Now.


      Excellent, but wrong section!

      ... this gem belongs to Tutorials! ;-)

      Cheers Rolf

      PS: not really a joke...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1010810]
Approved by Corion
[erix]: in this context, one learns all kinds of interesting psychological excuse-jargon: not just 'alternative facts'; how about 'gapped knowledge' ? :)
[erix]: which still manages to sound more or less positive
[Corion]: Huh. I wasn't aware that you cannot segregate Python 2 and Python 3 modules... Hopefully Perl 6 module use C6AN and thus avoid the problem...
[hippo]: I very much hope so.
[moritz]: there are already some (very few) Perl 6 modules on CPAN, but in a way that the p5 indexer avoids them
[hippo]: It's confusing enough to have the Perl6:: namespace which isn't for Perl6 but for features from that ported into Perl.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2017-01-24 09:19 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (203 votes). Check out past polls.