Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Checking the syntax of Perl code

Reply
Thread Tools

Checking the syntax of Perl code

 
 
Samuel
Guest
Posts: n/a
 
      05-18-2007
Hi,

I have written a code generator (in Perl) that produces Perl code and
would like to throw an assertion if the result contains invalid
syntax. Any idea if there is a way to perform a syntax check that does
not involve firing up a new process with "perl -c" or eval?

-Samuel

 
Reply With Quote
 
 
 
 
Tim Southerwood
Guest
Posts: n/a
 
      05-18-2007
Samuel wrote:

> Hi,
>
> I have written a code generator (in Perl) that produces Perl code and
> would like to throw an assertion if the result contains invalid
> syntax. Any idea if there is a way to perform a syntax check that does
> not involve firing up a new process with "perl -c" or eval?
>
> -Samuel


Is there a problem with using eval()?

Tim
 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      05-18-2007
On 18 May 2007 06:49:50 -0700, Samuel <(E-Mail Removed)> wrote:

>Subject: Checking the syntax of Perl code


perl -c

>I have written a code generator (in Perl) that produces Perl code and
>would like to throw an assertion if the result contains invalid
>syntax. Any idea if there is a way to perform a syntax check that does
>not involve firing up a new process with "perl -c" or eval?


D'Oh! However... no, I don't think so, since "nothing but perl can
parse Perl". However eval() does not fire up a new process, that I
know.

wolfgang:~ [16:24:15]$ perl -e 'eval q{system "ps x"}'
PID TTY STAT TIME COMMAND
18527 ? S 0:01 sshd: blazar@pts/4
18528 pts/4 Ss 0:01 -bash
10213 pts/4 S+ 0:00 perl -e eval q{system "ps x"}
10214 pts/4 R+ 0:00 ps x


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-18-2007
>>>>> "S" == Samuel <(E-Mail Removed)> writes:

S> I have written a code generator (in Perl) that produces Perl code and
S> would like to throw an assertion if the result contains invalid
S> syntax. Any idea if there is a way to perform a syntax check that does
S> not involve firing up a new process with "perl -c" or eval?

how were you planning on actually compiling this code? if you want it
inside this process you have to use eval STRING. note that any code
outside of subs will be executed. externally you can run perl -c and it
won't execute main level code but use commands will be run and those
modules can execute code. so there really is no way to just test for
perl syntax all by itself.

if you want to see how generated code is evaled and used, check out
Sort::Maker on cpan. it is a very rare problem that needs code
generation and it can be tricky doing it right. why do you think you
need code gen for your problem? i saw one case where someone generated
code for class accessors when closures would have been simpler and
better.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Samuel
Guest
Posts: n/a
 
      05-18-2007
On May 18, 4:16 pm, Tim Southerwood <(E-Mail Removed)> wrote:
> Is there a problem with using eval()?


Well, maybe I made an incorrect assumption there - is there a way to
use eval for checking the syntax *without executing the code* that I
am not aware of?

There is no problem with eval per-se (in fact, I do compile the code
using eval at a later time in the program), but I would like to catch
syntax errors sooner.

-Samuel

 
Reply With Quote
 
Samuel
Guest
Posts: n/a
 
      05-18-2007
On May 18, 6:11 pm, Uri Guttman <(E-Mail Removed)> wrote:
> how were you planning on actually compiling this code? if you want it
> inside this process you have to use eval STRING. note that any code
> outside of subs will be executed.


Ahh, you may be onto something here, I guess wrapping this into a
function should do the trick. I'll try this when I'm back in the
office on Monday.

> why do you think you
> need code gen for your problem?


We are using a highly specialized template language for controlling
appliances, which we compile into Perl code. There are probably other
ways to do this, but none that were similarly easy and straight
forward to do.

Thanks a lot for your help, Guys!

-Samuel

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      05-18-2007
>>>>> "S" == Samuel <(E-Mail Removed)> writes:

>> why do you think you
>> need code gen for your problem?


S> We are using a highly specialized template language for controlling
S> appliances, which we compile into Perl code. There are probably other
S> ways to do this, but none that were similarly easy and straight
S> forward to do.

that is definitely not the best (nor easiest) way to do
templating. there are (too) many templating modules already out there. i
am sure one of them does what you want and it will likely be faster and
better.

and what you may call a highly specialized template language, someone
else may call a trivial feature already implemented. so post some of
this template language and i bet you will be able to save a lot of
coding when you get answers to your real problem (not the eval
question).

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Samuel
Guest
Posts: n/a
 
      05-18-2007
On May 18, 8:24 pm, Uri Guttman <(E-Mail Removed)> wrote:
> that is definitely not the best (nor easiest) way to do
> templating. there are (too) many templating modules already out there.


Alright, I'll bite. The template looks like any other template
language:

{if device.vendor() is "cisco"}
show conf {extract /^interface (\S+)\.(\d+)/ as interfaces, units}
{loop interfaces as interface}
show interfaces {$interface} controller
{end}
{end}

I'm not in the office, but this is a simplified version of the
compiled template:

if ($device->vendor() eq "cisco") {
my @response = $stream->send("show conf");
my @interfaces;
my @units;
for (@response) {
next unless /^interface (\S+)\.(\d+)/;
push(@interfaces, $1);
push(@units, $2);
}
for my $interface (@interfaces) {
my @response = $stream->send("show interfaces $interface
controller");
}
}

(I skipped the included error handling, locking for parallelization
via threads, and some more.) This took less than a day to fully
implement.

Yes, some template languages are somewhat flexible and allow you to
register your own functions, but that's really not good enough. Also,
I believe that parsers and lexers do not really gain you a lot when
parsing this. The code isn't open sourced yet (it will be soon), but I
bet it will be hard to implement this with significantly less LOC
(approx. 350 currently), even by introducing new dependencies. Either
way, the time it took to write this is probably even less than the
time it would take to research any other template language.

-Samuel

 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      05-18-2007
>>>>> "Samuel" == Samuel <(E-Mail Removed)> writes:

Samuel> Well, maybe I made an incorrect assumption there - is there a way to
Samuel> use eval for checking the syntax *without executing the code* that I
Samuel> am not aware of?

Not absolutely.

There are definitely Perl constructs that cannot be parsed (even by
/usr/bin/perl) without having executed *some* Perl in the process.

Parsing Perl as a static document is impossible in the general case.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-18-2007
Samuel <(E-Mail Removed)> wrote:
> On May 18, 4:16 pm, Tim Southerwood <(E-Mail Removed)> wrote:
> > Is there a problem with using eval()?

>
> Well, maybe I made an incorrect assumption there - is there a way to
> use eval for checking the syntax *without executing the code* that I
> am not aware of?


If the code doesn't use BEGIN or END blocks of its own, or their moral
equivalents, then you could do something like this:

eval $whatever . q{;BEGIN {die "reached end\n"}};
unless ($@ eq "reached end\n") {
Something_is_wrong_with($whatever, $@);
};


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A different syntax for error checking Richard Bos C Programming 10 01-18-2008 06:00 PM
Checking for valid number syntax Count Dracula C Programming 9 12-22-2006 12:16 PM
Komodo syntax checking for python2.4 George Sakkis Python 2 04-26-2005 10:00 PM
Python switch for syntax checking Jeff Duffy Python 6 11-19-2004 11:55 PM
Checking XML DTD syntax and validating XML Clifford W. Racz XML 8 06-23-2004 08:12 PM



Advertisments