t-rex has asked for the wisdom of the Perl Monks concerning the following question:

hello

I have a template from which I extract few parameters by using perl regex as follows

template 1: <%= my $capi = ''; if (@{$ctxt{'capi_devices'}}){ $capi = 'afu_op=grpcapp' . "\n" . 'capp=1' . "\n" . 'icnt=200' . "\n" . 'capi_dev=' . join(' ', @{$ctxt{'capi_devices'}})."\n". 'capi_cards=' . @{$ctxt{'capi_devices'}}; } $capi; %> <%= my $marega = ''; if (@{$ctxt{'test'}}){ $marega = 'splthid=' . join(' ' ,@spthid_a). "\n" . 'splthjobops=' . join(' ', @{$ctxt{'test'}}). "\n" ; } $marega; %>

Now I have a generic function which processes this template ( it has many parameters but avoiding for the sake of simplicity ), the problem is when i try to extract the $capi some how it gets bypassed and I get the $marega. Here is the code for processing template

while($inp =~ /<%=(.*?)%>/s) { print "Dollar 1 before eval = $1\n"; $inp =~ s/<%=(.*?)%>/eval $1/es; print"INP after substitution = $inp\n"; } # only the concerned part of code with this extraction is written here

the output i get here is

Dollar 1 before eval = my $capi = ''; if (@{$ctxt{'capi_devices'}}){ $capi = 'afu_op=grpcapp' . "\n" . 'capp=1' . "\n" . 'afusched=1 1 1 0 0 0 0 0' . "\n" . 'afu_type=1 1 1 0 0 0 0 0' . "\n" . 'icnt=200' . "\n" . 'capi_dev=' . join(' ', @{$ctxt{'capi_devices'}})."\n". 'capi_cards=' . @{$ctxt{'capi_devices'}}; } $capi; INP after substitution = splthid=7 splthjobops=0 2 1 1 4 0 2 6 0 3 8 0 4 0 0 # here i expect the $1 contents after evaluation to come that is $capi + should be printed but instead $marega is executed.

i hope i can convey what problem i am facing by this writeup, thanks

Replies are listed 'Best First'.
Re: Regex error in the second last string
by haukex (Archbishop) on Feb 06, 2017 at 09:39 UTC

    Hi t-rex,

    I can't reproduce the issue with the code you showed. I tested with both the original code you posted (with dummy values inserted for the missing variables) and the following boiled down version.

    my $inp = <<'END'; <%= my $capi = 'foo'; $capi; %> <%= my $marega = 'bar'; $marega; %> END while($inp =~ /<%=(.*?)%>/s) { print "Dollar 1 before eval = $1\n"; $inp =~ s/<%=(.*?)%>/eval $1/es; print "INP after substitution = $inp\n"; } __END__ Dollar 1 before eval = my $capi = 'foo'; $capi; INP after substitution = foo <%= my $marega = 'bar'; $marega; %> Dollar 1 before eval = my $marega = 'bar'; $marega; INP after substitution = foo bar

    Please try to boil down your problematic code into a Short, Self-Contained, Correct Example that reproduces the problem.

    Perhaps if (@{$ctxt{'capi_devices'}}) is false, and that's why the $capi output is not showing up? (Although based on your sample output that wouldn't fully explain why that code block is skipped - your sample output appears to show only one iteration of the loop.)

    You could try to insert debugging warn statements at strategic places to see what does and doesn't get executed. See also the Basic debugging checklist.

    Regards,
    -- Hauke D

Re: Regex error in the second last string
by NetWallah (Canon) on Feb 06, 2017 at 20:35 UTC
    haukex (++) is right - the evaluation depends on the contents of
    $ctxt{'capi_devices'}
    I was able to get "expected output" after setting:
    $ctxt{capi_devices}=["CAPI DEVICE"];
    The value of $inp (After first iteration) was:
    afu_op=grpcapp capp=1 icnt=200 capi_dev=CAPI DEVICE capi_cards=1 <%= my $marega = \'\'; if (@{$ctxt{\'test\'}}){ $marega = \'splthid=\' . join(\' \' ,@spthid_a). "\\n" . \'splthjobops=\' . join(\' \', @{$ctxt{\'test\'}}). "\\n" ; } $marega; %>

            ...it is unhealthy to remain near things that are in the process of blowing up.     man page for WARP, by Larry Wall