PDA

View Full Version : Help with Perl Script on Mac OS X




BGTHEMOGUL
Nov 12, 2010, 08:34 PM
I'm completely new to Mac and just bought a Macbook Pro yesterday. I love this computer and everything about this operating system and platform in general. Here's the issue I'm having that I have done extensive research on and just don't understand the answers. Keep in mind I'm 110% new to macs!

To run a perl script on my PC computer before I downloaded ActiveState's ActivePerl program and installed it. Then I would just double click my .pl file I have saved to execute the script and it would open a dos like window and run it.

Well I transferred all my stuff over to the new Macbook Pro and now that .pl file just opens as a text file. It isn't executable anymore. What is the easiest method of running the script? I've tried a bunch of things in Terminal that I read while doing research but nothing has worked. I tried downloading ActivePerl for mac also hoping it would just recognize it and let me execute the file but that failed as well. I can't figure this out for the life of me!

Please help a brother out. I appreciate it!



Makosuke
Nov 12, 2010, 08:58 PM
Well, to start with Perl is installed on the MacOS out of the box, so you don't need to install anything at all to run a perl script. OSX is UNIX, so perl will behave exactly as it would in any other UNIX-like command line environment, if you've ever worked, say, with a Linux-based webhost via a telnet session.

Second, the command to run a perl script is just "perl [script path]". Dragging a file to a terminal window will automatically insert the path to that file, so the quickest way to run a script is to open a Terminal window, type "perl " (with the trailing space), and then drag the .pl file you want to run onto that terminal window. You'll get something like:
perl /Users/myname/test.pl
Now just hit return, and the script will execute.

If you want a specific commonly-used script to automatically open Terminal and execute, that can be done with a .command file, which you can look up instructions for elsewhere.

robvas
Nov 12, 2010, 09:01 PM
It's already installed. Fire up terminal and try this:

Create the following file, hello.pl - I use nano, you can use any editor you want.
#!/usr/bin/perl
print "Hello from Perl on Mac OS X!\n";


Then type:

chmod 755 hello.pl
(that makes it executable)
./hello.pl
(that will run it)

Hello from Perl on Mac OS X!
(that's the output of the file)

If you want to double-click .pl files, you can change the program they are opened with, from the 'Get Info' screen for that file

balamw
Nov 12, 2010, 09:07 PM
Well I transferred all my stuff over to the new Macbook Pro and now that .pl file just opens as a text file. It isn't executable anymore. What is the easiest method of running the script?

The two key things are the first line from robvas' post which tells it where perl is so it can run it #!/usr/bin/perl and the chmod which tells the system you can run it.

To double click it within Finder, you may need to install a third party launcher.

B

BGTHEMOGUL
Nov 12, 2010, 09:17 PM
That is the first line of the script already. The script worked perfect on the PC so I know it is written correctly. I just can't execute it.

Your example doesn't really help me figure out how to actually run MY script that I have. Can you please give me step by step instructions on what to do? Right now I just have the file called "zshare.pl" saved in my Documents folder.

How can I run this file? Running it by simply double clicking it would be ideal and then I would just open with textedit when I need to edit the script.

If it can't be opened just by double clicking then I would need to know how to get Terminal to run the script. I can't figure it out with the codes. I've tried stuff in Terminal both in the white window that opens when you open the application and also by choosing "New Command" under the shell option at the top. I'm lost.

BGTHEMOGUL
Nov 12, 2010, 09:21 PM
Thanks for the reply. I've tried what you wrote there several times and got no result. I've gone into Terminal and entered the following verbatim without the quotation marks.

"perl Users/Gizzle/Documents/zshare.pl"

When I click enter it does nothing. Then I tried entering that in the "command" option and it gave me an error.

BGTHEMOGUL
Nov 12, 2010, 09:22 PM
I've also tried to "Open With" and chose Terminal as the application and that failed as well.

lee1210
Nov 12, 2010, 10:19 PM
Open terminal.
Type:
perl
followed by a space.
Drag the file from a finder window into the terminal window.
The full path to the script will appear, so you'll have:
perl /Users/bobblethompkins/Documents/zshare.pl

Press enter to run your script.

Or when you open terminal:
cd Documents
perl zshare.pl

-Lee

firestarter
Nov 12, 2010, 10:31 PM
There are two ways to approach this.

Either do as others have suggested and just run the scripts in terminal.

Open the terminal window and then go into your Documents folder:

cd ~/Documents

Means 'change directory into the Documents folder that lives in my home directory' (~ is short for home).

You can then tell Perl to run the script:

perl testscript.pl

will start the perl interpreter and give it the file testscript.pl to run.

If you just type testscript.pl at the command line, the BASH terminal app will assume it's BASH code to run. You must add the extra line:
#!/usr/bin/perl
at the top of your testfile.pl file to tell BASH that it has to call perl in order to run this app.

You also have to make it executable:
chmod +x testfile.pl

then to run it you would type './testscript.pl'

The ./ is a security feature, and is telling the OS that you want to run the 'testscript.pl' file in the current directory. Modern UNIX variants are usually set up to NOT run apps by name in the current directory unless explicitly pointed to like this - this is to stop someone leaving a trojan in the directory with a name like 'ls' or 'cd'.


OK - that was all quite involved, and I think you're going to prefer my second suggestion.


Activestate on the PC gives you a nice script wrapper for your perl apps that makes them clickable, and they pop up in their own window. It's also possible to do that on the Mac, but you have to do a little work to make a standalone app.

The easiest/best way I've found so far is to use Platypus (http://www.sveinbjorn.org/platypus). This free app lets you create a double-clickable perl script, that will generate it's own pop up window to run in. It'll give you the same easy experience you had on the PC.

BGTHEMOGUL
Nov 12, 2010, 11:06 PM
Open terminal.
Type:
perl
followed by a space.
Drag the file from a finder window into the terminal window.
The full path to the script will appear, so you'll have:
perl /Users/bobblethompkins/Documents/zshare.pl

Press enter to run your script.

Or when you open terminal:
cd Documents
perl zshare.pl

-Lee

Thanks Lee. This worked and I see the script is running in the Terminal window. It seems to be running slow but at least it is running. Will that window automatically close when the script is finished like it does on PC? Or will it remain open and the script will just stop upon completion?

BGTHEMOGUL
Nov 12, 2010, 11:07 PM
OK - that was all quite involved, and I think you're going to prefer my second suggestion.


Activestate on the PC gives you a nice script wrapper for your perl apps that makes them clickable, and they pop up in their own window. It's also possible to do that on the Mac, but you have to do a little work to make a standalone app.

The easiest/best way I've found so far is to use Platypus (http://www.sveinbjorn.org/platypus). This free app lets you create a double-clickable perl script, that will generate it's own pop up window to run in. It'll give you the same easy experience you had on the PC.

This idea sounds much better. Once I download this application how do I set it up so I can just double click the .pl file to run the script? Is it as simple as just choosing "Open With" and selecting this program?

firestarter
Nov 12, 2010, 11:18 PM
This idea sounds much better. Once I download this application how do I set it up so I can just double click the .pl file to run the script? Is it as simple as just choosing "Open With" and selecting this program?

Yeah, just double click on Platypus

Choose a name for your app
Dropdown script type to Perl
Click 'select' to find your Perl script
I'd change 'Output' dropdown to 'Text window'
Click 'Create' and it asks where you want to save

Done.

Note: Platypus bundles your script into a new file, so if you change the original script, the Platypus app version WON'T change. It's best to conveniently run completed scripts - for debugging new scripts terminal is easier.

BGTHEMOGUL
Nov 12, 2010, 11:24 PM
Yeah, just double click on Platypus

Choose a name for your app
Dropdown script type to Perl
Click 'select' to find your Perl script
I'd change 'Output' dropdown to 'Text window'
Click 'Create' and it asks where you want to save

Done.

Note: Platypus bundles your script into a new file, so if you change the original script, the Platypus app version WON'T change. It's best to conveniently run completed scripts - for debugging new scripts terminal is easier.

Ah ok so this might not work for me then. I edit the script every time that I run it. So you are saying with this application I would have to create a new Platypus file every time I changed the script, correct?

No way of having it like on a PC where you can just double click the .pl file to run it and then right click and open with textedit to edit the script? Then double click to execute again the new script?

firestarter
Nov 12, 2010, 11:35 PM
You really need to spend half a day just learning some simple terminal commands and getting comfortable with that. The terminal is really powerful and productive, and I'm sure you would find it useful. A lot of the ideas behind Perl came from the Unix terminal, so it should be easy to learn.

If you really don't want to do that, then my final advice would be to buy TextMate (http://macromates.com/).

This is easily the best programmer's text editor I've used. You can also run the app you're currently editing - (Bundles->Perl->Run Script). If you're continually editing and rerunning it may be ideal.

BGTHEMOGUL
Nov 12, 2010, 11:44 PM
You really need to spend half a day just learning some simple terminal commands and getting comfortable with that. The terminal is really powerful and productive, and I'm sure you would find it useful. A lot of the ideas behind Perl came from the Unix terminal, so it should be easy to learn.

If you really don't want to do that, then my final advice would be to buy TextMate (http://macromates.com/).

This is easily the best programmer's text editor I've used. You can also run the app you're currently editing - (Bundles->Perl->Run Script). If you're continually editing and rerunning it may be ideal.

Thanks for the help. I think the Terminal option will be fine. It's pretty straight forward now with the way Lee explained and you further explained. Do you happen to know the command I can put in the script to close the window when finished?

firestarter
Nov 12, 2010, 11:48 PM
If you start the app from a line in the terminal, then you can't easily kill the terminal afterwards - you're just returned to the command line.

BGTHEMOGUL
Nov 12, 2010, 11:59 PM
If you start the app from a line in the terminal, then you can't easily kill the terminal afterwards - you're just returned to the command line.

Right that's exactly what happened with a test I just ran. It returned me to the command line when the script was finished. Thought maybe there is a command or code you can put into the perl script to kill the window when complete.

BGTHEMOGUL
Nov 13, 2010, 12:11 AM
Here is the code that works perfectly and kills the Terminal application completely when the script is finished.

; killall Terminal


Now I just need to find the code that kills only that particular window and not the entire application when the script completes.

lee1210
Nov 13, 2010, 12:20 AM
I posted about using a shell script wrapper, but the finder doesn't do the right thing with that, and .command exits the shell but leaves the terminal window open. Not sure how to handle this off the top of my head.

-Lee

BGTHEMOGUL
Nov 13, 2010, 12:31 AM
Ok the one I posted before works perfectly if you only run one script and want to kill the whole application and exit out when finished. I sometimes have multiple scripts running though and that's the issue. If it kills the whole application then the others would be shut down and not finish correctly.

firestarter
Nov 13, 2010, 12:35 AM
That killall is just nasty.

Can I ask why you're editing your code each time you run it? If you're running with a different file, or need different parameters there are better ways than hardcoding each time.

BGTHEMOGUL
Nov 13, 2010, 01:01 AM
I have to change the URL in the script every time.

firestarter
Nov 13, 2010, 06:35 AM
I have to change the URL in the script every time.

Then use <STDIN> to prompt the user for the URL!

Hacking the code each time you run is the wrong way to do things.

robbieduncan
Nov 13, 2010, 06:42 AM
Then use <STDIN> to prompt the user for the URL!

Hacking the code each time you run is the wrong way to do things.

The ReadLine (http://perldoc.perl.org/Term/ReadLine.html) package is good for reading from the terminal. I also like GetOpt::Long (http://perldoc.perl.org/Getopt/Long.html) to accept parameters from the command line.

firestarter
Nov 13, 2010, 06:57 AM
The ReadLine (http://perldoc.perl.org/Term/ReadLine.html) package is good for reading from the terminal. I also like GetOpt::Long (http://perldoc.perl.org/Getopt/Long.html) to accept parameters from the command line.

There's more than one way to do it (http://en.wikipedia.org/wiki/There's_more_than_one_way_to_do_it) ™

:D

chown33
Nov 13, 2010, 11:52 AM
Run an applescript command that prompts for a URL.
osascript -e 'tell app "System Events" to choose URL'
It writes the URL (or nothing if cancelled) to its stdout stream.

AppleScript has many more graphical user interaction mechanisms, and all are easily invoked by the 'osascript' command. Use AppleScript Editor to open the scripting dictionary of "Standard Additions" and look under the User Interaction section.

balamw
Nov 13, 2010, 03:13 PM
Run an applescript command that prompts for a URL.
osascript -e 'tell app "System Events" to choose URL'
It writes the URL (or nothing if cancelled) to its stdout stream.


Since AppleScript has better Finder integration and UI components, it would seem to make sense to wrap the perl script with the AppleScript rather than vice-versa, right?

Note too that you can simply drag & drop or copy & paste URLs from Safari in many cases which comes in handy if you want to pass a URL to curl or wget or the like...

B