I don't think there's a pure Perl solution... However, you could try to intercept the underlying execvp library function call (at least on unix-ish systems — I think Windows uses spawn). Then, in your wrapper function you could decide what to do, e.g. return fake results, or just pass through to the real function...  I know this is highly platform dependent, ugly and probably otherwise tricky in a number of ways... but anyhow, these would be the basic steps — just to illustrate the idea... (quite a number of issues remain to be solved!)

Demo perl program

#!/usr/bin/perl system "/bin/echo", "hello"; # or # system "/bin/echo hello"; # or # my $out = `/bin/echo hello`;

This is the sample wrapper library, which does nothing except printing a message to stderr, and then forwarding the call to the original library function:

/* fakesys.c */ #include <unistd.h> /* for execvp */ #include <dlfcn.h> /* for dlsym */ #include <stdio.h> static int (*real_execvp)(const char *file, char *const argv[]); int execvp(const char *file, char *const argv[]) { char *msg; /* lookup original shared lib function */ real_execvp = dlsym(RTLD_NEXT, "execvp"); /* see dlsym(3) */ if ( (msg = dlerror() ) != NULL ) { fprintf(stderr, "dlopen of execvp() failed : %s\n", msg); } fprintf(stderr, "calling execvp(\"%s\", ...)\n", file); /* execute original lib function */ return real_execvp(file, argv); }

Compile a shared lib (on Linux):

$ gcc -D_GNU_SOURCE -fPIC -shared fakesys.c -o libfakesys.so

and call the program with the intercepted execvp call, using the LD_PRELOAD mechanism (—> "man ld-linux.so"):

$ LD_PRELOAD=./libfakesys.so perl ./685699.pl

Output:

calling execvp("/bin/echo", ...) hello

You get the idea... :)


In reply to Re: mocking or trapping system calls by almut
in thread mocking or trapping system calls by wu-lee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.