newperlbie has asked for the wisdom of the Perl Monks concerning the following question:

Good Morning Everyone! I am generating a HTML page from my perl script.My requirement is to set a different background color to rows depending on the "IF" conditions they satisfy.I have the following code:
while (my $line = <$IN>) { if ( $line =~ /.*MATCH.*/){ #$ret .= "<tr>$line</tr>\n"; ###This is defined separately an +d it works,but the color is overwritten when I write next set of line +s $ret .= <div style="background-color: orange">$line</div>\n; } $ret .= " </tr>\n\n"; } close $IN; return $ret;
I get this error :

Bareword found where operator expected at trynew.pl line 105, near ""

$line
\n"" syntax error at trynew.pl line 105, near ""<div style="background" Execution of trynew.pl aborted due to compilation errors.

Please help,what is the correct syntax

Replies are listed 'Best First'.
Re: Conditional color formatting ---- HTML
by talexb (Chancellor) on Aug 22, 2018 at 14:54 UTC

    It looks like you are implementing the beginnings of a templating system. As you've probably guessed, this isn't the first time someone's thought about doing this.

    I've have very good success with Template::Toolkit as a templating system. The advantage of doing this is that it separates the presentation of the output from the business logic within the script.

    In your case, you would pass the information in to the template, and then use logic in the template (example below) to modify the display of the data.

    [% IF line.match('MATCH') %] <div style="background-color: orange"> [% ELSE %] <div> [% END %] [% line %] </div>
    See this page for information on how I wrote this example.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Conditional color formatting ---- HTML
by hippo (Archbishop) on Aug 22, 2018 at 14:42 UTC
    $ret .= <div style="background-color: orange">$line</div>\n;

    That's your bareword right there. Try something like this instead:

    $ret .= qq#<div style="background-color: orange">$line</div>\n#;

    And before you go too far down this whole route please consider CSS for styling and maybe even a simple templating system. You'll be glad you did (on both counts).

      Thanks! I did so with CSS
Re: Conditional color formatting ---- HTML
by LanX (Saint) on Aug 22, 2018 at 14:42 UTC
    > $ret .=  <div style="background-color: orange">$line</div>\n;

    HTML is not Perl, you have to doublequote the string.

    qq{..} would work right away, ".." would require changing the inner quotes to single ones ( " -> ')

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Conditional color formatting ---- HTML
by Anonymous Monk on Aug 22, 2018 at 22:54 UTC
    # My requirement is to set a different background color # to rows depending on the "IF" conditions they satisfy. # Please help,what is the correct syntax while (my $line = <$IN>) { if ($line =~ /.*MATCH.*/) { $ret .= "<tr><td style='background-color:orange'>$line </td></tr>\ +n"; } else { $ret .= "<tr>$line </tr>\n"; } } close $IN; return $ret;
      Oops typo, was missing td after else.
      # My requirement is to set a different background color # to rows depending on the "IF" conditions they satisfy. # Please help,what is the correct syntax while (my $line = <$IN>) { if ($line =~ /.*MATCH.*/) { $ret .= "<tr><td style='background-color:orange'>$line </td></tr>\ +n"; } else { $ret .= "<tr><td>$line </td></tr>\n"; } } close $IN; return $ret;