Putting code in a variable and running interactively?

J

Jimmy Phillips

I am fairly new to Perl so I wasn't real sure how to title this.

I want to build a regex interactively in a program and then run it. So
far I do it by writing it to a file along with the Perl envelope around it
then calling it from my program. Works ok but seems clunky. Then I found
a tutorial about executing perl code in a regular expression but so far it
hasn't quite worked the way I want.

So... Is it possible to put a legimate line of Perl code in a variable,
then (somehow) execute that variable. Sort of like so...

$z = "Print amount";
(execute) $z;

Or put another way. Build program B within program A then run program B
within the original program A?

If this was a standard programming environment like Builder or Kylix I
wouldn't even ask such a rediculous question, but in my so far brief
experience with Perl I haven't found much it won't to.

JP
 
J

Jürgen Exner

Jimmy said:
So... Is it possible to put a legimate line of Perl code in a
variable, then (somehow) execute that variable. Sort of like so...

$z = "Print amount";
(execute) $z;

perldoc -f eval

jue
 
N

nobull

Jimmy Phillips said:
I am fairly new to Perl so I wasn't real sure how to title this.

Actually I would put your subject line in the top 5%. Good subject
lines are what make Usenet a really useful searchable resource. At
least they are when you post to newsgroups that actually exist.
So... Is it possible to put a legimate line of Perl code in a variable,
then (somehow) execute that variable.

Jürgen has answered that but I'm not sure if you really need to.
I want to build a regex interactively in a program and then run it.

What do you mean by "run it". You don't run regular expressions, you
use them in m// or s/// or split().

You can use a calcualated regex directly in a m// or s/// or split() -
no need to resort to evi^Hal.

This newsgroup does not exist (see FAQ). Please do not start threads
here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top