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

Just stumbled on this website and not sure if this question to easy for something like this but I am totally new to Perl and am struggling just to get hello world up and running. It's because of this pesky line at the beginning "#!usr/bin/perl". I am running on windows 7 and when I take this line out the command prompt spits out hello world as it should. I know that windows doesn't technically use this line but How come when I leave it in and run hello.pl the terminal just goes to the next input with no error or output what so ever.

Replies are listed 'Best First'.
Re: Issue with #!usr/bin/perl
by kcott (Archbishop) on Mar 13, 2013 at 06:05 UTC

    That "pesky line" is called a shebang line and you've almost certainly mistyped it: I expect it should be "#!/usr/bin/perl".

    I can't see how you're "struggling just to get hello world up and running": you say your code works with it and without it - what problem are you struggling with?

    You're no doubt running some perl.exe program (from ActivePerl, Strawberry Perl, or wherever) with hello.pl as an argument - you're possibly achieving that by clicking on an icon. I expect perl.exe is treating the shebang line as a comment because it starts with a "#". Someone with more in-depth knowledge might be able to provide a better (possibly different) answer but, at the end of the day, you can safely ignore it.

    I used to run Perl programs from a WinXP box where I had Strawberry Perl and Cygwin. I would leave the shebang line pointing to whatever Perl I had on Cygwin: this allowed me to run the same script without changes from both a Cygwin command line and the WinXP GUI.

    I don't have Windows 7 but the following may help explain what's going on. Perhaps you can recreate this session on your system.

    1. Create a Hello world script with a bogus shebang line:
      $ cat > fred.pl #!/usr/bin/not_perl print "Hello, world!\n";
    2. Check that there really isn't a not_perl program:
      $ ls -l /usr/bin/not_perl ls: /usr/bin/not_perl: No such file or directory
    3. Make fred.pl executable:
      $ chmod +x fred.pl
    4. Execute it:
      $ fred.pl -bash: ./fred.pl: /usr/bin/not_perl: bad interpreter: No such file or +directory
    5. Now use a real perl program to run fred.pl (noting that the shebang line is ignored and there's no error messages):
      $ perl fred.pl Hello, world!

    There's also some documentation about simulating shebang lines which may be of interest. See perlrun: #! and quoting on non-Unix systems.

    -- Ken

Re: Issue with #!usr/bin/perl
by dasgar (Priest) on Mar 13, 2013 at 06:16 UTC

    Most of the time, the shebang line is ignored on Windows. However, if you have Cygwin or something similar installed, those environments will read and act upon it. My only guess is that you're trying to use something like Cygwin's Perl and that you have a typo in your shebang line. I think "#!usr/bin/perl" should be "#!/usr/bin/perl" like in kcott's post. Basically, you're missing the first forward slash before usr.

Re: Issue with #!usr/bin/perl
by nagalenoj (Friar) on Mar 13, 2013 at 05:23 UTC

    Hi,

    Yes, you're right. It is not useful in windows machine. This line instructs the operating system the place which the interpreter is available.

    Shabang is optional. Not mandatory to be given.

    In windows machine, this line is skipped as like any other comment line.

      Actually on Windows, the shebang line is not ignored. The Operating System ignores it, but Perl pays attention. Compare:

      #!/usr/bin/perl print undef;

      with:

      #!/usr/bin/perl -w print undef;

      The former issues no warning; the latter warns about using an uninitialized value.

      This is because in Perl, the shebang line performs two roles. It's used by Unix-like operating systems as a "dispatch mechanism" - to decide which program should interpret the script. And on all operating systems Perl parses it looking for command-line options. It doesn't honour all command line options found there (e.g. -M) but does some (like -w and -t).

      package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
        There's also this useful trick for writing perl scripts that will run using whatever is the first perl in the $PATH:
        #!/bin/sh exec perl -x $0 #!perl # your perl code goes here

        "But why not use #!/usr/bin/env perl?", I hear you cry.

        Because env isn't always in /usr/bin - it's in /bin on some obscure platforms - and there can be "issues" with passing arguments to the executable via env.

        Thanks for the clarification. As I haven't used windows machine for perl scripting, never encounter such and I didn't know this before.

        Thanks.

        what's the difference between shebang -w vs. use strict; use warnings; ? Thinking of putting a shebang at the top of all my scripts that say #!/perl/shebang - be a win32 japh you know this is pointless, even with the -w.
Re: Issue with #!usr/bin/perl
by aitap (Curate) on Mar 13, 2013 at 14:08 UTC
    In addition to the comments above, Apache web server on Windows respects sha-bang, too, and refuses to run Perl CGI scripts if this line containg wrong path to perl.exe.
    Sorry if my advice was wrong.
Re: Issue with #!usr/bin/perl
by hashperl (Initiate) on Mar 13, 2013 at 08:54 UTC

    Using Strawberry Perl for windows I use:-

    #!C:\strawberry\perl\bin\perl.exe