Creating a Perl script * runs itself * with flags

I am very new to Perl and recently encountered the following problem. My Perl code must be in one file (it is called by another program in which I do not control). Now I want to make a script run with a flag (for example, perl myscript.pl -T for taint mode), although it is initially called without a flag. How can I do this, i.e. How do I "set a flag" from a Perl file?

One idea I got is to run the file again (after calling without flags), this time with the -T flag (by issuing a shell command, for example system($^X, "myscript.pl -T", @ARGS); ) . Will this work? Any better ideas?

+8
perl flags
source share
1 answer

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

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

This will not work in all cases. Firstly, you will lose some other command line switches, from which a script is usually called using:

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

There are some ways to work around some of them ( use warnings or add -w ) to your exec statement above, analyze @INC and %INC to determine which -I and -M keys were added), but the more you learn how your script, the more you can figure out which workarounds you need to worry about.


Depending on the dynamics of the group in the game, another approach here is to exercise some indirect control over the calling program. Go ahead and put -T on the shebang line, as @Hunter McMillen and @ThisSuitIsBlackNot suggest waiting for the error report and then diagnosing the problem as "not called by the program using the -T switch ..."

+5
source share

All Articles