http://www.perlmonks.org?node_id=820343

hexcoder has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks,

while preparing a test case for the perl debugger of perl 5.11.3, i tried to use the '-d' switch in the shebang line of my testscript. I did that in order to make the script selfcontained.

perlrun suggests there is no difference in switches given in the commandline and the shebang line.

However when i started the script without the commandline switch '-d', the initialization for the debugger was incomplete. All line information seemed to be lacking.

Here is the script, which uses some debugger type ahead commands to make it self contained.

#!./perl -wd use strict; use warnings; BEGIN { push @DB::typeahead, 'b 8', 'L b', 'c', 'q'; $DB::single = 0; } print "Hello\n";
[Update: I exchanged the originally wrong script with the right one, sorry.]

Here is a session without the -d commandline option. No line information is available. I checked this also interactively with the 'l' debugger command. There is no output. The %DB::dbline hash has not been set up.

heiko@heiko-desktop:~/perl/my_tests$ ~/perl5.11.3 option_d_bug.pl Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(option_d_bug.pl:8): auto(-4) DB<1> b 8 Line 8 not breakable. auto(-3) DB<2> L b auto(-2) DB<3> c Hello Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. auto(-1) DB<3> q
Here is the same session with a -d commandline parameter:
heiko@heiko-desktop:~/perl/my_tests$ ~/perl5.11.3 -d option_d_bug.pl Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(option_d_bug.pl:8): print "Hello\n"; auto(-4) DB<1> b 8 auto(-3) DB<2> L b option_d_bug.pl: 8: print "Hello\n"; break if (1) auto(-2) DB<3> c Hello Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. auto(-1) DB<3> q heiko@heiko-desktop:~/perl/my_tests$
From my point of view switch processing should be the same in both cases.

Ok, now for the question: Can some kind monk point me to the location in the Perl source, where shebang option processing is taking place? I try to find a fix then.

Thanks a lot, hexcoder

  • Comment on Should options in the shebang line generally behave like switches given in the commandline?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Should options in the shebang line generally behave like switches given in the commandline?
by JavaFan (Canon) on Jan 29, 2010 at 11:40 UTC
    From my point of view switch processing should be the same in both cases.
    There are subtle differences between switches given on the command line, and given on the she-bang line. Switches given on the command line are passed to the perl process when the process starts, using the normal OS way of passing arguments using one of the exec calls. They can be processed beforing doing anything else. Not so for switches on the she-bang line - then perl has to already open a stream for instance. This means for instance that you cannot give a -C on the she-bang line, unless you give it on the command line as well. And there are also subtle differences between perl foo and ./foo when it comes to switches (Too late for "-T" option for instance).

    As for the -d switch, I don't know enough of the debugger to be of any help there.

      This means for instance that you cannot give a -C on the she-bang line

      There's no reason for -C not to work from the shebang line. It's a known bug noone's gotten around to fixing yet.

Re: Should options in the shebang line generally behave like switches given in the commandline?
by cdarke (Prior) on Jan 29, 2010 at 13:29 UTC
    Interestingly perldebug, under the heading Calling the debugger, lists four valid ways to call the debugger - and none of them use the #! line. So I guess that using #! to invoke the -d option is not valid..

    To be fair though, perlrun says: you still can get consistent switch behavior regardless of how Perl was invoked, so we have some inconsistency in the doc. Although a lawyer might argue that the switch behavior is consistent - it is the underlying effect of the switch that is not.
Re: Should options in the shebang line generally behave like switches given in the commandline?
by rubasov (Friar) on Jan 29, 2010 at 17:02 UTC
    Just a little advice regarding the portability of shebang lines:

    Some (mainly ancient, but somewhere still living) Unices imposed serious limitations on the length of the shebang line (in characters) or on the processed number of the options (or option words). So it is considered good practice not to use relative paths, long commands and options/switches in the shebang line. IIRC I also read about cases where the amount of white space was relevant between the #! and the path. So my recommendation is to stick with this:

    #! /usr/bin/perl
    or this:
    #! /usr/bin/env perl

    If you move your options to in-code (eg. use warnings instead of perl -w) it also enhances readability.

Re: Should options in the shebang line generally behave like switches given in the commandline?
by hexcoder (Curate) on Jan 30, 2010 at 11:07 UTC
    Thanks for all your helpful feedback.

    I like orthogonal design, so I try to fix it (maybe with the help of perl.perl5.porters). I cannot see any harm in that.

    Greetings, hexcoder

Re: Should options in the shebang line generally behave like switches given in the commandline?
by Khen1950fx (Canon) on Jan 30, 2010 at 22:47 UTC
    Strange, but when I tried your script, it went into an infinte loop and started creating a bunch of "Daughter Perl debugger" sessions. If you take out the BEGIN block and just do #!./perl -d on the shebang line, the debugger works as expected:
    #!/usr/bin/perl -d use strict; use warnings; print "Hello\n";