[32393] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 3660 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Apr 8 03:09:27 2012

Date: Sun, 8 Apr 2012 00:09:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 8 Apr 2012     Volume: 11 Number: 3660

Today's topics:
        how i loved lisp cons and UML and Agile and Design Patt <xahlee@gmail.com>
    Re: How to keep the script from stopping or hanging <hjp-usenet2@hjp.at>
    Re: How to keep the script from stopping or hanging <rodbass63@gmail.com>
    Re: How to keep the script from stopping or hanging <jurgenex@hotmail.com>
    Re: How to keep the script from stopping or hanging (Randal L. Schwartz)
    Re: looking for a hexagon tiling module <derek.moody@clara.net>
    Re: looking for a hexagon tiling module <hjp-usenet2@hjp.at>
    Re: looking for a hexagon tiling module <*@eli.users.panix.com>
    Re: Running multiple scripts <dave@invalid.invalid>
    Re: Running multiple scripts <willem@toad.stack.nl>
    Re: Running multiple scripts <hjp-usenet2@hjp.at>
    Re: Running multiple scripts <dave@invalid.invalid>
    Re: Running multiple scripts (Tim McDaniel)
    Re: Running multiple scripts <uri@stemsystems.com>
    Re: Running multiple scripts <dave@invalid.invalid>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 7 Apr 2012 07:29:15 -0700 (PDT)
From: Xah Lee <xahlee@gmail.com>
Subject: how i loved lisp cons and UML and Agile and Design Patterns and Pythonic and KISS and YMMV and stopped worrying
Message-Id: <bb27a7e2-c90c-4b9c-b9fe-a4f0f310c7a9@n5g2000pbg.googlegroups.com>

OMG, how i loved lisp cons and macros and UML and Agile eXtreme
Programing and Design Patterns and Anti-Patterns and Pythonic and KISS
and YMMV and stopped worrying.

=E3=80=88World Multiconference on Systemics, Cybernetics and Informatics???=
=E3=80=89
http://xahlee.org/comp/WMSCI.html

highly advanced plain text format follows, as a amenity for tech
geekers.

-----------------------------------------------
World Multiconference on Systemics, Cybernetics and Informatics ???

Xah Lee, 2010-04-04

Starting in 2004, i regularly receive email asking me to participate a
conference, called =E2=80=9CWorld Multiconference on Systemics, Cybernetics
and Informatics=E2=80=9D (WMSCI). Here's one of such email i got today:


Dear Xah Lee:

As you know the Nobel Laureate Herbert Simon affirmed that design is
an essential ingredient of the Artificial Sciences Ranulph Glanville,
president of the American Society for Cybernetics and expert in design
theory, affirms that =E2=80=9CResearch is a variety of design. So do resear=
ch
as design. Design is key to research. Research has to be designed.=E2=80=9D=
 An
increasing number of authors are stressing the relationships between
Design and Research. Design is a mean for Research, and Research is a
mean for Design. Design and research are related via cybernetic loops
in the context of means-ends logic. Consequently, we invite you to
submit a paper/abstract and/ot to organize an invited session in the
International Symposium on Design and Research in the Artificial and
the Natural Sciences: DRANS 2010 (http://www.sysconfer.org/drans)
which is being organized in the context of The 14th World Multi-
Conference on Systemics, Cybernetics and Informatics: WMSCI 2010
(http://www.sysconfer.org/wmsci), 2010 in Orlando, Florida, USA.

=E2=80=A6

Here's the first email i got from them from my mail archive:

 From: sci2005@iiis.org
Subject: Inviting you to participate in SCI 2005
Date: October 20, 2004 1:39:48 PM PDT
To: XAH@XAHLEE.ORG

 Dear Dr. Xah Lee:

On behalf of the SCI 2005 Organizing Committee, I would like to invite
you to participate in the 9th World Multi-Conference on Systemics,
Cybernetics and Informatics (http://www.iiisci.org/sci2005), which
will take place in Orlando, Florida, USA, from July 10-13, 2005.

Full text wmsci.txt.

I do not know this organization. I don't know how they got my email or
how they know that i'm involved in the computer science community.
(surely from trawling email addresses in science forums) Though, after
getting a few of their emails, one clearly gets a sense that it is a
scam, soliciting innocent idiotic academicians (many PhDs are
idiots.).

Here's what Wikipedia has to say about them: World Multiconference on
Systemics, Cybernetics and Informatics. Here's a juicy quote:


WMSCI attracted publicity of a less favorable sort in 2005 when three
graduate students at MIT succeeded in getting a paper accepted as a
=E2=80=9Cnon-reviewed paper=E2=80=9D to the conference that had been random=
ly
generated by a computer program called SCIgen.[8] Documents generated
by this software have been used to submit papers to other similar
conferences. Compare to the Sokal affair.

WMSCI has been accused of using spam to advertise its conferences.[8]

Now and then, whenever i got their email, the curiosity in me do
lookup the several terms they used in the email, partly to check the
validity. For example, in this one, it mentions Herbert Simon. Another
one i recall i got recently mentioned Science 2.0. Both of the terms i
haven't heard of before.

One'd think that it is easy to tell scam from real science, but with
today's science proliferation, it's actually not that easy. Even if
you are a academic, it's rather common that many new science terms you
never heard of, because there are tremendous growth of new disciplines
or cross disciplines, along with new jargons. Cross-discipline is
rather common and natural, unlike in the past where science is more or
less clearly delineated hierarchy like Physics, Math, Chemistry,
Biology, etc and their sub-branches. However, many of today's new
areas is a bit questionable, sometimes a deliberate money-making
scheme, which i suppose is the case for WMSCI. Many of these, use
terms like =E2=80=9Cpost-modern=E2=80=9D, =E2=80=9Cscience 2.0=E2=80=9D to =
excuse themselves from the
rather strict judgment of classic science. Many of these terms such as
=E2=80=9Csystemics=E2=80=9D, =E2=80=9Ccybernetics=E2=80=9D, =E2=80=9Cinfoma=
tics=E2=80=9D are vague. Depending on the
context, it could be a valid emerging science discipline, but it could
also be pure new-age hogwash. And sometimes, nobody really knows
today. Fledgling scientific fields may started off as pseudo-science
but later became well accepted with more solid theories. (e.g.
evolutionary psychology)

In the past 2 decades, there are quite a few cases where peer reviewed
papers published in respected journals are exposed as highly
questionable or deliberate hoax, arose massive debate on the peer
review system. The peer-review system itself can't hold all the blame,
but part of it has to do with the incredible growth of sciences and
limitation of the single human mind to make sense of them all. For
examples, see:

 =E2=80=A2Sokal Affair (The Sokal Affair was a publishing hoax perpetrated =
by
Alan Sokal, a physics professor at New York University. In 1996, Prof.
Sokal submitted an article to Social Text, an academic journal
dedicated to postmodern cultural studies.)
 =E2=80=A2Bogdanov Affair (Twin brother physicists published in peer review=
ed
journal that was accused to be random mix of physics jargons)
 =E2=80=A2Jan Hendrik Schon (German physicist who briefly rose to prominenc=
e
after a series of apparent breakthroughs that were later discovered to
be fraudulent.[1] Before he was exposed, Sch=C3=B6n had received the Otto-
Klung-Weberbank Prize for Physics in 2001, the Braunschweig Prize in
2001 and the Outstanding Young Investigator Award of the Materials
Research Society in 2002, which was later rescinded.)

When it comes to philosophy, it is worse. For example, there's this
Hegel (1770 =E3=80=9C 1831). Personally, i regard him as the worst mumble-
jumbo scumbag.

In the software engineering area, which i'm a expert, there are quite
a huge number of wishy-washy shit. Examples include: eXtreme
Programing (espousing that programing should be done by 2 males
sitting in front of a computer together, one code while the other
sucks his cock.), Universal Modeling Language (which proclaims that
this pseudo-language solves the world's problems), Design Patterns
(which peddles best practices under the aegis of OOP and jargonism.),
and, of course there's Larry Wall, whose crime is too numerous to
list. (disclaimer: opinion only.)

Related:
 =E2=80=A2Larry Wall and Cults
 =E2=80=A2Perl: Theory vs Practice
 =E2=80=A2Jargons of Info Tech Industry
 =E2=80=A2Why Software Suck

Even in the area of math, i'm reminded of the guy Buckminster Fuller
(1895 =E3=80=9C 1983). Personally, i despise him. One of the worst new-age
garbage from the guy is the =E2=80=9CSynergetics=E2=80=9D.

On the other hand, there's Stephen Wolfram, with his A New Kind of
Science. It has received criticism from many scientists and
mathematicians as garbage, exasperated by Wolfram's megalomania
personality. Personally i respect his work highly. (See also: Notes on
A New Kind of Science.)

Related:
 =E2=80=A2Men Are From Mars, Women Are From Venus
 =E2=80=A2Spy vs Spy; Tech Geekers vs Spammers
 =E2=80=A2Scientology and Falun Gong

Truely, your troll,

 Xah


------------------------------

Date: Sat, 7 Apr 2012 15:21:19 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: How to keep the script from stopping or hanging
Message-Id: <slrnjo0fqf.17m.hjp-usenet2@hrunkner.hjp.at>

On 2012-04-07 12:25, Nene <rodbass63@gmail.com> wrote:
> This script below stops and hangs after it fails to find the directory
> in the following code:
> chdir("/c\$/$stuff/") || die "cannot cd into /c\$/$stuff ($!)";
>
> Here is the output it gets when it doesn't find the directory:
> cannot cd into /c$/blah (No such file or directory) at ./start_blah.pl
> line 12, <SCRATCHPAD> line 40.
>
> But I don't want it to stop, I want it to proceed.  How do I go about
> getting that?

Look up what "die" does.

	hp


-- 
   _  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         | 
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org


------------------------------

Date: Sat, 7 Apr 2012 08:11:55 -0700 (PDT)
From: Nene <rodbass63@gmail.com>
Subject: Re: How to keep the script from stopping or hanging
Message-Id: <2e8f0813-f1ce-4c9b-ab36-7cab44cba970@z17g2000yqf.googlegroups.com>

On Apr 7, 9:21=A0am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2012-04-07 12:25, Nene <rodbas...@gmail.com> wrote:
>
> > This script below stops and hangs after it fails to find the directory
> > in the following code:
> > chdir("/c\$/$stuff/") || die "cannot cd into /c\$/$stuff ($!)";
>
> > Here is the output it gets when it doesn't find the directory:
> > cannot cd into /c$/blah (No such file or directory) at ./start_blah.pl
> > line 12, <SCRATCHPAD> line 40.
>
> > But I don't want it to stop, I want it to proceed. =A0How do I go about
> > getting that?
>
> Look up what "die" does.

Thanks, I took out the "die", the script doesn't stop anymore.
>
> =A0 =A0 =A0 =A0 hp
>
> --
> =A0 =A0_ =A0| Peter J. Holzer =A0 =A0| Deprecating human carelessness and
> |_|_) | Sysadmin WSR =A0 =A0 =A0 | ignorance has no successful track reco=
rd.
> | | =A0 | h...@hjp.at =A0 =A0 =A0 =A0 |
> __/ =A0 |http://www.hjp.at/| =A0-- Bill Code on a...@irtf.org



------------------------------

Date: Sat, 07 Apr 2012 08:19:56 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: How to keep the script from stopping or hanging
Message-Id: <3nm0o75pfksmael7blggsvasg2m3lv51mo@4ax.com>

Nene <rodbass63@gmail.com> wrote:
>This script below stops and hangs after it fails to find the directory
>in the following code:
>chdir("/c\$/$stuff/") || die "cannot cd into /c\$/$stuff ($!)";
>
>But I don't want it to stop, I want it to proceed.  How do I go about
>getting that?

Maybe don't tell the script to "die()"?

jue


------------------------------

Date: Sat, 07 Apr 2012 10:22:57 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: How to keep the script from stopping or hanging
Message-Id: <86wr5rmp72.fsf@red.stonehenge.com>

>>>>> "Nene" == Nene  <rodbass63@gmail.com> writes:

Nene> chdir("/c\$/$stuff/") || die "cannot cd into /c\$/$stuff ($!)";

Nene> But I don't want it to stop, I want it to proceed.  How do I go about
Nene> getting that?

But then it'll be in the wrong directory.  The chdir() is there for a
reason, and apparently relatively mandatory, for the original author to
put the die there in the first place.

print "Just another Perl hacker,"; # the original

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.posterous.com/ for Smalltalk discussion


------------------------------

Date: Sat, 7 Apr 2012 19:03:00 +0100
From: "Derek.Moody" <derek.moody@clara.net>
Subject: Re: looking for a hexagon tiling module
Message-Id: <ant0718000b0BxcK@strongarm.half-baked-idea.co.uk>

In article <eli$1204061406@qz.little-neck.ny.us>, Eli the Bearded
<URL:mailto:*@eli.users.panix.com> wrote:
> I'd like a module to manipulate co-ordinates on an X,Y plane that
> have an overlapping hexagon grid. Consider, for example, a game

OK,as a sometime game programmer...

> I want the functions to be able to work that stuff out.
> Does it exist currently?

> and for bonus points the hexgrid should be able to have a different rotation

A hex grid is conceptually the same as an overlapping set of rectangles - it
maps to the layout of the bricks in a wall for eg.

Maintain:

  Each object as hex-grid-location and state (as complex as required.)
  A lookup table of state renderings
    (either all-possible or as generated and preserved.)
  A pre-screen image one hex too big in each margin.

A lookup table of renderings for any set of seven can be xor-mapped to any
position.  This might involve pre-rendering stages too - awaiting, eg,
lighting modifications. 

Snapshot the screen display from the pre-screen at the desired origin.
Most likely you'll have a trade-off between hardware blitting and software,
often both are implemented giving a first class display on capable hardware
and lower res on the rest.

A pure perl blitter is unlikely to be adequate on uncontrolled end-user
machines.

This is very much video game programmimg 101.  

And rather hardware dependent so not the sort of stuff that I'd expect to
see on CPAN.

Cheerio,

-- 
>> derek.moody@clara.net



------------------------------

Date: Sat, 7 Apr 2012 21:44:23 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: looking for a hexagon tiling module
Message-Id: <slrnjo168n.bk0.hjp-usenet2@hrunkner.hjp.at>

["Followup-To:" header set to comp.lang.perl.misc.]
On 2012-04-06 18:37, Eli the Bearded <*@eli.users.panix.com> wrote:
> I'd like a module to manipulate co-ordinates on an X,Y plane that
> have an overlapping hexagon grid. Consider, for example, a game
> that uses a hexagonal grid to move around. Chinese Checkers (star
> halma) is one such game. You would want functions to turn a mouse
> click into a hexagon tile id of some sort, and you'd want functions
> to calculate how to draw the tiles if the side is S pixels, or
> if you want to fit N tiles across on a M wide field.
>
> I don't want to implement a game, I do want to be able to divide a
> pixel plane into hexagons, calculate which pixels would be interior,
> which pixels would be a border, know which hexagons are complete
> and which are clipped. I don't want something that is tied to a
> particular drawing system, because it probably won't be the one I
> want to use.
[...]
> As an example, here's this bit of ASCII art I must have spent
> all of five minutes on. The hexagonal tiles are numbered with
> example coordinates. There is a image area superimposed upon
> this grid with a border of : + and =. The upper left hand corner
> is about one quarter of the 0,0 hex grid. Cell 1,2 is the upper-
> most, leftmost full hexagon, cell 1,1 is the leftmost, upper-
> most full hexagon. 12 characters right and 16 down from the +
> in the image corner is the * in cell 3,2. 18 characters right
> and 5 down from the + is the L on the border of 0,3 and 1,3.
>
>          ___         ___         ___         ___
>         /   \       /   \       /   \       /   \
>        /     \     /     \     /     \     /     \
>       /  0,0  \___/  0,2  \___/  0,4  \___/  0,6  \_
>       \   +===/===\=======/===\=======/===\=======/=
>        \  :  /     \     /     \     /     \     /
>         \_:_/  0,1  \___/  0,3  \___/  0,5  \___/
>         / : \       /   \       /   \       /   \
>        /  :  \     /     \     /     \     /     \
>       /  1,0  \___/  1,2  \_L_/  1,4  \___/  1,6  \_
>       \   :   /   \       /   \       /   \       / 
>        \  :  /     \     /     \     /     \     /
>         \_:_/  1,1  \___/  1,3  \___/  1,5  \___/
>         / : \       /   \       /   \       /   \
>        /  :  \     /     \     /     \     /     \
>       /  2,0  \___/  2,2  \___/  2,4  \___/  2,6  \_
>       \   :   /   \       /   \       /   \       / 
>        \  :  /     \     /     \     /     \     /
>         \_:_/  2,1  \___/  2,3  \___/  2,5  \___/
>         / : \       /   \       /   \       /   \
>        /  :  \     /  *  \     /     \     /     \
>       /  3,0  \___/  3,2  \___/  3,4  \___/  2,6  \_
>       \   :   /   \       /   \       /   \       / 
>        \  :  /     \     /     \     /     \     /
>         \_:_/  3,1  \___/  3,3  \___/  3,5  \___/
>         / : \       /   \       /   \       /   \
>

I took that as a challenge for a bit of weekend programming ;-)

It doesn't do everything you want (must leave some of the fun to you)
but it should get you started:


package Math::HexGrid;

use warnings;
use strict;
use 5.010;
use POSIX qw(floor);

our $VERSION = 0.001;

sub new {
    my ($class, $s, $h) = @_;

    # $s is the length of a side of a cell,
    # $h is the height of half a cell.
    #    we can compute that from $s or let the user specify it.
    $h = int($s * sqrt(3) / 2) unless defined $h;
    my $self = { s => $s, h => $h };
    bless $self, $class;
    return $self;
}

sub center_of_hex {
    my ($self, $ind_x, $ind_y) = @_;

    my ($s, $h) = @{$self}{'s', 'h'};
    my ($px_x, $px_y);

    # That's simple. The horizontal differenc between the center of
    # (0,0) and (0,2) is obviously 3 * $s. (0,1) sits between them
    # but is offset by $h in the y axis.
    if ($ind_x % 2 == 0) {
	$px_x = $ind_x * 3/2 * $s;
	$px_y = $ind_y * 2 * $h;
    } else {
	$px_x = $ind_x * 3/2 * $s;
	$px_y = ($ind_y * 2 + 1) * $h;
    }
    return ($px_x, $px_y);
}

sub hex_from_coord {
    my ($self, $px_x, $px_y) = @_;

    my ($s, $h) = @{$self}{'s', 'h'};

    # Not quite as simple, but if you squint a bit
    # you can see that the pattern repeats every 
    # 3 * $s horizontally and every 2 * $h vertically.
    # so we split the plane into rectangles of this size
    my $seg_x = floor($px_x / (3 * $s));
    my $seg_y = floor($px_y / (2 * $h));
    my $rel_x = $px_x - $seg_x * 3 * $s;
    my $rel_y = $px_y - $seg_y * 2 * $h;

    my ($ind_x, $ind_y);

    # and then we split those rectangles into vertical stripes
    # again.
    # Everything between +/- 0.5 $s horizontally from the center of a
    # cell belongs to that cell.
    # but between that the cells overlap and we have to check on which
    # side of the diagonals the pixel is.
    given ($rel_x) {
	when ($_ < 0.5 * $s) {
	    $ind_x = $seg_x * 2;
	    $ind_y = $seg_y + ($rel_y >= $h);
	}
	when ($_ < 1 * $s) {
	    my $r_x = ($_ - $s / 2) / ($s / 2);
	    my $r_y = abs(($rel_y - $h) / $h);
	    if ($r_y < $r_x) {
		$ind_x = $seg_x * 2 + 1;
		$ind_y = $seg_y;
	    } else {
		$ind_x = $seg_x * 2;
		$ind_y = $seg_y + ($rel_y >= $h);
	    }
	}
	when ($_ < 2 * $s) {
	    $ind_x = $seg_x * 2 + 1;
	    $ind_y = $seg_y;
	}
	when ($_ < 2.5 * $s) {
	    my $r_x = (2.5 * $s - $_) / ($s / 2);
	    my $r_y = abs(($rel_y - $h) / $h);
	    if ($r_y < $r_x) {
		$ind_x = $seg_x * 2 + 1;
		$ind_y = $seg_y;
	    } else {
		$ind_x = $seg_x * 2 + 2;
		$ind_y = $seg_y + ($rel_y >= $h);
	    }
	}
	when ($_ < 3 * $s) {
	    $ind_x = $seg_x * 2 + 2;
	    $ind_y = $seg_y + ($rel_y >= $h);
	}
    }
    return ($ind_x, $ind_y);

}

1;

	hp

PS: I did see Derek's posting just before posting this, but afaics he
concentrates on rendering graphics while my code does just geometry, no
graphics at all (although I've also written a small test script to
create a PNG with hex-tiles).

-- 
   _  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         | 
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org


------------------------------

Date: Sun, 8 Apr 2012 04:45:37 +0000 (UTC)
From: Eli the Bearded <*@eli.users.panix.com>
Subject: Re: looking for a hexagon tiling module
Message-Id: <eli$1204080020@qz.little-neck.ny.us>

In comp.lang.perl.misc, Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
> ["Followup-To:" header set to comp.lang.perl.misc.]

Noted.

> It doesn't do everything you want (must leave some of the fun to you)
> but it should get you started:

Oh, I got started, but I started from a different angle than
you. I like your hex_from_coord(), I will probably adapt that.

I translated some C sharp code I found to create hexagons and
hexagon grids, but I'm not happy with that guy's inside-polygon
function. 

Code I found:
http://www.codeproject.com/Articles/14948/Hexagonal-grid-for-games-and-other-projects-Part-1

(Note that I'm using floating point coordinates.)

# basic math

sub degtorad { my $deg = shift; return ($deg * $pi / 180); }
sub round5p  { my $num = shift; return sprintf('%0.5f', $num); }

# Consider hexagon with side 's':
# a is distance between two parallel sides,
# b is distance between two opposite vertices.
# 
#        __ b __  
#       /       \
#    . :   ___. h:
#   /     /   \   \
#  |     /     \   r    a = 2*r		total height
#  a    /       \./     b = 2*h + s	total width
#  |    \       /
#  |     \     /
#   \.    \___/
#          . .
#           s
sub hfroms   { my $s = shift; return ($sin30 * $s); }
sub rfroms   { my $s = shift; return ($cos30 * $s); }
sub bfroms   { my $s = shift; return (2 * hfroms($s) + $s); }
sub bfromsh  { my $s = shift; my $h = shift; return (2 * $h + $s); }
sub afroms   { my $s = shift; return (2 * rfroms($s)); }
sub afromr   { my $r = shift; return (2 * $r); }

$sin30 = sin( degtorad(30) );
$cos30 = cos( degtorad(30) );

# Two hexagon orientations:
#       0 ___ 1             0
#        /   \             ,'',
#       /     \        5 ,'    ', 1
#    5 /  flat \         |      |
#      \       / 2       |pointy|
#       \     /        4 ',    ,' 2
#        \___/             ',,'
#       4    3              3

sub pt_f_upperleft   { return 0; }
sub pt_p_top         { return 0; }
sub pt_f_upperright  { return 1; }
sub pt_p_upperright  { return 1; }
sub pt_f_middleright { return 2; }
sub pt_p_bottomright { return 2; }
sub pt_f_bottomright { return 3; }
sub pt_p_bottom      { return 3; }
sub pt_f_bottomleft  { return 4; }
sub pt_p_bottomleft  { return 4; }
sub pt_f_middleleft  { return 5; }
sub pt_p_upperleft   { return 5; }

# From a side length and a single starting point, work out where
# the other points are.

# HexagonalTest/HexagonalTest/Hexagonal/Hex.cs: Hex
sub calc_vertices {
  my $s = shift;	# length of side
  my $p0_x   = shift;	# x location of point #0
  my $p0_y   = shift;	# y location of point #0
  my $orient = shift;	# 'flat' or 'pointy'

  my @points;
  
  my $h = hfroms($s);
  my $r = rfroms($s);

  push(@points, [$p0_x, $p0_y]);			# point 0

  if(defined($orient) and lc($orient) eq 'flat') {
    # flat
    push(@points, [($p0_x + $s), $p0_y]);		# point 1
    push(@points, [($p0_x + $s + $h), ($p0_y + $r)]);	# point 2
    push(@points, [($p0_x + $s), ($p0_y + 2 * $r)]);	# point 3
    push(@points, [$p0_x, ($p0_y + 2 * $r)]);		# point 4
    push(@points, [($p0_x - $h), ($p0_y + $r)]);	# point 5
  } else {
    # pointy
    push(@points, [($p0_x + $r), ($p0_y + $h)]);	# point 1
    push(@points, [($p0_x + $r), ($p0_y + $s + $h)]);	# point 2
    push(@points, [$p0_x, ($p0_y + $s + 2 * $h)]);	# point 3
    push(@points, [($p0_x - $r), ($p0_y + $s + $h)]);	# point 4
    push(@points, [($p0_x - $r), ($p0_y + $h)]);	# point 5
  }

  return @points;
} # end &calc_vertices

# HexagonalTest/HexagonalTest/Hexagonal/Board.cs: Initialize
sub hex_board {
  my $bs    = shift; # hash ref
  my @hexes;

  # TODO: allow pixelwidth and pixelheight, then set gridwidth and gridheight
  if(!defined($$bs{gridwidth}))   { die "hex_board need gridwidth\n"; }
  if(!defined($$bs{gridheight}))  { die "hex_board need gridheight\n"; }

  if(!defined($$bs{side}))        { die "hex_board need side\n"; }
  if(!defined($$bs{orient}))      { die "hex_board need orient\n"; }
  if(!defined($$bs{xoffset}))     { die "hex_board need xoffset\n"; }
  if(!defined($$bs{yoffset}))     { die "hex_board need yoffset\n"; }

  my $h = hfroms($$bs{side});
  my $r = rfroms($$bs{side});

  my $hexwidth;
  my $hexheight;
  my $isflat = undef;

  if( $$bs{orient} eq 'flat' ) {
    $isflat = 1;

    $hexwidth  = $$bs{side} + $h;
    $hexheight = 2 * $r;

    $$bs{pixelwidth}  = $$bs{gridwidth}  * $hexwidth  + $h;
    $$bs{pixelheight} = $$bs{gridheight} * $hexheight + $r;
  } else {
    $hexwidth  = 2 * $r;
    $hexheight = $$bs{side} + $h;

    $$bs{pixelwidth}  = $$bs{gridwidth}  * $hexwidth  + $r;
    $$bs{pixelheight} = $$bs{gridheight} * $hexheight + $h;
  }

  print STDERR "$$bs{gridwidth} $$bs{orient} $$bs{side} unit cells is $$bs{pixelwidth} wide\n";
  print STDERR "$$bs{gridheight} $$bs{orient} $$bs{side} unit cells is $$bs{pixelheight} tall\n";
  my %where = (
    inTopRow => undef,
    inBottomRow => undef,
    inLeftColumn => undef,
    inRightColumn => undef,
    isTopLeft => undef,
    isTopRight => undef,
    isBotomLeft => undef,
    isBottomRight => undef,
  );

  # i = y coord (rows), j = x coord (columns) of the hex tiles 2D plane
  my $i;
  my $j;
  my $ij_x;
  my $ij_y;
  my $refhex;

  for ($i = 0; $i < $$bs{gridheight} ; $i ++) {
    if($i == 0) {
      $where{inTopRow} = 1;
    } else {
      $where{inTopRow} = 0;
    }

    if($i == ($$bs{gridheight} - 1)) {
      $where{inBottomRow} = 1;
    } else {
      $where{inBottomRow} = 0;
    }

    for ($j = 0; $j < $$bs{gridwidth} ; $j ++) {

      # not sure why orig code set these first three
      $where{inRightColumn} = $where{isTopRight} = $where{isBottomRight} =
      $where{inLeftColumn} = $where{isTopLeft} = $where{isBottomLeft} =  0;

      if($j == 0) {
        $where{inLeftColumn} = 1;
	if($where{inTopRow}) {
	  $where{isTopLeft} = 1;
	}

	if($where{inBottomRow}) {
	  $where{isBottomLeft} = 1;
	}
      }

      if($j == ($$bs{gridwidth} - 1)) {
        $where{inRightColumn} = 1;
	if($where{inTopRow}) {
	  $where{isTopRight} = 1;
	}

	if($where{inBottomRow}) {
	  $where{isBottomRight} = 1;
	}
      }

      if($where{isTopLeft}) {
        if($isflat) {
	  $ij_x = $h + $$bs{xoffset};
	  $ij_y =      $$bs{yoffset};
	} else {
	  # pointy
	  $ij_x = $r + $$bs{xoffset};
	  $ij_y =      $$bs{yoffset};
	}
      } else {
        # not top left

        if($isflat) {
	  if($where{inLeftColumn}) {
	    # calc from above
	    $refhex = $hexes[$i - 1][$j];
	    ($ij_x, $ij_y) = @{$$refhex[pt_f_bottomleft]};
	  } else {
	    # not left column

	    # calc from left, with staggered columns
	    $refhex = $hexes[$i][$j - 1];

	    if($j % 2) {
	      ($ij_x, $ij_y) = @{$$refhex[pt_f_middleright]};
	    } else {
	      ($ij_x, $ij_y) = @{$$refhex[pt_f_upperright]};
	      $ij_x += $h;
	      $ij_y -= $r;
	    }
	  }
	} else {
	  # pointy
	  if($where{inLeftColumn}) {
	    # calc from above, with staggered columns
	    $refhex = $hexes[$i - 1][$j];

	    if($i % 2) {
	      ($ij_x, $ij_y) = @{$$refhex[pt_p_bottomright]};
	    } else {
	      ($ij_x, $ij_y) = @{$$refhex[pt_p_bottomleft]};
	    }
	  } else {
	    # not left column
	    # calc from left
	    $refhex = $hexes[$i][$j - 1];

	    ($ij_x, $ij_y) = @{$$refhex[pt_p_upperright]};
	    $ij_x += $r;
	    $ij_y -= $h;
	  }
	}
      }

      push(@{$hexes[$i][$j]},
      		calc_vertices($$bs{side}, $ij_x, $ij_y, $$bs{orient}));
    } # for j
  } # for i

  return @hexes;
} # end &hex_board

The InsidePolygon he has comes from
http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
And is based on "Solution 1 (2D)" there. But the solution 3, for
convex polygons only, looks nicer to me. For one thing it distinguishes
inside, outside, and right on the edge.

My next directions after an inside polygon function will be finding
the center point of a cell (easy) and then finding the vertices of
a different sized cell on that same center point (not tough). That's
the direction I was going to head into for finding cell wall margins.

Elijah
------
the author of that hex code doesn't do very good ASCII art diagrams


------------------------------

Date: Sat, 7 Apr 2012 13:15:00 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Running multiple scripts
Message-Id: <fV45K0OBJxbE-pn2-hN9pV9fdwH1t@localhost>

On Sat, 7 Apr 2012 11:59:39 UTC, "Dave Saville" <dave@invalid.invalid>
wrote:

<snip>

A bit of Googling turns up "do" which is yet another perl thing I 
didn't know about :-) But although it works once I don't get control 
back if the invoked script does not fail.

do 'script1.pl';
print "hello";
do 'script2.pl';

Never says "hello" unless script1 dies.
-- 
Regards
Dave Saville


------------------------------

Date: Sat, 7 Apr 2012 13:22:11 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: Running multiple scripts
Message-Id: <slrnjo0fs3.2n91.willem@toad.stack.nl>

Dave Saville wrote:
) On Sat, 7 Apr 2012 11:59:39 UTC, "Dave Saville" <dave@invalid.invalid>
) wrote:
)
)<snip>
)
) A bit of Googling turns up "do" which is yet another perl thing I 
) didn't know about :-) But although it works once I don't get control 
) back if the invoked script does not fail.

The script you called probably has an 'exit' in there somewhere, which
causes your master script to exit.

) do 'script1.pl';
) print "hello";
) do 'script2.pl';
)
) Never says "hello" unless script1 dies.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


------------------------------

Date: Sat, 7 Apr 2012 15:19:34 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: Running multiple scripts
Message-Id: <slrnjo0fn6.17m.hjp-usenet2@hrunkner.hjp.at>

On 2012-04-07 11:59, Dave Saville <dave@invalid.invalid> wrote:
> I have a commercial program. The author has provided "hooks" at 
> various points in the program. The "hook" code can be written in 
> anything. Most of mine are perl but there is a problem in that I 
> actually want to run more than one script per hook.
>
> At present I have a master script per hook that runs the required 
> scripts. But performance wise I am (re)loading the perl interpreter 
> every script. All scripts take the same two parameters and it is not 
> practical to combine them into one as they may be used by themselves 
> or in more than one hook.
>
> Is there some cunning way I can invoke a perl script from inside 
> another without turning it into a function/package? Remember I need to
> use them standalone too.
>
> I was thinking, without any real knowledge, if I could do something 
> like the following although I don't know how I would pass the 
> parameters.
>
> use strict;
> use warnings;
> my $parm1 = shift; 
> my $parm2 = shift;
> my $HANDLE;
> open $HANDLE, "somecode.pl";
> undef $/;
> my $stuff = <$HANDLE>;
> close $HANDLE;
> eval $stuff;

Yes, that would work. 

However, there is already a perl-builtin for the open...eval part. It is
called "do EXPR". 

And then there is a builtin which calls "do" and does a couple of other
things, which is called "require".

And then there is a builtin which calls "require" and does a couple of
other things, which is called "use".

So pretty soon we arrive at the standard Perl mechanism for using
libraries, which is what you should have thought of in the first place
if you wanted to reuse code.

So I would put all my hooks into one or more .pm files and then write
mini-scripts like this:

#!/usr/bin/perl
use strict;
use warnings;
use CommercialApp::Hooks1 qw(hook1a hook1b);
use CommercialApp::Hooks2 qw(hook2a);

hook1a(@ARGV);
hook1b(@ARGV);
hook2a(@ARGV);
__END__

or for the simple case:

#!/usr/bin/perl
use strict;
use warnings;
use CommercialApp::Hooks1 qw(hook1b);

hook1b(@ARGV);
__END__

(I might even omit the strictures and warnings in this case ...)

	hp


-- 
   _  | Peter J. Holzer    | Deprecating human carelessness and
|_|_) | Sysadmin WSR       | ignorance has no successful track record.
| |   | hjp@hjp.at         | 
__/   | http://www.hjp.at/ |  -- Bill Code on asrg@irtf.org


------------------------------

Date: Sat, 7 Apr 2012 13:46:46 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Running multiple scripts
Message-Id: <fV45K0OBJxbE-pn2-qsKzC8oNCHJ7@localhost>

On Sat, 7 Apr 2012 13:22:11 UTC, Willem <willem@toad.stack.nl> wrote:

> Dave Saville wrote:
> ) On Sat, 7 Apr 2012 11:59:39 UTC, "Dave Saville" <dave@invalid.invalid>
> ) wrote:
> )
> )<snip>
> )
> ) A bit of Googling turns up "do" which is yet another perl thing I 
> ) didn't know about :-) But although it works once I don't get control 
> ) back if the invoked script does not fail.
> 
> The script you called probably has an 'exit' in there somewhere, which
> causes your master script to exit.

You are correct - I had just figured it out. Bother as I need multiple
return values and "exit value" was the easy way and as it's a "main" 
script it won't let me use "return value" :-(

So a complete restructure to exit at the end of the program by 
assigning the return code required to another variable as the last 
statement rather than short circuiting (and reducing the indentation 
{}'s ) by sticking an "exit n" on an conditional in the middle.  
 
Or, following Willem's advice, rewriting the lot as .pm's - which I 
wanted to avoid as that flies in the face of needing to be able to use
the scripts individually.
-- 
Regards
Dave Saville


------------------------------

Date: Sat, 7 Apr 2012 15:20:35 +0000 (UTC)
From: tmcd@panix.com (Tim McDaniel)
Subject: Re: Running multiple scripts
Message-Id: <jlpm03$lkj$1@reader1.panix.com>

In article <fV45K0OBJxbE-pn2-qsKzC8oNCHJ7@localhost>,
Dave Saville <dave@invalid.invalid> wrote:
>On Sat, 7 Apr 2012 13:22:11 UTC, Willem <willem@toad.stack.nl> wrote:
>
>> Dave Saville wrote:
>> ) On Sat, 7 Apr 2012 11:59:39 UTC, "Dave Saville" <dave@invalid.invalid>
>> ) wrote:
>> )
>> )<snip>
>> )
>> ) A bit of Googling turns up "do" which is yet another perl thing I
>> ) didn't know about :-) But although it works once I don't get
>> control ) back if the invoked script does not fail.
>> 
>> The script you called probably has an 'exit' in there somewhere,
>> which causes your master script to exit.
>
>You are correct - I had just figured it out. Bother as I need multiple
>return values and "exit value" was the easy way and as it's a "main" 
>script it won't let me use "return value" :-(

Well, it's possible to get halfway there by wrapping the top-level
code (the stuff outside any sub bodies) in
    sub init {
    }
Then you can do a "return".  But that's only halfway, ...

>So a complete restructure to exit at the end of the program by
>assigning the return code required to another variable as the last
>statement

 ... because main programs don't work the way subs work: as "man
perlrun" says as of Perl 5.14:

    If the program runs off the end without hitting an exit() or die()
    operator, an implicit exit(0) is provided to indicate successful
    completion.

Unless you're running these scripts hundreds of times per second, or
the machine is really old and slow, I'd suck it up and just invoke
them as regular programs.  Except in those cases, any inefficiency
would be insignificant.  "Micro-optimization leads to micro-results".

Or follow Peter J. Holzer's advice in the other direct reply to you
and put the guts into real libraries.

Or, if you still want the current structure, then (untested) in each
of the standalone scripts, head each with
    package SomethingUniqueToThisFile;
to try to reduce the global namespace pollution, and invoke them as
(untested)
    eval {
        local @ARGV = (...);
        do "the_script";
    };
and also check its exit status, $@, and $!, as mentioned in the do doc
in perlfunc.

But each script can still pollute the global namespace.  If one of
them sets $|, say, or some other special variable, your caller and
everything it calls afterwards will see it.

-- 
Tim McDaniel, tmcd@panix.com


------------------------------

Date: Sat, 07 Apr 2012 14:07:24 -0400
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Running multiple scripts
Message-Id: <87hawvpg9v.fsf@stemsystems.com>

>>>>> "DS" == Dave Saville <dave@invalid.invalid> writes:

  DS> Or, following Willem's advice, rewriting the lot as .pm's - which I 
  DS> wanted to avoid as that flies in the face of needing to be able to use
  DS> the scripts individually.

there are at least two easy ways to do that. first, make module files
and then also make some very short wrapper scripts that load and run
them. then you can call the modules from other programs or run them
standalone from the wrappers. that is how i would go about it. the other
way is to make the module files both loadable and executable. you can
tell if the module was loaded by checking caller (a top level script
will not have any caller data). so put the main line code in an if block
checking caller. this code is the same as the wrapper script i mentioned
before. here is some pseudo code for that:

package Foo ;

use base Exporter ;
our @EXPORT = qw( my_module_code ) ;

unless ( caller() ) {

# you can mung @ARGV first if needed

	my_module_code( @ARGV ) ; 

explicit exit since i like them. no harm with this one

	exit() ;
}

sub my_module_code {

	# your stuff here
}

1 ;


uri


------------------------------

Date: Sat, 7 Apr 2012 20:29:45 +0000 (UTC)
From: "Dave Saville" <dave@invalid.invalid>
Subject: Re: Running multiple scripts
Message-Id: <fV45K0OBJxbE-pn2-E3fK6bSHYLwV@localhost>

<snip>

When I started looking deeoer it turned out that sorting the "exits" 
out was not that hard after all. 

I now have it working with "do" and the scripts can still be used 
standalone. Result :-)

Thank you all for your advice.

-- 
Regards
Dave Saville


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 3660
***************************************


home help back first fref pref prev next nref lref last post