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

I have a script which I am doing something similar to:
print<<EOF <META NAME="keywords" CONTENT="$formdata{keywords}"> <META NAME="description" CONTENT="$formdata{description}"> EOF
What I am trying to do is write an IF statement so the script only displays the codes they added input to. This is a meta tag building script, if they don't add text in one of the fields I don't want it to display <meta name="keywords" CONTENT="">, I just don't want it to display at all. Any suggestions? Thanks!

Edit Masem 2002-03-13 - Code tags

Replies are listed 'Best First'.
Re: If statements while in PRINT
by Masem (Monsignor) on Mar 14, 2002 at 01:45 UTC
    Let's assume that you have your parameters from CGI.pm. Then, you can do something like the following:
    my @params = $cgi->param(); foreach my $param ( @params ) { if ( $cgi->param( $param ) ) { print "<META NAME=\"$param\" CONTENT=\"", $cgi->param( $param ), "\">"; } }

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: If statements while in PRINT
by rob_au (Abbot) on Mar 14, 2002 at 01:42 UTC
    How about something such as this ...

    print '<meta name="', $key, '" content="', $formdata{$key}, '">' if length $formdata{$key};

    This would easily allow you to iterate through the entire contents of the hash using a foreach loop with a keys %formdata construct.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: If statements while in PRINT
by Zaxo (Archbishop) on Mar 14, 2002 at 03:01 UTC

    I have a reservation I get into below, but here's another way to do it:

    print $formdata{$_} ? qq(META NAME="$_" CONTENT="$formdata{$_}">\n) : +'' for keys %formdata;
    The ternary operator is handy for picking values.

    My reservation is that the http server probably won't know to interpret meta tags as http headers for cgi output. The situation is similar to that for SSI - wrong handler. The solution is to feed your hash to the &CGI::header method.

    I reread your question before posting. I think my reservation doesn't apply to your problem, if you are using perl to generate static html pages. I'll let the warning stand, since it may be relevant to others.

    After Compline,
    Zaxo

Re: If statements while in PRINT
by webadept (Pilgrim) on Mar 14, 2002 at 03:02 UTC
    This might be off base, but it looks like your script is working through a browser input/ form data. If this is so, then the key won't show up if the user doesn't put in an answer for the field, even if you pass that field to your CGI script. In other words if I have a field named "address" and the user doesn't fill in that field then the field address won't be in the parse string.

    This function is pretty well known from the O'Riely CGI book (good book to get if you are going to do a lot of this stuff)
    sub parse_form_data { local (*FORM_DATA) = @_; local ($request_method, $query_string, @key_value_pairs, $key_value, $key, $value); $request_method = $ENV{'REQUEST_METHOD'}; if ($request_method eq "GET") { $query_string = $ENV{'QUERY_STRING'}; } elsif ($request_method eq "POST") { read (STDIN, $query_string, $ENV{'CONTENT_LENGTH'}); } else { &return_error (500, "Server Error", "Server uses unsupported method"); } @key_value_pairs = split (/&/, $query_string); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; if (defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } }
    Now with that you can write a simple function page like this to see if the variable even exists before using it.
    #!/usr/local/bin/perl &parse_form_data (*simple_form); print "Content-type: text/plain", "\n\n"; $address = $simple_form{'address'}; if ($address) { print "Your Address is ", $simple_form{'address'}, ".", "\n"; print "Please visit this Web server again!", "\n"; } else { print "You did not enter a address.", "\n"; print "But, you are welcome to visit this Web server again!", "\n" +; } exit(0);
    Don't give me credit for the code, again its paraphrased from the O'Riely CGI book, but as you can see it takes care of the problem on if you got the information or not.

    Hope this helps

    Glenn H.
Re: If statements while in PRINT
by Juerd (Abbot) on Mar 14, 2002 at 06:47 UTC

    print map { qq[<META NAME="$_" CONTENT="$formdata{$_}">\n] } grep { defined $formdata{$_} and $formdata{$_} ne '' } qw { author keywords description generator };

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: If statements while in PRINT
by venimfrogtongue (Novice) on Mar 14, 2002 at 07:21 UTC

    Ok, most of you supplied information which I bet makes sense, but I am really new to perl and this is my first attempt to make a program. The program works without errors but it prints null coding.

    I am not looking for you to just give me the codes to fix it, I really want to learn how to do this as I want to become a perl guru someday.

    You can see the working script here . You can type in information, but leave some fields blank and see what happens. The results will be printed on the next page.

    To see the source I am working with, goto source

    .

    Can someone use lamer terms (for a newbie) on the easiest way to fix this problem? Remember, I don't want the codes made for me, I want to learn from this.

    I would VERY much appreciate this!

    Thanks!!!!

    venimfrogtongue

      What (almost) everyone is suggesting is that you can solve your problem by approaching it in a differnet way. Instead of having a single print statement that outputs a scalar containing multiple lines of HTML -- with some sort of (unknown) embedded logic that is smart enough to supress lines that shouldn't be there, try having multiply calls to the "print" function, if-and-only-if the data is there to print that line.

      This could be as simple as having unique calls for each line...

      print "...html 1..." if ...condition_1...; print "...html 2..." if ...condition_2...; ...
      Or, since the lines are all very similar, and the values are driven by a hash anyway, you could loop over the keys...
      foreach (keys %formdata) { print "...$formdata{$_} ..."; }

      (The exception so far being Juerd's method of turning the foreach/print inside out using print/map)

A reply falls below the community's threshold of quality. You may see it by logging in.