Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Perl / March 2008



Tip: Looking for answers? Try searching our database.

How to run Perl script at Mac OS (Darwin) Release?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gary Yang - 07 Mar 2008 18:35 GMT
Hi,
 
 Below is my Perl script. The script named, test1.pl
 
 test1.pl
 
 #!/usr/local/ActivePerl-5.10/bin/perl -w
 
 print "$^O\n";
 
 
 I have to type, "perl test1.pl" in order to run it. I got command not found if I simply typed test1.pl. Can someone tell me why and how to fix it?
 
 test1.pl
 -bash: test1.pl: command not found
 
 
 Thanks
 
 
 Gary
 

     
---------------------------------
Never miss a thing.   Make Yahoo your homepage.
Chas. Owens - 07 Mar 2008 18:47 GMT
> Hi,
>   Below is my Perl script. The script named, test1.pl
[quoted text clipped - 8 lines]
>   test1.pl
>   -bash: test1.pl: command not found
snip

First, you  must make sure it has  been marked as executable:

chmod a+x test1.pl

then you must either make sure it is in a directory in your PATH or
run it with either a relative or absolute name:

./test1.pl

/home/username/test1.pl
Signature

Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

Larry Prall - 07 Mar 2008 18:48 GMT
Change the she-bang (#!) line to read:

#!/usr/bin/perl -w

That's the location of the default perl installation on OS X.

- Larry

> Hi,
>
[quoted text clipped - 19 lines]
> ---------------------------------
> Never miss a thing.   Make Yahoo your homepage.
jeremiah@jeremiahfoster.com - 09 Mar 2008 12:31 GMT
> Change the she-bang (#!) line to read:
>
> #!/usr/bin/perl -w
>
> That's the location of the default perl installation on OS X.

That _may_ be the problem, but it is not necessarily the problem. I  
believe that if there was no perl interpreter in the path that the OP  
specified, bash would say -

"bash: ./test1i.pl: #!/usr/local/ActivePerl-5.10/bin/perl: bad  
interpreter: No such file or directory"

But instead bash is saying "Command not found." So the OP may in fact  
have a perl interpreter in the path specified on the command line, but  
he is not calling the script correctly. So advising the OP to change  
the shebang may be premature.

The script was not called correctly from the command line, of that we  
can be certain.

    Jeremiah

>> Hi,
>>
[quoted text clipped - 19 lines]
>> ---------------------------------
>> Never miss a thing.   Make Yahoo your homepage.
Igor Sutton - 09 Mar 2008 13:17 GMT
>> Change the she-bang (#!) line to read:
>>
[quoted text clipped - 16 lines]
> The script was not called correctly from the command line, of that  
> we can be certain.

I think he's missing the execution bit (where someone already  
spotted) and since bash on MacOS X doesn't have the current directory  
in PATH, one must execute the program like:

$ ./test1.pl

Instead of

$ test1.pl

In the same directory, or change the PATH to use the current working  
directory

$ PATH=$PATH:. test1.pl

Hope this helps
Doug McNutt - 09 Mar 2008 16:12 GMT
Hashing of tools by shells has not been mentioned. It may not be part of the problem but on invocation many, if not all, shells examine the $PATH variable and make a table of executables that it finds. The table is then converted to a hashed lookup array in order to improve speed of response. (That probably makes little sense today but it was started during the days of mag tape.)

So if you create a perl script, place it somewhere in $PATH, and set its execute permission a running shell will not find it. A full path, perhaps starting with the current directory " . " always works.

In tcsh the command to rework the hash of tools is "rehash". Restarting the shell with a new Terminal.app window will do pretty much the same thing.

Signature

Applescript syntax is like English spelling:
Roughly, though not thoroughly, thought through.

Packy Anderson - 11 Mar 2008 06:37 GMT
> Hashing of tools by shells has not been mentioned. It may not be  
> part of the problem but on invocation many, if not all, shells  
[quoted text clipped - 10 lines]
> Restarting the shell with a new Terminal.app window will do pretty  
> much the same thing.

It's not likely that Gary is having problems with his shell creating  
a hash table of executables on startup; he's using bash (the default  
in OS X these days), and bash doesn't do that:

>  test1.pl
>  -bash: test1.pl: command not found

If he had created test1.pl in a directory in his path (say, /Users/
gary/bin) but the executable bit wasn't set, he'd have gotten the error

-bash: /Users/gary/bin/test1.pl: Permission denied

even if the file was created after his bash shell started.  If he  
then had the executable bit set properly (chmod +x test1.pl) and  
didn't have a perl installed at /usr/local/ActivePerl-5.10/bin/perl,  
he'd have gotten the error

-bash: /Users/gary/bin/test1.pl: /usr/local/ActivePerl-5.10/bin/perl:  
bad interpreter: No such file or directory

Of course, maybe he's eschewing the default perl on OS X because he  
wants to use the 5.10 for some reason and he downloaded ActiveState's  
build for OS X; if that's the case, then that's probably the shebang  
line he wants.

Anyway, I'll shut up now, since people have just about beat this  
subject to death.  ;)

Here's hoping Gary's happily programming perl on his Mac now...

-packy

--
Packy Anderson                                              
packy@dardan.com

If I had a boat, I'd go out on the ocean;
And if I had a pony, I'd ride him on my boat.
We could both together go out on the ocean--
Me upon my pony on my boat.
jeremiah@jeremiahfoster.com - 08 Mar 2008 11:01 GMT
> Hi,
>
[quoted text clipped - 9 lines]
> not found if I simply typed test1.pl. Can someone tell me why and  
> how to fix it?

Indeed, as Chas. mentioned, the script you have written must be  
'executable'. There is a distinction between a script that is plain  
text, like yours, and a script that can 'execute' that plain text as  
if it were instructions to the computer. When you called your script,  
you did it like this: test1.pl But your computer did not understand  
that you wanted to execute all the commands in your script, it just  
saw plain text.

To tell your computer to execute your script, you have to change the  
permissions. That is, you have to give permission to execute the  
script. The way to do that is to use a command called 'chmod'.  chmod  
'CHanges file MODes' - it turns a plain script into an executable  
script. Do a `man chmod` to find out more, or just do what Chas.  
suggested:

chmod a+x test.pl

Now you can call your script and your computer will understand, "Aha!"  
it will say. "I am to execute each command in this file as if someone  
wrote it on the command line!" To call your script, do this:

./test.pl

See the dot and the slash before your script's name? That says,  
'execute this script right here.' Now you should see the output from  
your script.

    Jeremiah
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.