0

In this part of Perl cgi code $_ variable failed to print its contents inside <td> tag using Perl cgi?

sub do_work {
 my $fh = shift;
 my $content; 
 while ( my $line= <$fh> ) {
 my @name1 = grep {$_ =~ m/^\[/} $line;
 s/\W//g for @name1;
 $content .= join '', @name1;
 }
 return $content;
}
sub do_task {
 my $fh = shift;
 my $load;
 while(my $firstline = <$fh>) {
 $firstline =~ s/.*=//g;
 my @words = split /,., $firstline;
 my $add = 0;
 $load1 .= join("\n", @words); 
 }
 return $load1; 
}
sub layout {
 my ($load, @words = @_;
 my $add = 0;
 print << "EOF";
 <html><body><table>
 <tr>
 <th>s.no</th>
 <th>name</th>
 </tr>
 EOF
 foreach(@words) {
 $add++;
 print<<"EOF"
 <tr>
 <td>$add</td>
 <td>$_</td>
 EOF
 }
 print <<"EOF"
 </table></body></html>
 EOF
}

Description :

  1. To read the @words again and again i had used foreach loop. Here @words are read from the previous subroutine. I tried to load the same array contents into the <td> tag. But from my code it is not getting the into the foreach? Because of what mistake it is not loading its contents.

Expected output:

From $load1 it got the following output:

hardware
software
os

So i tried to print it in table i.e inside <td> tag so i had used the foreach loop to take default value.

Now i expect my output should be as follows:

s.no name
1 Hardware
2 Software
3 os
simbabque
54.4k8 gold badges77 silver badges141 bronze badges
asked May 24, 2017 at 10:00
6
  • 1
    This is not your real code. It has syntax errors. Please include your real code! And while you're at it, please also include example data to reproduce the problem, and explain what output you expect. Commented May 24, 2017 at 11:27
  • real code is too big thats why i had added the part of the code where it shows problem. @simbabque Commented May 24, 2017 at 11:41
  • added expected output. @simbabque Commented May 24, 2017 at 11:41
  • 1
    Your indentation is horrible and your brace placement is inconsistent. If you want people to read and understand your code, please take the time to make it as easy to read as possible. Commented May 24, 2017 at 13:26
  • 1
    I know that your code is too large to share. But the code that you give us should at least compile. Having just gone through your code to make it readable I spot at least two lines with obvious syntax errors - my @words = split /,., $firstline; and my ($load, @words = @_;. If you can't take the time to make your code even compile, then why should we spend time looking at it? Commented May 24, 2017 at 13:34

1 Answer 1

1

The EOF marker for the HEREDOC needs to be all the way on the left. It cannot have leading whitespace. Because of that, there should be a syntax error because the rest of your code is not recognized as code by the parser.

It should look like that.

sub layout
{
 my($load)=@_;
 my @words=@_;
 my $add=0;
 print << "EOF";
 <html><body><table>
 <tr>
 <th>s.no</th>
 </tr>
EOF
 foreach(@words)
 {
 $add++;
 print<<"EOF"
 <tr>
 <td>$add</td>
 <td>$_</td>
EOF
 }
 print <<"EOF"
 </table></body></html>
EOF
}

Indented HEREDOCs will probably be available with Perl 5.26. A new feature was released with the development version 5.25.7 that introduces new syntax <<~"EOF". But you likely do not have that version, or a newer one.


In addition to that, you are putting the first argument of the function in $load, but also into @words. That's probably wrong. That should be my ($load, @words) = @_.

answered May 24, 2017 at 10:16
Sign up to request clarification or add additional context in comments.

6 Comments

After Indentation also i find same problem. @simbabque
added expected output @simbabque
why the <tr> is not closed inside the foreach loop @simbabque
@insite because I copied it from the question, and it's missing there too ;)
@insite both me and others have asked you in comments on the question to at least make the code there compile, and to include example input. If you do not do that we're not going to help you, because we can't. You might think that it's just the example, so it doesn't matter, but it does. Most of the times you will find the problem yourself when trying to reduce it to a minimal example. Often the problem is not where you think it is. I am willing to help you, for free, on my day off (there's a bank holiday in my country today), but only if you help, too.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.