Mailing List Archive

Re: OpenGL programming using PERL
In article <STAN.95Sep13020300@skyler.arc.ab.ca>,
Stan Melax <stan@skyler.arc.ab.ca> wrote:
>
> > I hope you've read all the notes on Module creation in the Module List.
> Yes, and I found it very useful.
>
Thanks (please send me an entry for the OpenGL module sometime soon).

> > > Of course there may be
> > > some differences between C and PERL, for example:
> > > C: glBegin( GL_POLYGON);
> > > PERL: glBegin($GL_POLYGON);
> >
> > In Perl5 the standard way to define constants is with an exported function.
> > Take a look at the Fcntl module for a simple example. That way your code is
> >
> > C: glBegin(GL_POLYGON);
> > PERL: glBegin(GL_POLYGON);
> >
> One little thing I dont like about that is when you do something like:
> print( GL_POLYGON, "\n")
> and it assumes GL_POLYGON is a file thing instead of a subroutine call.
> But I realize that in most contexts (besides debugging with print statements)
> in which a constant is used there will not be such an ambiguity.
>

$ perl
sub GL_POLYGON { 42 };
print(GL_POLYGON, "\n");
^D
No comma allowed after filehandle at - line 2.

Oh dear! CC'd to perlbug@perl.com.

Meanwhile you can use &GL_POLYGON or GL_POLYGON+0 as a workaround.

> There are lots and lots of constants to be defined. would there be
> performance/memory problems with a subroutine for each?
>
POSIX define *lots* of constants. The POSIX module uses an AUTOLOADer
to only define a constant when it's first used.

As for performance, yes, $FOO is much faster than FOO (&FOO). Currently.
It's possible that in the next release of Perl (or the one after)
functions which are constant will be recognised and optimised to
avoid the function call overhead.

> Which would a perl programmer prefer $CONSTANT or CONSTANT?
> He/she is probably used to using $'s in statements like for($i=0;$i<n;$i++).
>
Used to using $ for _variables_. h2ph convert #defines to functions
to avoid the need to use $ and to protect the value from being modified.

> Is the best solution be to offer both options?
> Would that create confusion and make it harder for someone to
> read someone else's code?
> Which style should the examples use? '$' or no '$'
>
If you really want to offer both then don't use '$'s in the examples.
Just state that $ versions can be used if needed for performance in
tight loops. (Also you should define the $ constants in XS code and
apply SvREADONLY_on() to them.)

> > > Ideally we want to generate most of the module (xsubs, typemap, etc.)
> > > from a well written perl script that takes /usr/include/GL/gl.h
> > > (and glx.h and glxtokens.h) as input.
> > >
> > Great. Using a MakeMaker Makefile.PL will make that much easier.
>
> We're trying to build (or at least get a good and proper start)
> on a module that the world will be able to use instead of
> just hacking together something that meets our own short
> term requirements.
>
Wonderful. OpenGL is becoming an important standard.

> Stan
>
Tim.