Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Running Perl in an Expect loop

by StoneLeopard (Novice)
on Dec 15, 2016 at 13:59 UTC ( [id://1177839]=note: print w/replies, xml ) Need Help??


in reply to Re: Running Perl in an Expect loop
in thread Running Perl in an Expect loop

Hi Hauke, what I am trying to build is a preprocessor for a format that has embedded free-form Perl within <% ... %> limiters. This in itself is easy since I can simply open(|perl) and send stuff to it, but the second quirk is that there is also a second level of preprocessor macros following Verilog convention (e.g. `include FILE). The corner case that is hard to handle is `include <%$file%> which implies that I need to first send it through Perl, then include the file, which may internally have more Perl :( This is why I was trying to set up a bidirectional link to a Perl process. I'll look at some of the other suggestions in the thread. Thanks Pankaj

Replies are listed 'Best First'.
Re^3: Running Perl in an Expect loop
by Corion (Patriarch) on Dec 15, 2016 at 14:04 UTC

    Many of the templating systems can do the replacement for you. Why do you want to talk to a separate Perl process at all if there is eval?

    # First stage: sub process_perl { my( $code ) = @_; my $res = eval $code; my $err = $@; warn "Whoops. Caught error [$err] for code [$code]" if $err; return $res; }; $template =~ s!<%([^%]+)%>!process_perl("$1")!gse;

    If you want/need to do a second round of replacements then, do it afterwards:

    $template =~ s!\binclude\b ([^\s]+)!process_include("$1")!gse;
      The issue is that the file syntax is *not* Perl, i.e. there can be things like this:
      CHAIN<%=$chain_num%>_EN [<%=$lsb%>:<%=$lsb++%>] = 1'h0;

      Even the format spec document suggests this approach "wrap all non-Perl chunks with prints, leaving the code snippets as is, and let Perl evaluate the whole thing".

      The reason a simple eval() doesn't work is because there is Perl variable state that needs to be honored across the entire file (e.g. $lsb in the example above is set to 0 at the start and incremented a number of times through the file. Hence, we will need a full Perl process to evaluate the whole file.

      In any case, after understanding the REPL issue, I broke up the processing into 2 sections:

      * send the entire file to the Perl child process, and close the child's input filehandle

      * then read back the generated output in a while loop

      * if we encounter an "include" statement, then recursively restart the process with the new file.

      This works perfectly. Thanks for all the suggestions.

      Pankaj

        Hi StoneLeopard,

        The reason a simple eval() doesn't work is because there is Perl variable state that needs to be honored across the entire file (e.g. $lsb in the example above is set to 0 at the start and incremented a number of times through the file. Hence, we will need a full Perl process to evaluate the whole file.

        I don't follow this argument as a reason for needing a separate process. The state of package variables (those declared with our, or if no strict 'vars' is in effect, any undeclared variables) is honored across evals:

        $ perl -le 'eval q{ print ++$x }; eval q{ print ++$x }' 1 2

        If you had said that you don't want code from evaling one file to affect code from another evaled file, that would be a slightly different story. There are a few Perl modules, pragmas and variables which affect everything in the interpreter process, so if those are used, I would agree that yes, the best way to go is starting up a new interpreter for evaluating each file. But other than that, most Perl code can be compartmentalized in a package just fine. A package within its own block would hold lexicals, package variables, subroutine definitions, pragmas with lexical scopes such as strict and warnings, and in most cases subroutines imported from used modules, and all those would be kept separate from other packages. And special variables can and should be localized.

        The suggested approach "wrap all non-Perl chunks with prints, leaving the code snippets as is, and let Perl evaluate the whole thing" makes sense to me. And just to show that it can be done all within a single Perl process with eval, here's a simple implementation of exactly that. Doing a second round of replacements, as Corion said, is just a matter of applying the same idea a second time.

        File a.txt:

        CHAIN<%=$chain_num%>_EN [<%=$lsb%>:<%=$lsb++%>] = 1'h0; <% use File::Spec::Functions qw/catfile/ %>

        File b.txt:

        <%$lsb++%> <% for my $x (0..2) { %> Hello, World Nr. <%=$x%> (LSB=<%=$lsb%>) <% } warn "test warning" %> $this is not $Perl <% # note catfile() not available here %>

        The script:

        Output of running perl script.pl a.txt b.txt:

        >>>>> Output: CHAIN_EN [:0] = 1'h0; <<<<< test warning at Generated2 line 8. >>>>> Output: Hello, World Nr. 0 (LSB=1) Hello, World Nr. 1 (LSB=1) Hello, World Nr. 2 (LSB=1) $this is not $Perl <<<<<

        Note how the two files each have their own $lsb variable.

        Of course, I've just re-invented the wheel (for fun, so it's okay :-) ). See for example Embperl.

        Hope this helps,
        -- Hauke D

        Update: Added info on special variables.

        Update 2018-03-10: Posted a modified version of the above as this StackOverflow answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1177839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (2)
As of 2024-04-25 18:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found