including a Perl script in a C executable

Discussion in 'Perl Misc' started by ccc31807, Jan 22, 2013.

  1. ccc31807

    ccc31807 Guest

    I have a small C program that displays file attributes, essentially runningthe stat command and emitting the output to the console.

    This morning, I have a new requirement -- to nicely format the output, printing the field names in CAPS and justifying the field values.

    C isn't my game, and while I probably could (with some effort) read up on reading in system output as strings, munging them, and displaying them, if it's possible I'd rather use Perl. The program must be self contained in oneexe file. The object is to save a manager from knowing anything about the stat command (yeah, I know.)

    Is this doable? Or must I write the entire thing in C?

    Thanks, CC.
    ccc31807, Jan 22, 2013
    1. Advertisements

  2. You could write a Perl script which generates a C source file from a
    perl script by creating an initialized array of char *s containg the
    lines of the Perl script, short mockup of that:

    print("static char const *script[] = {\n");

    while (<>) {


    If you want to keep things simple, you could then use popen to start
    the perl interpreter with input from a pipe and (assuming the FILE *
    was named perl_in) use a loop a la

    char *cur;

    cur = script;
    while (*cur) {
    fprintf(perl_in, "%s\n", *cur);

    to feed the 'compiled in' script to perl for execution.

    OTOH, why don't you use Perl to determine the the necessary output
    data with the help of stat/fstat (=> perldoc -f -f) and print the
    'nicely formatted' output in one go?
    Rainer Weikusat, Jan 22, 2013
    1. Advertisements

  3. [...]

    This needs to be char const **cur ...
    Rainer Weikusat, Jan 22, 2013
  4. perldoc -f sprintf

    Teach your manager to select in Windows Explorer (you did mention exe,
    therefore I conclude that you are on Windows) "View" -> "Details";
    right-click on the header bar where it says "Name, Date Modified, Type
    .... " (actual headers will vary), and select "More..." from the context
    menu. Then he can select to display whatever information his heart

    Jürgen Exner, Jan 22, 2013
  5. ccc31807

    C.DeRykus Guest

    IIUC (and I'm not at all sure I do), you could
    comment out the existing code in the .exe and
    hack up a replacement to shell out to a runnable
    perl script

    Of course, just adding some formatting to the
    original would probably be easier. But, if you
    want the former and, assuming .exe takes a file
    as its only argument, here's a quick sketch:

    #include <stdio.h>

    int main( int argc, char *argv[] ) {

    // put entire perl script in string
    char *perlsource = "#!/usr/bin/perl\n...";

    // output perl source to file
    FILE *fp;
    char *perlfile = "/tmp/"; // edit

    // add error checking to open/flose/system
    fp = fopen(perlfile, "w");
    fprintf(fp, "%s\n", perlsource);

    char cmd[100];
    sprintf(cmd, "perl %s %s", perlfile, argv[1] );
    C.DeRykus, Jan 23, 2013
  6. ccc31807

    ccc31807 Guest

    It's a 'manager problem' and you are exactly right. I can't cater to every whim, at least not when I've got work to do otherwise. He can get the information he wants, but not necessarily the format he wants, and we've worked it out.

    Besides, I just wondered if it could be done ... an idle thought that I'm now a little ashamed of posting. I write very little in C, I write a lot of Perl, and I suppose I can blame the question on a neuron misfire thinking that I should be able to invoke a Perl script from C the same way I can invoke an executable in Perl (by system(), e.g.).

    Thanks to all who replied, CC.
    ccc31807, Jan 23, 2013
  7. ccc31807

    J. Gleixner Guest

    C has system(), but that's a dumb way to go since there is printf
    and toupper in C, there's no need to involve Perl. The program is
    already using printf, so it should be a very simple change.

    Do it right. Spend 5-minutes learning now and avoid hours of
    frustration later.
    J. Gleixner, Jan 24, 2013
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.