Making Perl script *run itself* with flags
Asked Answered
A

1

8

I'm very new to Perl, and have recently encountered the following problem. My Perl code must all reside within a single file (it's being called by another program I have no control of). I now wish to make the script run with a flag (e.g., perl myscript.pl -T for taint mode), although it is initially called without the flag. How can I do that, i.e., how do I "set the flag" from within the Perl file?

One idea that I've had is to make the file launch itself again (after being called without flags), this time with the -T flag (by issuing a shell command such as system($^X, "myscript.pl -T", @ARGS);). Would that work? Any better ideas?

Archaimbaud answered 2/7, 2015 at 17:8 Comment(6)
if you control myscript.pl you can set taint mode by altering your shebang line to include -T e.g. #!/usr/bin/env perl -TCasualty
What command is used to invoke the script in the other program? If it's something like /path/to/myscript.pl, you can simply add -T to the shebang line.Symploce
a -T in the shebang line doesn't necessarily enable taint mode -- if the script is invoked with perl from the shell without -T, you get a fatal errorMesquite
Still, putting -T in the shebang is the simplest solution. It'll either work or your script won't start, telling you to try a more complex workaround.Visible
@ThisSuitIsBlackNot, unfortunately I have no control over the program that invokes my script, otherwise it would've been too easy and not too interesting :)Archaimbaud
@Archaimbaud That's not what I said. I asked how the other program invokes your script. If it calls /path/to/myscript.pl from a shell, for example, you can add -T to the shebang line in myscript.pl to enable taint mode; if it calls perl /path/to/myscript.pl, for example, adding -T to the shebang line will cause an error as mob points out.Symploce
M
5

I think you're on the right track. You can make use of the $^{TAINT} variable, and exec instead of system:

exec($^X, $0, '-T', @ARGV) if $^{TAINT} < 1;

This won't work in all cases. For one thing, you'll lose some other command line switches that your script might initially be called with:

perl -I/path/to/some/more/libs -w -MNecessary::Module myscript.pl ...

There are some workarounds to some of these (use warnings or add -w switch to your exec statement above, analyze @INC and %INC to determine what -I and -M switches were added) but the more you can learn about exactly how your script is called, the more you can figure out what workarounds you need to worry about.


Depending on the ... group dynamics in play here, another approach is to exercise some indirect control of the calling program. Go ahead and put -T on the shebang line as @Hunter McMillen and @ThisSuitIsBlackNot suggest, wait for a bug report, and then diagnose the problem as "program not invoked with -T switch" ...

Mesquite answered 2/7, 2015 at 17:27 Comment(1)
Unfortunately, this doesn't seem to work for me, most likely due to the issues you've mentioned. Putting the -T on the shebang line produces the following error: "-T" is on the #! line, it must also be used on the command lineArchaimbaud

© 2022 - 2024 — McMap. All rights reserved.