What good is -CSDA specified only on the shebang line?
I am looking for someone to authoritatively confirm or correct what I think I know about the -CSDA
option on the shebang line of a Perl script.
See perldoc perlrun for the documentation of -CSDA
. Briefly
- S:
STDIN
,STDOUT
andSTDERR
are assumed to be in UTF-8- D:开发者_高级运维 UTF-8 is the default PerlIO layer for both input and output streams
- A: @ARGV elements are expected to be strings encoded in UTF-8
For
-CSDA
to have any effect, it must be specified on the command line as inperl -CSDA script.pl
.Prior to 5.10,
-CSDA
on the shebang line would silently fail because the standard streams would have already been opened and@ARGV
already populated by the time it was encountered unless-CSDA
was already specified on the command line as well.After 5.10,
-CSDA
which appears only on the shebang line causesperl
to croak because of that problem.A script with
-CSDA
that used to work withperl
s pre-5.10 should have the-CSDA
removed from the shebang line because it was never invoked with those options on the command line (and the options, if specified solely on the shebang line, did nothing).
I would love to get some solid feedback on which of my assumptions above are wrong.
Not sure how authoritative I am, but I know how this works.
- Your first assumption is almost accurate. For the SDA options to have any effect, they must be present when the interpreter is started. That could be due to -CSDA on the command line, or it could be due to the PERL_UNICODE environment variable, or possibly some other method I am unaware of.
- Your second assumption is correct, at least for 5.8.8. Note that the D flag would still have had its normal effect for streams opened by the script.
- Your third assumption is correct. However, starting with 5.10.1 it will not croak if the appropriate flags are enabled via the PERL_UNICODE environment variable or some other mechanism.
- Your fourth assumption is not generally correct. I guess you are referring the situation when the script is invoked directly, rather than invoking the perl interpreter with the script as an argument. There are two general cases.
- On a system where the operating system determines that any file with the extension ".pl" will be passed to the perl interpreter for execution, like Windows, you may be correct. But it could be argued that the script croaking when invoked without -CSDA is the desired behavior, rather than things mysteriously failing because the standard input and @ARGV are not UTF-8 as the script expects.
- On a system that reads the shebang line when a script is directly executed, like most *nix shells, the command line options specified in the shebang line will be used when invoking the interpreter and therefore the -CSDA on the shebang line will be honored.
If your script is
#!/usr/bin/perl -CSDA
and you start your script using
./script foo
The OS will launch Perl as follows:
/usr/bin/perl -CSDA ./script foo
The change in behaviour only comes into play if launch the script incorrectly, such as by using
/usr/bin/perl ./script foo
The fix isn't to remove -CSDA
, the fix is to call the script correctly.
精彩评论