Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How to put '#!/usr/bin/env perl -w' at the beginning of a perlscript?

Reply
Thread Tools

How to put '#!/usr/bin/env perl -w' at the beginning of a perlscript?

 
 
Peng Yu
Guest
Posts: n/a
 
      12-29-2009
Since my perl is installed in a nonstandard location, I have to use '/
usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
to do it.

Currently, I have the following error.

$ head -n 1 ./main.pl
#!/usr/bin/env perl -w
$ ./main.pl
/usr/bin/env: perl -w: No such file or directory
 
Reply With Quote
 
 
 
 
Ilya Zakharevich
Guest
Posts: n/a
 
      12-29-2009
On 2009-12-29, Peng Yu <(E-Mail Removed)> wrote:
> Since my perl is installed in a nonstandard location, I have to use '/
> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
> to do it.
>
> Currently, I have the following error.
>
> $ head -n 1 ./main.pl
> #!/usr/bin/env perl -w
> $ ./main.pl
> /usr/bin/env: perl -w: No such file or directory


Your (Unix) kernel cannot handle more than one argument. A known
limitation. You need to set the effect -w "by hand":

#!/usr/bin/env perl
BEGIN { $^W = 1 } # Can't use -w `with env perl'

Yours,
Ilya
 
Reply With Quote
 
 
 
 
Peter J. Holzer
Guest
Posts: n/a
 
      12-29-2009
On 2009-12-29 21:44, Peng Yu <(E-Mail Removed)> wrote:
> Since my perl is installed in a nonstandard location, I have to use '/
> usr/bin/env perl'.

[...]
> #!/usr/bin/env perl -w


No. You should use

#!/path/to/your/perl -w

instead.

> I also what to use it with '-w'. I'm wondering how
> to do it.
>
> Currently, I have the following error.
>
> $ head -n 1 ./main.pl
> #!/usr/bin/env perl -w
> $ ./main.pl
> /usr/bin/env: perl -w: No such file or directory


This is because the syntax for the shebang line is extremely restrictive
on most unixes. In this case (Linux?) everything after the first space
is passed to /usr/bin/env as a single argument. There may be a
workaround but The Right Thing(TM) is to use the correct path to perl in
the first place.

hp

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      12-29-2009
>>>>> "Peng" == Peng Yu <(E-Mail Removed)> writes:

Peng> Since my perl is installed in a nonstandard location, I have to use '/
Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
Peng> to do it.

Don't use "-w". Add "use warnings;" early in your script.

Also, /usr/bin/env perl is a hack. You should replace it with your
specific perl path.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
 
Reply With Quote
 
Peng Yu
Guest
Posts: n/a
 
      12-29-2009
On Dec 29, 4:17*pm, (E-Mail Removed) (Randal L. Schwartz) wrote:
> >>>>> "Peng" == Peng Yu <(E-Mail Removed)> writes:

>
> Peng> Since my perl is installed in a nonstandard location, I have to use'/
> Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
> Peng> to do it.
>
> Don't use "-w". *Add "use warnings;" early in your script.
>
> Also, /usr/bin/env perl is a hack. *You should replace it with your
> specific perl path.


If I use my specific perl path, it will not be portable, right? After
all, if it moves to a different machine, the path has to be fixed.
 
Reply With Quote
 
l v
Guest
Posts: n/a
 
      12-29-2009
On 12/29/2009 4:25 PM, Peng Yu wrote:
> On Dec 29, 4:17 pm, (E-Mail Removed) (Randal L. Schwartz) wrote:
>>>>>>> "Peng" == Peng Yu<(E-Mail Removed)> writes:

>>
>> Peng> Since my perl is installed in a nonstandard location, I have to use '/
>> Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
>> Peng> to do it.
>>
>> Don't use "-w". Add "use warnings;" early in your script.
>>
>> Also, /usr/bin/env perl is a hack. You should replace it with your
>> specific perl path.

>
> If I use my specific perl path, it will not be portable, right? After
> all, if it moves to a different machine, the path has to be fixed.


Create a symbolic link for /usr/bin/perl to your non-standard location
and you should then be able to use the correct shebang in your Perl scripts.

--
Len


 
Reply With Quote
 
Kevin Collins
Guest
Posts: n/a
 
      12-29-2009
On 2009-12-29, l v <(E-Mail Removed)> wrote:
> On 12/29/2009 4:25 PM, Peng Yu wrote:
>> On Dec 29, 4:17 pm, (E-Mail Removed) (Randal L. Schwartz) wrote:
>>>>>>>> "Peng" == Peng Yu<(E-Mail Removed)> writes:
>>>
>>> Peng> Since my perl is installed in a nonstandard location, I have to use '/
>>> Peng> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
>>> Peng> to do it.
>>>
>>> Don't use "-w". Add "use warnings;" early in your script.
>>>
>>> Also, /usr/bin/env perl is a hack. You should replace it with your
>>> specific perl path.

>>
>> If I use my specific perl path, it will not be portable, right? After
>> all, if it moves to a different machine, the path has to be fixed.

>
> Create a symbolic link for /usr/bin/perl to your non-standard location
> and you should then be able to use the correct shebang in your Perl scripts.


This assumes you have rights to do that, which is often not the case...

In a similar situation with one of my clients, where I have access only as a
"user" not an "admin" I wrote a ksh wrapper script (perlwrap) that does various
techniques to identify the "correct" perl (based on OS version and
architecture), and then:

exec /path/to/perl "$@"

Then, in my scripts I have:

#!/mypath/perlwrap -w

Of course, that assumes you can store the wrapper script in a fixed path...

Kevin

 
Reply With Quote
 
Wanna-Be Sys Admin
Guest
Posts: n/a
 
      12-30-2009
Peng Yu wrote:

> Since my perl is installed in a nonstandard location, I have to use '/
> usr/bin/env perl'. I also what to use it with '-w'. I'm wondering how
> to do it.
>
> Currently, I have the following error.
>
> $ head -n 1 ./main.pl
> #!/usr/bin/env perl -w
> $ ./main.pl
> /usr/bin/env: perl -w: No such file or directory


just use "use warnings;" which offers some advantages. Keep the env the
same. Problem solved.
--
Not really a wanna-be, but I don't know everything.
 
Reply With Quote
 
Wanna-Be Sys Admin
Guest
Posts: n/a
 
      12-30-2009
Kevin Collins wrote:

>> Create a symbolic link for /usr/bin/perl to your non-standard
>> location and you should then be able to use the correct shebang in
>> your Perl scripts.

>
> This assumes you have rights to do that, which is often not the
> case...
>


The Op's first post shows they have shell access, and I find that
assumption curious, as I've never seen a system prevent a user from
creating symbolic links (even if it did, it would be unlikely a system
could prevent the user from executing the ln binary they could upload
themselves, even if tehy didn't have access to the compilers). But, I
suppose it's possible. I just think they should call use warnings; at
the top and not have to mess with anything else, though I never, ever
use env in a script myself.
--
Not really a wanna-be, but I don't know everything.
 
Reply With Quote
 
Dirk Heinrichs
Guest
Posts: n/a
 
      12-30-2009
Ben Morrow wrote:

>
> Quoth Tad McClellan <(E-Mail Removed)>:
>> Wanna-Be Sys Admin <(E-Mail Removed)> wrote:
>> > Peng Yu wrote:
>> >
>> >> Since my perl is installed in a nonstandard location, I have to use '/

>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

> Please re-read the solution offered...


The offered solution was simply wrong, so what Tad wrote is perfectly valid.
The right solution is to add the "nonstandard" location to $PATH, so that
env can find perl there.

The problem has nothing to do with "-w" at all!!!

Bye...

Dirk
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
index of string from beginning of line vs beginning of file Jesse B. Ruby 9 03-27-2010 04:04 PM
why does the following with Queue, q.put('\x02', True) not put itin the queue? Gabriel Rossetti Python 3 04-25-2008 03:41 PM
replacing text at the beginning of a file in perl and appending to that output aixenv@yahoo.com Perl Misc 3 07-20-2005 06:17 PM
Beginning Perl - Simon Cozens code download nospam@nospam.net Perl Misc 4 01-29-2004 07:46 PM



Advertisments