Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Execute a Perl code without ".pl" extension

by Anonymous Monk
on Mar 10, 2006 at 01:20 UTC ( #535583=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Usually I would execute my Perl code like the following:
$ perl param > someoutput.txt
How can I make that code executable like this:
$ ./myperlcode param > someoutput.txt

Replies are listed 'Best First'.
Re: Execute a Perl code without ".pl" extension
by jZed (Prior) on Mar 10, 2006 at 01:25 UTC
    In order to answer your question, we'd have to know what shell and OS you are using. If you are using bash or a related *nix shell, then the "shebang" line of your code (the first line of the myperlcode script) should have a full path to your perl interpreter and the myperlcode file should be executable. If you're on some other OS or some other shell, that may not be sufficient.
Re: Execute a Perl code without ".pl" extension
by moklevat (Priest) on Mar 10, 2006 at 01:26 UTC
    Assuming you are working on *nix:

    1) You include the shebang #!/usr/bin/perl -w at the top of your script

    Update:As Moriarty points out below, your shebang should point to your perl binary, which may be /usr/bin/perl, or may be /usr/local/bin/perl, or may be somewhere else, but can be found with the command which perl

    2) Rename your script from to myperlcode

    3) Change permissions on your script like: chmod +x myperlcode

      This isn't completely true

      1) This will only work if the perl executable is in /usr/bin/ (which just happens to be the most popular place to put it).

      2) It is not necessary to rename the script as *nix doesn't care about extensions.

        Fair enough, but even though *nix doesn't care what the file is named, the OP wanted to know how to do

        $ ./myperlcode param > someoutput.txt

        and the original script was named

Re: Execute a Perl code without ".pl" extension
by holli (Monsignor) on Mar 10, 2006 at 20:33 UTC
    For Windows you'll need to add ";.pl" to the PATHEXT environment variable. (given that the .pl extension is associated with the perl executable, which is always the case with a normal Active Perl installation.)

    holli, /regexed monk/
      Thanks holli, for that information!

      When I saw the title of this node, my first thought was that it would answer a question I, too, have been having, ever since installing Perl on this new laptop.  It seemed natural to assume the question was related to running on Windows ... but I had to read down to the last of the messages before I found the only one which applied to my situation ... yours.  Danke sehr ;)

      @ARGV=split//,"/:L"; map{print substr crypt($_,ord pop),2,3}qw"PerlyouC READPIPE provides"
Re: Execute a Perl code without ".pl" extension
by izut (Chaplain) on Mar 10, 2006 at 10:25 UTC

    I always put the following

    #!/usr/bin/env perl

    instead other common shebang (#!) lines. It will call env(1) and then execute perl from environment (looking in /bin, /usr/bin, etc.).

    Update: As Anonymous Monk said, the correct canonical path is /usr/bin/env.

    Igor 'izut' Sutton
    your code, your rules.

      Be careful, it may bite you in production environment because the environment of a user isn't garanteed, and many systems have several perl executables installed... Especially, don't do that on a web environment, it may be a huge security risk.

        I work basically with Red Hat environments, just one Perl installed. I can't see the point using that in a web environment. Did you mean CGI or mod_perl? If it is mod_perl, does it cares as Apache uses the embedded interpreter? And about the CGI case, I think env will load the environment of user that is supposed to run the web server, or am I wrong?

        Igor 'izut' Sutton
        your code, your rules.

      sorry to disappoint you, but the canonical path to env is /usr/bin so please write
      #!/usr/bin/env perl
      And yes, that should work on any *nix system, according to somme standard which name I conveniently forgot (so you can't check me on this ;-), but (seriously!) remember to have read about.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://535583]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-05-27 14:26 GMT
Find Nodes?
    Voting Booth?