Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 and STDERR 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 in perl -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 causes perl to croak because of that problem.

  • A script with -CSDA that used to work with perls 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.

like image 598
Sinan Ünür Avatar asked Mar 18 '11 14:03

Sinan Ünür


People also ask

What is the purpose of the shebang line?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

What is a valid shebang line?

Rules for the shebang The shebang command must be the first line of the file and can contain any valid path for the interpreter, followed by an argument that the command will receive. The shebang line is read by the system before the execution of the program, but that line will not be automatically deleted.01-Dec-2014.

What is a valid shebang line in Python?

#!/usr/bin/env python """ The first line in this file is the "shebang" line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the "bang". The # is not called the "she", so sometimes the "shebang" line is also called the "hashbang".

Does #! Have to be on the first line?

The shebang must be the first line because it is interpreted by the kernel, which looks at the two bytes at the start of an executable file. If these are #! the rest of the line is interpreted as the executable to run and with the script file available to that program.


2 Answers

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.
like image 114
Anomie Avatar answered Oct 13 '22 10:10

Anomie


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.

like image 33
ikegami Avatar answered Oct 13 '22 11:10

ikegami