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

Ok, I'm trying to write a perl program to do somewhat carefree web updates for me. I basically run a script and it asks me for text, and image names, and makes an update for me without having to touch any HTML (which makes me a happy guy) But, Perl seems to be having some trouble with my code. Any takers? I get the error Can't modify <HANDLE> in scalar assignment at ./webpage.pl line 103, near "$name1)" which is in the first if statement of the makeTable subroutine. I'm really itching to get this working as I think I'm very close. My problem may be how I'm dealing with backslashes from HTML refs with my scalar names, I'm not sure. I'm fairly new to Perl and don't know it's subtleties yet. Code is below. Thanks for the help, Adam
print "Do you have any images? "; chomp($image_answer = <STDIN>); if($image_answer eq "yes" || $image_answer eq "y") { print "How many? (1-4) "; chomp($numPics = <STDIN>); &makeTable($numPics); } elsif($image_answer eq "no" || $image_answer eq "n") {print "No soup for you!\n";} sub makeTable { if($numPics==1){ print NEWCONTENT "<TABLE><TR><TD>"; print "Name of image 1: "; chomp(<STDIN>=$name1); print NEWCONTENT "<IMG SRC=pics/$name1></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/spacer.gif></TD</TR> \n"; print NEWCONTENT "</TABLE>"; print "Table for 1 created"; } elsif(numPics==2){ print NEWCONTENT "<TABLE><TR><TD>"; print "Name of image 1: "; chomp(<STDIN>=$name1); print "Name of image 2: "; chomp(<STDIN>=$name2); print NEWCONTENT "<IMG SRC=pics/$name1></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/$name2></TD></TR> \n"; print NEWCONTENT "</TABLE>"; print "Table for 2 created"; } elsif(numPics==3){ print NEWCONTENT "<TABLE><TR><TD>"; print "Name of image 1: "; chomp(<STDIN>=$name1); print "Name of image 2: "; chomp(<STDIN>=$name2); print "Name of image 3: "; chomp(<STDIN>=$name3); print NEWCONTENT "<IMG SRC=pics/$name1></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/$name2></TD></TR> \n"; print NEWCONTENT "<TR><TD><IMG SRC=pics/$name3></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/spacer.gif></TD</TR> \n"; print NEWCONTENT "</TABLE>"; print "Table for 3 created"; } elsif(numPics==4){ print NEWCONTENT "<TABLE><TR><TD>"; print "Name of image 1: "; chomp(<STDIN>=$name1); print "Name of image 2: "; chomp(<STDIN>=$name2); print "Name of image 3: "; chomp(<STDIN>=$name3); print "Name of image 4: "; chomp(<STDIN>=$name4); print NEWCONTENT "<IMG SRC=pics/$name1></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/$name2></TD></TR> \n"; print NEWCONTENT "<TR><TD><IMG SRC=pics/$name3></TD>"; print NEWCONTENT "<TD><IMG SRC=pics/$name4></TD</TR> \n"; print NEWCONTENT "</TABLE>"; print "Table for 4 created"; } else print "Number entered is not within accepted paramters. \n"; }

Replies are listed 'Best First'.
(jeffa) Re: HTML and Perl
by jeffa (Bishop) on Mar 10, 2002 at 21:50 UTC
    Hmmm, I see a huge problem with your approach. Fancy this, write me a program that allows me to add more than four pictures, how about four hundred? Do you see the problem?

    Instead of hard coding possible results and using endless if-elsif-elsif-elsif ... just use a while loop and rely on the end of line character (CNTRL-D) to stop. Also, use CGI.pm to create the HTML (i added the usage of CGI::Pretty to format the resulting HTML to be more readable):

    use strict; use CGI qw(:standard); use CGI::Pretty; my $image; while (1) { print "name (CNTL-D or ENTER exits): "; chomp($image = <>); exit unless $image; print table( # or you can use: print NEWCONTENT table( Tr([ td([img({-src=>"pics/$image"})]), td([img({-src=>"pics/spacer.gif"})]), ]), ), "\n"; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: HTML and Perl
by dreadpiratepeter (Priest) on Mar 10, 2002 at 20:52 UTC
    Well, the problem that causes that error is that you have your expressions backward in the chomp. it should be:
    chomp($name1=<STDIN>);
    You repeat the error throughout the code. But, I can spot a couple of other problems:
  • in the elsifs you are missing the $ on numPics
  • your final else is missing the braces around the statement.
    use strict would have found all these problems, and is a neccessity for beginning perl programmers.

    -pete
    "I am Jack's utter lack of disbelief"
Re: Problem with an odd filehandle
by Zaxo (Archbishop) on Mar 10, 2002 at 21:14 UTC

    Many problens here.

    1. You are trying to parse cgi form data: use CGI;
    2. See the repetition in your elsifs? That's a sign you want a loop or its cousin map.
    3. The expression <STDIN>=$name would generate your error message. You can't assign to the STDIN stream.

    After Compline,
    Zaxo

Re: Problem with an odd filehandle
by chromatic (Archbishop) on Mar 11, 2002 at 02:51 UTC
    The problem is, <STDIN>=$name1 tries to assign the value identified as $name1 to the (read only) line read from standard input. Swap the lvalue (left side of the assignment operator) with the rvalue (right side) and you'll have better luck: my $name1 = <STDIN>; Of course, if you have a variable number of things to read, you're better off with an array. See push.