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

Hi all,
I am new to perl area. I have couple of doubts, just tell if i am wrong.

1. The difference ./abc.cgi and perl abc.cgi
2. Is it possible to run a perl program without #!/usr/bin/perl?
My answer to 2nd qn is
without giving interpreter path, the script will run. I tried this in my system. But i read in some book that it will throw error.
what is the correct answer?
For qn number 1, both will execute. But what's the proper answer?
Thank you,
Stef.

20050208 Edit by castaway: Changed title from 'Please clarify my doubt'

Replies are listed 'Best First'.
Re: On running perl scripts
by Random_Walk (Prior) on Feb 07, 2005 at 12:38 UTC

    The #!/usr/bin/perl (often called the shebang line) tells a *NIX shell to load the given interpreter when it tries to execute a file.

    The #! part is the ascii representation of a 'magic number' that the unix shell uses to determine file type. This one tells the shell to load up the given interpreter. If you leave this out the file will not exececute with ./filename. If you run perl and give it a filename perl will try to compile and run the contents of the file. To do this it does not need the shebang line and will skip it if it is there, the file will run just fine (assuming it is valid perl code)

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!

      The #!/usr/bin/perl (often called the shebang line) tells a *NIX shell to load the given interpreter when it tries to execute a file.

      To be pedantic it is not the shell that is doing this - it is the execve(3) system function, which should be available in any POSIX compliant system.

      /J\

        Pedantic is apropriate here.

        So when you flag flag a file executable the shell just chucks it to execve(3) without bothering to look inside. execve(3) then does the magic number, er, magic.

        I do love this Monastery, you really do learn a new thing every day here

        Thanks,
        R.

        Pereant, qui ante nos nostra dixerunt!
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: On running perl scripts
by samizdat (Vicar) on Feb 07, 2005 at 13:19 UTC
    The answer to qn #1 is also dependent upon your Apache setup. Certain directories can be enabled to activate files ending in '.cgi' as executable, for example, the 'cgi-bin' directory is traditionally set up this way.

    In the normal case, your script must have its execute permission bit set for you to activate it using the #! line as ./abc.cgi, whereas saying perl abc.cgi bypasses this necessity. In the first case, the UN!X shell is processing the file; in the second, the perl interpreter is processing it.
Re: On running perl scripts
by Anonymous Monk on Feb 07, 2005 at 13:35 UTC
    1. perl abc.cgi will execute abc.cgi using which ever perl is found in first in your PATH. If there's no perl in your PATH, it will not be executed. If the first line is a she-bang line, and the command listed there doesn't containt the word 'perl', perl will feed abc.cgi to that command.
      ./abc.cgi will run abc.cgi. If the file is a binary, it will be run as a binary. If it starts with a she-bang line, the kernel will feed abc.cgi to whatever program is listed on the she-bang line. Else, it will be executed by the same shell you're running in.
      So, without knowing more details, we don't know whether there's a difference, and if there's a difference, what the difference is.
    2. Yes. A couple of ways:
      1. Using perl program.
      2. By putting #!/usr/local/bin/perl as the first line, and having a /usr/local/bin/perl.
      3. By putting
        #!/bin/sh eval 'exec perl -wS $0 ${1+"$@"}' if $running_under_some_shell;
        as the first lines of your shell.
      4. By doing a fork and exec, and execing perl.
Re: On running perl scripts
by blazar (Canon) on Feb 07, 2005 at 15:04 UTC
    I am new to perl area. I have couple of doubts, just tell if i am wrong.

    1. The difference ./abc.cgi and perl abc.cgi

    (As a side note) s/(?<=difference )/between/

    Suffice to say that the difference between those two cmds is much less than that between Perl and CGI.

    'Perl' ne 'CGI'

    2. Is it possible to run a perl program without #!/usr/bin/perl?
    Yes, it is. And your perl question is?

    Please don't take my word as I'm having an attitude in your regards. Despite what you may think at first sight, please take them as constructive comment!