Help with a HTML table

P

perlUSER

Hello,
I am writing Perl/CGI script to build a page listing the files in a
directory.

I am calling a sub routine to write a row:
This piece of code is working fine.
sub printFile {
$q->Tr(
$q->td({colspan=>1},
span {class=>"textmain"}, "someReport.pdf")
)
}


I made a change to print a row for each file in loop.
There is no effect with the following code:
sub printFile {
foreach my $file (sort {$a cmp $b} @list) {
$q->Tr(
$q->td({colspan=>1},
span {class=>"textmain"}, $file)
)
}
}

Please help me why this is not working.
Any help is greatly appreciated.

Thanks in advance,
Sri.
 
A

A. Sinan Unur

I am calling a sub routine to write a row:
This piece of code is working fine.
sub printFile {

The name of this sub is misleading. There are no pring statements.
$q->Tr(
$q->td({colspan=>1},
span {class=>"textmain"}, "someReport.pdf")
)
}

How do you know it is working fine? It looks like you do not have warnings
and strictures enabled.

Please read and follow the posting guidelines for this group.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
P

Paul Lalli

perlUSER said:
I am writing Perl/CGI script to build a page listing the files in a
directory.

I am calling a sub routine to write a row:
This piece of code is working fine.
sub printFile {
$q->Tr(
$q->td({colspan=>1},
span {class=>"textmain"}, "someReport.pdf")
)
}

This function returns a string, because CGI.pm's Tr() method returns a
string. This function does not print anything. If this is actually
"working fine", then when you're calling it, you must be calling it
like so:

print printFile();
I made a change to print a row for each file in loop.
There is no effect with the following code:
sub printFile {
foreach my $file (sort {$a cmp $b} @list) {
$q->Tr(
$q->td({colspan=>1},
span {class=>"textmain"}, $file)
)
}
}
Please help me why this is not working.

This function does not return a string. This function does not return
anything at all. A foreach loop does not return the values its block
evaluated.

I would refactor your code to make the subroutine called printFile
actually *print*. But if you don't want to do that, you'll have to
store the strings you want to return from the function as you're
creating them, and then return them at the end of the function:

sub printFile {
my $Trs;
for my $file (sort @list) {
$Trs .= $q->Tr(
$q->td( span({class=>'textmain'}, $file) )
);
}
return $Trs;
}

Paul Lalli
 
P

perlUSER

A. Sinan Unur said:
The name of this sub is misleading. There are no pring statements.


How do you know it is working fine? It looks like you do not have warnings
and strictures enabled.

In the main program the place where I write the "Tr", I simply called
the sub routine to write "Tr".
 
P

Paul Lalli

perlUSER said:
In the main program the place where I write the "Tr", I simply called
the sub routine to write "Tr".

I was about to post something similar to Sinan, chiding him for making
assumptions.... then I copied and pasted your code, with strict and
warnings enabled, and got this:

Can't locate object method "span" via package "class" (perhaps you
forgot to load "class"?)

Unless you did both
use CGI;
and
use CGI qw/:standard/;

then you would need $q->span(), not just span()

This tells us that you did not copy and paste your code, but instead
re-typed it. Don't do that. Please read the posting guidelines for
the group.

Paul Lalli
 
P

perlUSER

A. Sinan Unur said:
The name of this sub is misleading. There are no pring statements.


How do you know it is working fine? It looks like you do not have warnings
and strictures enabled.

In the main program the place where I write the "Tr", I simply called
the sub routine to write "Tr".
 
P

perlUSER

Paul said:
sub printFile {
my $Trs;
for my $file (sort @list) {
$Trs .= $q->Tr(
$q->td( span({class=>'textmain'}, $file) )
);
}
return $Trs;
}

Paul Lalli

Paul,
You are simply great. I missed the basic concept. The "for" block
didn't return anything. I defined a var to returned the result set and
my page looks much better.

Thank's again.

Sri.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top