[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] syntax barriers?




On Sun, 13 May 2001, Michael G Schwern wrote:
> On Sat, May 12, 2001 at 10:05:39AM -0700, Marc Lehmann wrote:
> > Can you improve the "[]" solution or even find the perfect "syntax/error
> > barrier"?
> 
> Write the user code out to a seperate file and use 
> 
>         eval{ do($user_file) };
>         warn $@ if $@;

Or you could just use string eval and heredocs:

eval <<'STRING_THAT_IS_NOT_A_LINE_IN_USER_CODE'; die $@ if $@;
#line 1 "usercode"
...
STRING_THAT_IS_NOT_A_LINE_IN_USER_CODE

Actually, you don't even need to use a heredoc.  Just put the user code
in single quotes after doing s/([\\'])/\\$1/g on it.  The whole thing
would look something like:

  my $code = "";  # insert wrapper code as needed
  foreach my $file (@user_files) {
      my $usercode = qq(#line 1 "$file"\n) . preprocess(readfile($file));
      $usercode =~ s/([\\'])/\\$1/g;
      $code .= "eval '$usercode'; die $@ if $@;
  }
  eval $code; die $@ if $@;

Of course, if you can rewrite your program to eval each file of user
code separately, that's even better.

(Random observation: To be really careful, remember to strip linefeeds
and double quotes from filenames before using them in #line comments..)

-- 
Ilmari Karonen - http://www.sci.fi/~iltzu/
"Signal handlers are known to be broken in perl.  Did you expect them to
 somehow get unbroken when ITHREADS is enabled?"
                           -- Gurusamy Sarathy on the perl5-porters list



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe