TedYoung has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
In my application framework I have packages that export subroutines. These subroutines are effectively treated as enumerations, however, behind the scenes they lazily load complex data from a database (these are not constants -- no () prototype).
I noticed a parsing problem with Perl:
use strict; use warnings; sub AL_CORP_MIN { 1 }; my $level = 1; if (AL_CORP_MIN < $level) { print 1 };
Gives me the following error:
Unterminated <> operator at index.app line 6.
If I change the code to the following:
use strict; use warnings; sub AL_CORP_MIN { 1 }; my $level = 1; if ($level > AL_CORP_MIN) { print 1 };
It parses and works just fine. I can live with this. However, in the real version the objects returned from these subroutines are overloaded to return numeric values so I often like to express ranges in the following format:
AL_CORP_MIN <= $level && $level <= AL_CORP_MAX
Another situation in which Perl improperly parses subroutine calls is the following:
use strict; use warnings; sub AL_CORP_MIN { 1 }; my $level = 1; my $foo = $level == AL_CORP_MIN ? 'a' : 'b';
Error:
Search pattern not terminated at index.app line 6.
Again, I can work around this with:
my $foo = $level == AL_CORP_MIN() ? 'a' : 'b';
None of these issues arise if the subs were defined as constant -- () prototype and simple bodies. I assume because the definitions are folded in.
My Perl version:
Summary of my perl5 (revision 5 version 8 subversion 8) configuration: Platform: osname=linux, osvers=2.6.22-3-amd64, archname=i486-linux-gnu-threa +d-multi uname='linux deneb 2.6.22-3-amd64 #1 smp thu oct 11 15:23:23 utc 2 +007 i686 gnulinux ' config_args='-Dusethreads -Duselargefiles -Dccflags=-DDEBIAN -Dccc +dlflags=-fPIC -Darchname=i486-linux-gnu -Dprefix=/usr -Dprivlib=/usr/ +share/perl/5.8 -Darchlib=/usr/lib/perl/5.8 -Dvendorprefix=/usr -Dvend +orlib=/usr/share/perl5 -Dvendorarch=/usr/lib/perl5 -Dsiteprefix=/usr/ +local -Dsitelib=/usr/local/share/perl/5.8.8 -Dsitearch=/usr/local/lib +/perl/5.8.8 -Dman1dir=/usr/share/man/man1 -Dman3dir=/usr/share/man/ma +n3 -Dsiteman1dir=/usr/local/man/man1 -Dsiteman3dir=/usr/local/man/man +3 -Dman1ext=1 -Dman3ext=3perl -Dpager=/usr/bin/sensible-pager -Uafs - +Ud_csh -Uusesfio -Uusenm -Duseshrplib -Dlibperl=libperl.so.5.8.8 -Dd_ +dosuid -des' hint=recommended, useposix=true, d_sigaction=define usethreads=define use5005threads=undef useithreads=define usemulti +plicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS +-DDEBIAN -fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE +_SOURCE -D_FILE_OFFSET_BITS=64', optimize='-O2', cppflags='-D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBIAN +-fno-strict-aliasing -pipe -I/usr/local/include' ccversion='', gccversion='4.1.2 20061115 (prerelease) (Debian 4.1. +1-21)', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=1 +2 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', + lseeksize=8 alignbytes=4, prototype=define Linker and Libraries: ld='cc', ldflags =' -L/usr/local/lib' libpth=/usr/local/lib /lib /usr/lib libs=-lgdbm -lgdbm_compat -ldb -ldl -lm -lpthread -lc -lcrypt perllibs=-ldl -lm -lpthread -lc -lcrypt libc=/lib/libc-2.3.6.so, so=so, useshrplib=true, libperl=libperl.s +o.5.8.8 gnulibc_version='2.3.6' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E' cccdlflags='-fPIC', lddlflags='-shared -L/usr/local/lib' Characteristics of this binary (from libperl): Compile-time options: MULTIPLICITY PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP THREADS_HAVE_PIDS USE_ITHREAD +S USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API Built under linux Compiled at Nov 5 2007 06:11:48 @INC: /etc/perl /usr/local/lib/perl/5.8.8 /usr/local/share/perl/5.8.8 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.8 /usr/share/perl/5.8 /usr/local/lib/site_perl .
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parse error with subroutines and <
by chromatic (Archbishop) on Dec 14, 2007 at 19:51 UTC | |
by TedYoung (Deacon) on Dec 14, 2007 at 20:02 UTC | |
by Fletch (Bishop) on Dec 14, 2007 at 20:17 UTC | |
by ikegami (Patriarch) on Dec 14, 2007 at 21:35 UTC | |
|
Re: Parse error with subroutines and <
by Fletch (Bishop) on Dec 14, 2007 at 19:55 UTC |