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

i have this in the middle of my cgi how do i get the foreach statment and the if statement to work inside the print <<EndHTML; tag. I have been using print ""; on every line but want to use this for less code. Is there a way?
print <<EndHTML; <html><head><title>Order Form</title></head> <body> <h2>Thank You</h2> You should except a reply back in the next 24 hours. Feel free to emai +l any additional comments to <a href="mailto:">JUST 'N STYLE</a><br> foreach $key (keys(%FORM)) { print "$key = $FORM{$key}<br>"; } <p></p>$FORM{'firstname'} $FORM{'lastname'}<br> Email: $FORM{'email'}<br> Phone Number: $FORM{'number'}<br> <br>Catalog: $FORM{'catalog'}<br> Brand: $FORM{'brand'}<br> Item number: $FORM{'inumber'}<br> Color: $FORM{'color'}<br> Sizes: $FORM{'size'}<br> if ($FORM{'services'} eq "yes") { <br>Services:<br> $FORM{'GraphicsDesign'}<br> $FORM{'Screenprinting'}<br> $FORM{'Digitizing'}<br> $FORM{'Embroidery'}<br> $FORM{'Manufacturing'}<br> } if ($FORM{'textarea'} ne "") { print "<br>Comments:<br>$FORM{'textarea'}"; } </body></html> EndHTML

Replies are listed 'Best First'.
Re: foreach and if inside <<ENDHTML? Can they work?
by Zaxo (Archbishop) on Aug 13, 2003 at 05:26 UTC

    A heredoc like you show interpolates perl variables (scalars and arrays) but does not evaluate perl expressions. The same rules as qq() or double quotes apply.

    If the heredoc tag is introduced in single quotes, no interpolation is done at all.

    You can get snippets of code to run by faking the interpolation into evaluating a reference by way of, say,

    print <<EOT; @{[map {foo($_)]} @list]} ${\bar($text)} EOT

    After Compline,
    Zaxo

Re: foreach and if inside <<ENDHTML? Can they work?
by blokhead (Monsignor) on Aug 13, 2003 at 06:19 UTC
    Your goal from all this code + HTML seems to be templated HTML, complete with if-logic and loops. I have a feeling that if you just take a glance at HTML::Template, you will quickly realize how much time and agony it can save you. In short, it does simple variable substition as well as, you guessed it, if-logic and loops! If you search the monastery, you'll find just loads of resources, not the least of which is HTML::Template Tutorial.

    (Of course, there are other good templating systems out there, but HTML::Template covers exactly the functionality it appears you are looking for, so I think it's the best choice for you)

    As your code stands at the moment, you seem to rate a #3 on Cody Pendant's scale of Perl/HTML interaction, which isn't bad. You could certainly do much worse! However, you've stumbled upon one of the main limitations of here-docs. So although I don't really address your original question (the other posts cover that pretty well), I just wanted to save you the trouble of trying to make your own templating system (#4 on the scale), which is a common next step up the ladder, but nearly always given up later in favor of a proven templating system (#5).

    blokhead

      Heres a link to the html that links to the post.cgi You can feel out the form and hit submit if you want to see what i am working with. I am on a tripod server and i can only use the pm that they previde. See my problem!
        HTML::Template is a pure Perl module, so you can just upload the .pm file and require it from your script.

        blokhead

        Thanks guys for the help. This is my finally script please tell me what you think. It sends it another script that thn emails me using my mail porgram in tripod the tripodmail.pm Feel free to check out the script at HERE If you check it out and find a problem please tell me.
        #!/usr/bin/perl require TripodMail; print "Content-type:text/html\n\n"; $FORM{'GraphicsDesign'} = ""; $FORM{'Screenprinting'} = ""; $FORM{'Digitizing'} = ""; $FORM{'Embroidery'} = ""; $FORM{'Manufacturing'} = ""; $alpha = ""; $printy_stuff = ""; read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value;} if ($FORM{'services'} eq "yes") { $printy_stuff = qq( Services:<br> $FORM{'GraphicsDesign'}<br> $FORM{'Screenprinting'}<br> $FORM{'Digitizing'}<br> $FORM{'Embroidery'}<br> $FORM{'Manufacturing'}<br>); } if ($FORM{'textarea'} ne "") { $alpha = "Coments: $FORM{'textarea'}"; } print <<EndHTML; <html><head><title>Order Form</title></head> <body> <h1>Please check your order.</h1> If all the info is correct please click yes, if not click the back but +ton on your browser. <p></p>$FORM{'firstname'} $FORM{'lastname'}<br> Email: $FORM{'email'}<br> Phone Number: $FORM{'number'}<br> <br>Catalog: $FORM{'catalog'}<br> Brand: $FORM{'brand'}<br> Item number: $FORM{'inumber'}<br> Color: $FORM{'color'}<br> Sizes: $FORM{'size'}<br><br> $printy_stuff<BR> $alpha <form method="post" action="post.cgi"> <input type="hidden" name="firstname" value="$FORM{'firstname'}"> <input type="hidden" name="inumber" value="$FORM{'inumber'}"> <input type="hidden" name="number" value="$FORM{'number'}"> <input type="hidden" name="catalog" value="$FORM{'catalog'}"> <input type="hidden" name="Embroidery" value="$FORM{'Embroidery'}"> <input type="hidden" name="brand" value="$FORM{'brand'}"> <input type="hidden" name="lastname" value="$FORM{'lastname'}"> <input type="hidden" name="Screenprinting" value="$FORM{'Screenprintin +g'}"> <input type="hidden" name="size" value="$FORM{'size'}"> <input type="hidden" name="email" value="$FORM{'email'}"> <input type="hidden" name="color" value="$FORM{'color'}"> <input type="hidden" name="Digitizing" value="$FORM{'Digitizing'}"> <input type="hidden" name="GraphicsDesign" value="$FORM{'GraphicsDesig +n'}"> <input type="hidden" name="services" value="$FORM{'services'}"> <input type="hidden" name="Manufacturing" value="$FORM{'Manufacturing' +}"> <input type="hidden" name="button" value="submit"> <input type="hidden" name="textarea" value="$FORM{'textarea'}"> <input type="submit" name="button" value=" yes "> </form> EndHTML print "</body></html>\n";
Re: foreach and if inside <<ENDHTML? Can they work?
by sgifford (Prior) on Aug 13, 2003 at 05:35 UTC
    What you probably want to do is just use multiple print statements, each with its own here document. This will be less print statements than one for every line, without trying to force everything into one statement.
    print <<EndHTML; <html><head><title>Order Form</title></head> <body> <h2>Thank You</h2> You should except a reply back in the next 24 hours. Feel free to emai +l any additional comments to <a href="mailto:">JUST 'N STYLE</a><br> EndHTML; foreach $key (keys(%FORM)) { print "$key = $FORM{$key}<br>"; } print <<EndHTML; <p></p>$FORM{'firstname'} $FORM{'lastname'}<br> Email: $FORM{'email'}<br> Phone Number: $FORM{'number'}<br> <br>Catalog: $FORM{'catalog'}<br> Brand: $FORM{'brand'}<br> Item number: $FORM{'inumber'}<br> Color: $FORM{'color'}<br> Sizes: $FORM{'size'}<br> EndHTML; if ($FORM{'services'} eq "yes") { print <<EndHTML; <br>Services:<br> $FORM{'GraphicsDesign'}<br> $FORM{'Screenprinting'}<br> $FORM{'Digitizing'}<br> $FORM{'Embroidery'}<br> $FORM{'Manufacturing'}<br> EndHTML; } if ($FORM{'textarea'} ne "") { print "<br>Comments:<br>$FORM{'textarea'}"; } print "</body></html>\n";
Re: foreach and if inside <<ENDHTML? Can they work?
by Cody Pendant (Prior) on Aug 13, 2003 at 05:38 UTC
    Congratulations on moving from print print print to print <<HERE

    The sensible thing to do is get the processing out of the way first:

    for(a..z){ $alphabet .= $_; } if ($x ne 'biteme'){ $printy_stuff = qq( <br>Services:<br> $FORM{'GraphicsDesign'}<br> $FORM{'Screenprinting'}<br> $FORM{'Digitizing'}<br> $FORM{'Embroidery'}<br> $FORM{'Manufacturing'}<br> ); } else { $printy_stuff = 'no input'; } print <<HERE Here's the alphabet: $alphabet Here's some graphics stuff if you entered anything: $printy_stuff HERE


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
      ok thanks for that. I just can't get the foreach to work. It not that big of deal since it's just for me to see all the varibles.
Re: foreach and if inside <<ENDHTML? Can they work?
by PodMaster (Abbot) on Aug 13, 2003 at 05:30 UTC
    They're called here documents, and they're just an elaborate quoting mechanism. Your snippet is functionally equivalent to print qq~ ... stuff here ... ~; Variables get interpolated in double quotes, but double quotes aren't eval ( blip print "@{[1,2,3,4]}\n").

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.