Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help -- how to execute "computed Perl code"

Reply
Thread Tools

Help -- how to execute "computed Perl code"

 
 
Steve D
Guest
Posts: n/a
 
      09-02-2003
My code dynamically creates a scalar with a text string that is a
valid Perl code line.

How can I get Perl to execute the line contained in that scalar?

I could write it to a temp file and then "do <file>", but I want to
avoid that overhead. It does not seem eval and "do BLOCK" are the
answer.

Any good solutions?

Regards,
Steve D

Basic idea:

$x = "Send A message 13 ;" ;
# how can I execute $x to call (defined in a different module) the
subroutine:
# Send ($;@) {...}

Just putting
Send A message 13 ;
at the point of interest does run "Send", so visibility is not a
problem, at least at that point.
 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      09-02-2003
On 1 Sep 2003 18:41:31 -0700
http://www.velocityreviews.com/forums/(E-Mail Removed) (Steve D) wrote:

> My code dynamically creates a scalar with a text string that is a
> valid Perl code line.
>
> How can I get Perl to execute the line contained in that scalar?
>
> I could write it to a temp file and then "do <file>", but I want to
> avoid that overhead. It does not seem eval and "do BLOCK" are the
> answer.
>
> Any good solutions?


Not sure if this i what you're looking for or not:

==untested==
#!/usr/bin/perl -w
use strict;
#create a reference to a subroutine
#execute the system command 'ls' (Windows, it would be 'dir')
my $code = sub { system("ls") };
#call to the subroutine
$code->();
==untested==

You can apply this idea to whatever it is that you're doing.

HTH
Jim
---
Copyright notice: all code written by the author in this post is
considered GPL. http://gnu.org for more information.
---
a real quote ...
Linus Torvalids: "They are somking crack ...."
(http://www.eweek.com/article2/0,3959,1227150,00.asp)
---
a fortune quote ...
Surprise your boss. Get to work on time.

 
Reply With Quote
 
 
 
 
James Willmore
Guest
Posts: n/a
 
      09-02-2003
On Tue, 02 Sep 2003 02:54:28 GMT
James Willmore <(E-Mail Removed)> wrote:
> On 1 Sep 2003 18:41:31 -0700
> (E-Mail Removed) (Steve D) wrote:
> > My code dynamically creates a scalar with a text string that is a
> > valid Perl code line.
> > How can I get Perl to execute the line contained in that scalar?
> > I could write it to a temp file and then "do <file>", but I want
> > to avoid that overhead. It does not seem eval and "do BLOCK" are
> > the answer.
> > Any good solutions?

>
> Not sure if this i what you're looking for or not:
>
> ==untested==
> #!/usr/bin/perl -w
> use strict;
> #create a reference to a subroutine
> #execute the system command 'ls' (Windows, it would be 'dir')
> my $code = sub { system("ls") };
> #call to the subroutine
> $code->();
> ==untested==
>
> You can apply this idea to whatever it is that you're doing.


After I posted, I got to thinking (uh-oh). If you want to see other
examples of what I posted, take a look at the CGI module. Most, if
not all, the code is done the way you (I think) would like to do.

HTH
--
Jim
---
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
---
a real quote ...
Linus Torvalids: "They are somking crack ...."
(http://www.eweek.com/article2/0,3959,1227150,00.asp)
---
a fortune quote ...
Too often I find that the volume of paper expands to fill the
available briefcases. -- Governor Jerry Brown
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-02-2003
Steve D <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> My code dynamically creates a scalar with a text string that is a
> valid Perl code line.
>
> How can I get Perl to execute the line contained in that scalar?
>
> I could write it to a temp file and then "do <file>", but I want to
> avoid that overhead. It does not seem eval and "do BLOCK" are the
> answer.


"eval" is the answer, in the string-eval form. What have you tried?

Anno
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-02-2003
(E-Mail Removed) (Steve D) writes:

> My code dynamically creates a scalar with a text string that is a
> valid Perl code line.


This is a powerful technique but one that should only be used in very
special circumstances.

Unlike some of the regulars here I'm not gripped by a rabid fear of
this technique, indeed I embrace it. But before you use it you must
understand how parsing data as code can easily produce obscure hidden
bugs or security holes.

> How can I get Perl to execute the line contained in that scalar?
>
> It does not seem eval [is] the answer.


Why? (Oh and don't forget #line)

> Any good solutions?


Dunno. The solution you have rejected (for no apparent reason) is the
solution to your "Y" but there's a fairly good chance it's not a good
solution to your "X"[1].

Since you do not tell us anything about your "X" we can't advise on a
good solution.

[1] An "XY" problem is where you want to do "X" but ask "How do I do
Y?" because you think that doing "Y" is part of doing "X" but actually
doing "Y" is something more complex/riskly/whatever than the direct
solution to doing "X".

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 
Reply With Quote
 
Steve D
Guest
Posts: n/a
 
      09-02-2003
James Willmore <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) m>...
> On Tue, 02 Sep 2003 02:54:28 GMT


NOPE. Your "solution" requires the code text to be known at COMPILE
time.

I need to compile code text that has been create at RUN time. The
following is a complete program example of what I want to do (the
@code_text is just a simple way to define strings -- in reality they
are composed from much more complex circumstances).

As I said, I *could* write @code_text to a file, then "do <file>" to
get the effect I need, but want to avoid writing and reading a file
just to get evaluation of the code.

Regards,
Steve

#! /bin/perl
use strict ; use warnings ;

my $a = 1 ;
my @code_text = ( '$a += 3 ;', '$a /= 2 ;', 'print $a ; ' ) ;

for my $code_text ( @code_text ) {
my $code = sub { $code_text } ;
print "$code_text\n" ;
# DOES NOT WORK
# I want to evaluate the "code" contained in $code_text
# NOT the "value" of $code_text. $a should get 3 added,
# then be divided by 2, and then printed.
$code->() ;
} ;

print "$a should be 2 ( 1 plus 3 divided by 2 )\n"
 
Reply With Quote
 
Steve D
Guest
Posts: n/a
 
      09-02-2003
James Willmore <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) m>...
> On Tue, 02 Sep 2003 02:54:28 GMT
> James Willmore <(E-Mail Removed)> wrote:
> > On 1 Sep 2003 18:41:31 -0700
> > (E-Mail Removed) (Steve D) wrote:
> > > My code dynamically creates a scalar with a text string that is a
> > > valid Perl code line.


James,
Your code gave me some ideas. Some more experimentation did finally
come up with a solution:

#! /bin/perl
use strict ; use warnings ;

my $a = 1 ;
my @code_text = ( '$a += 3 ;', '$a /= 2 ;', 'print "$a\n" ; ' ) ;

for my $code_text ( @code_text ) {
my $code = sub { eval "$code_text" } ; # Just need eval to make it
work
print "Executing $code_text\n" ;
$code->() ;
} ;

print "$a should be 2 ( 1 plus 3 divided by 2 )\n"

=================================
The eval in the sub def is the key. I had tried a do of an eval, an
eval of an eval, but never a sub of an eval. That's the key.

Thanks for your help.

Regards,
Steve
 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      09-02-2003
On 2 Sep 2003 05:03:27 -0700, Steve D <(E-Mail Removed)> wrote:
[snippage]

> # I want to evaluate the "code" contained in $code_text

^^^^
And that is what you should do...

(and make sure you understand the risks associated with executing
arbitrary code).

--
Sam Holden

 
Reply With Quote
 
Simon Taylor
Guest
Posts: n/a
 
      09-02-2003
Steve D wrote:
> My code dynamically creates a scalar with a text string that is a
> valid Perl code line.
>
> How can I get Perl to execute the line contained in that scalar?
>
> I could write it to a temp file and then "do <file>", but I want to
> avoid that overhead. It does not seem eval and "do BLOCK" are the
> answer.
>
> Any good solutions?


I won't elaborate on the answers in this thread to date, but simply
mention the 'Safe' module. I've found it to be very handy in some
circumstances when you want to execute arbitrary (dynamically created) code.

It allows you to run code in a reasonably safe 'compartment'.

Regards,

Simon Taylor

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      09-02-2003
>>>>> "BM" == Brian McCauley <(E-Mail Removed)> writes:

BM> Unlike some of the regulars here I'm not gripped by a rabid fear of
BM> this technique, indeed I embrace it. But before you use it you must
BM> understand how parsing data as code can easily produce obscure hidden
BM> bugs or security holes.

then you misunderstand why we always say it is evil. i have used string
eval but only as a last resort or where is provides a distinct
advantage. most newbies hear about it and start to use it for every nail
they see when they can usually use perl structures or other concepts
instead of eval. so the mantra here is don't use it (unless you know why
you are using it).

in this thread i still have no clear understanding of the problem
itself, let alone why eval is needed or even better, why it didn't work
at all.

BM> Dunno. The solution you have rejected (for no apparent reason) is the
BM> solution to your "Y" but there's a fairly good chance it's not a good
BM> solution to your "X"[1].

i smell XY problem as well. poorly articulated problems with eval as a
potential (and rejected!) solution reeks of XY.

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
Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
CGI: Execute a perl script inside another perl script xdarcos@hotmail.com Perl Misc 20 01-18-2005 12:33 PM
Execute another perl script from my perl script Petterson Mikael Perl Misc 3 01-05-2005 01:31 PM
Can i execute aspx file in my asp pag with server.execute method(sorry) Savas Ates ASP General 1 08-17-2004 04:52 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments