Page 1 of 1

Need help making a "Perl" program work for my mac.

Posted: 2010-11-14 03:26pm
by Crossroads Inc.
For those following my Dwarf Fortress game, recently I started deisgning a very elaborate city that is taking forever to do. looking at the Bay12 DF forums, apprently someone wrote a program that can automaticlly designate, not just circles, but "domes" as well.
I wrote a quick Perl program to designate my spherical dome correctly. It has radius 20, for a different radius change $r. Non-integer values of $r may look better for circles, but I use whole numbers for spheres.

Code: Select all

#!/usr/bin/perl


use Tk;

$Z = 0;

################### EDIT RADIUS HERE ##########################
$r = 20;
###############################################################

$R = int(2*$r) - int(int(2*$r)-$r);
$mw = tkinit();
$cv = $mw->Canvas(-width => (2*$R+1)*16, -height => (2*$R+1)*16)->pack();
for my $y (0..2*$R) {
	for my $x (0..2*$R) {
		$cv->create("rectangle",
			$x*16, $y*16,
			($x+1)*16, ($y+1)*16,
			-fill => "white",
			-tags => ["t $y $x"]);
	}
}
$cv->create("text", 0, 0, 
		-anchor => "nw", 
		-text => "Level 0", 
		-font => "Helvetica 24", 
		-fill => "yellow",
		-tags => ["text"]);

$mw->bind("<less>", sub {$Z++; upd()});
$mw->bind("<greater>", sub {$Z--; upd()});
$mw->bind("<space>", \&upd);
$mw->bind("<Escape>", sub {exit});
upd();
MainLoop;

sub upd {
	$cv->itemconfigure("text", -text => "Level $Z");
	for my $y (0..2*$R) {
		for my $x (0..2*$R) {
			$col = (($x-$R)**2 + ($y-$R)**2 + $Z*$Z < $r*$r) ? "white" :
				(($x-$R)**2 + ($y-$R)**2 + ($Z-1)*($Z-1) < $r*$r) ? "#5f5f5f" : "black";
			$cv->itemconfigure("t $y $x", -fill => $col);
		}
	}
}
There was even a bit about how to load it, but I want to make sure I am doing this rigth before messing around in my system:
Save this thing as a plain-text .pl file with a no-format text file editor (TextEdit on Mac, disable RTF) and run it on a terminal (Applications -> Utilities):

Code: Select all

$ perl sphere.pl
Perl should be installed by default on all systems other than Windows.
BTW: The $ is the command prompt, don't type it.
I am preparing to do an attempt tonight on my iMac and just want to know if there is anythign I should look out for before trying to run it.

Re: Need help making a "Perl" program work for my mac.

Posted: 2010-11-14 03:31pm
by Chris OFarrell
Are you running OSX or one of its variants? As the are 'nix based, there is probably a Peal implementation out there for it, but I honestly don't know if its by default installed on your OS.

You might want to check that first, if the command line is giving any kind of error saying that it doesn't know what you're talking about. As you are invoking Perl explicitly and specifying the file-name directly, the shell SHOULD pick it up and be cool, I don't think you'll need to change the file permissions.

I would check the hashbang at the top, the '#!/usr/bin/perl' and make sure that that IS where Perl is on your computer if all else fails.

Re: Need help making a "Perl" program work for my mac.

Posted: 2010-11-14 03:43pm
by Marcus Aurelius
Chris OFarrell wrote:Are you running OSX or one of its variants? As the are 'nix based, there is probably a Peal implementation out there for it, but I honestly don't know if its by default installed on your OS.
Like the instructions Crossroads quoted say, perl programs should run out of the box on Mac OS X. If he follows the instructions everything should just work :wink:

Re: Need help making a "Perl" program work for my mac.

Posted: 2010-11-14 04:11pm
by Dave
Apple dev page says perl is installed on OS 10.2 (Jaguar), which was ages ago, so I would be inclined to believe that whatever version of OSX Crossroads has, he has perl.

Before continuing, let me say that I don't own a mac, but I use linux enough and I've messed briefly with perl.

As a quick and dirty way to check this, Crossroads, open the terminal* and run

Code: Select all

perl -v
If you get back something along the lines of

Code: Select all

This is perl, v5.8.1-RC3 built for darwin-thread-multi-2level
you're good, otherwise, if you get errors, you probably don't have it.

If you have success, the other thing to check is if the hashbang** matches the perl path. In english, that means making sure that first line of your program you picked up

Code: Select all

#!/usr/bin/perl
points at where perl actually is.
You can find where perl is by running

Code: Select all

whereis perl
in the terminal.

You should get back something like

Code: Select all

perl: /usr/bin/perl
which, in this case, would show that perl is where your program expects it to be. If not, change that first line in the program to match what "whereis" told you it should be (e.g.

Code: Select all

#!/usr/fobble/grommit/widget/perl
or whatever)


Finally, running your script as

Code: Select all

perl -a sphere.pl
should give you more debugging information than zero if things go wrong.

*
The Mac Terminal: An Introduction wrote: To open Terminal, navigate to your Applications folder, open Utilities, and double click on Terminal. You will be greeted with a message similar to this:

Code: Select all

Last login: Tue Mar 6 17:21:36 on console
Welcome to Darwin!
ibook:~ Alex$
** hashbang, the #! at the beginning of most scripts, where 'hash' is slang for the number sign and 'bang' is slang for the exclamation point. See also shebang, crunchbang, pound bang, etc.