Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting a portable command line wrapper in C
    text
    copied!<p>I'm writing a perl module called <a href="http://github.com/schwern/perl5i" rel="noreferrer">perl5i</a>. Its aim is to fix a swath of common Perl problems in one module (using lots of other modules).</p> <p>To invoke it on the command line for one liners you'd write: <code>perl -Mperl5i -e 'say "Hello"'</code> I think that's too wordy so I'd like to supply a perl5i wrapper so you can write <code>perl5i -e 'say "Hello"'</code>. I'd also like people to be able to write scripts with <code>#!/usr/bin/perl5i</code> so it must be a compiled C program.</p> <p>I figured all I had to do was push "-Mperl5i" onto the front of the argument list and call perl. And that's what I tried.</p> <pre><code>#include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; /* * Meant to mimic the shell command * exec perl -Mperl5i "$@" * * This is a C program so it works in a #! line. */ int main (int argc, char* argv[]) { int i; /* This value is set by a program which generates this C file */ const char* perl_cmd = "/usr/local/perl/5.10.0/bin/perl"; char* perl_args[argc+1]; perl_args[0] = argv[0]; perl_args[1] = "-Mperl5i"; for( i = 1; i &lt;= argc; i++ ) { perl_args[i+1] = argv[i]; } return execv( perl_cmd, perl_args ); } </code></pre> <p>Windows complicates this approach. Apparently programs in Windows are not passed an array of arguments, they are passed all the arguments as a single string and then do their own parsing! Thus something like <code>perl5i -e "say 'Hello'"</code> becomes <code>perl -Mperl5i -e say 'Hello'</code> and Windows can't deal with the lack of quoting.</p> <p>So, how can I handle this? Wrap everything in quotes and escapes on Windows? Is there a library to handle this for me? Is there a better approach? Could I just not generate a C program on Windows and write it as a perl wrapper as it doesn't support #! anyway?</p> <p>UPDATE: Do be more clear, this is shipped software so solutions that require using a certain shell or tweaking the shell configuration (for example, <code>alias perl5i='perl -Mperl5i'</code>) aren't satisfactory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload