Putting code in a variable and running interactively?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jimmy Phillips

    Putting code in a variable and running interactively?

    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ürgen Exner

    #2
    Re: Putting code in a variable and running interactively?

    Jimmy Phillips wrote:[color=blue]
    > 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;[/color]

    perldoc -f eval

    jue


    Comment

    • nobull@mail.com

      #3
      Re: Putting code in a variable and running interactively?

      Jimmy Phillips <sendto@spammer s.com> wrote in message news:<pan.2004. 08.25.01.44.53. 843246@spammers .com>...[color=blue]
      > I am fairly new to Perl so I wasn't real sure how to title this.[/color]

      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.
      [color=blue]
      > So... Is it possible to put a legimate line of Perl code in a variable,
      > then (somehow) execute that variable.[/color]

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

      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.

      Comment

      Working...