Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to print an HTML input tag with its regex pattern in Perl CGI?

by underTheRadar (Acolyte)
on Mar 29, 2019 at 14:07 UTC ( [id://1231869]=perlquestion: print w/replies, xml ) Need Help??

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

I've already created a sign up page for my website as index.cgi but it would seem that the email address input box cannot accept any input at all.

However, it works just fine when the page is made into an html file.

I've tried using:
  qq|| = caused a 500 internal server error
  qq{} and qq() = the page loads fine but email address box is unable to accept any input.

# Here's the pseudocode print "Content-type: text/html\r\n\r\n"; print qq{ <!DOCTYPE html> <html> <head> </head> <body> <input type="email" name="emailAddress" class="inputBox" placehold +er="Email Address" id="emailBox" pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-] ++(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9] +)?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|mu +seum)\b" required> </body> </html>};
  • Comment on How to print an HTML input tag with its regex pattern in Perl CGI?
  • Download Code

Replies are listed 'Best First'.
Re: How to print an HTML input tag with its regex pattern in Perl CGI?
by haukex (Archbishop) on Mar 29, 2019 at 14:15 UTC

    qq interpolates no matter what opening and closing characters you use, so characters such as $ and \ will have special meaning in the string. If you don't want anything interpolated into the string, use a heredoc with single quotes:

    print <<'END_HTML'; <!DOCTYPE html> <html> ... </html> END_HTML

    However, generating HTML like this is pretty error-prone, as you've discovered. I suggest you look into a templating system such as Template::Toolkit.

    As for debugging CGI scripts, you should check your server's logs for the cause of a 500 error. See also CGI Help Guide, Troubleshooting Perl CGI scripts, and your browser's debugging tools to inspect the HTML.

    Update: The reason you're seeing a difference between qq|| and qq{}/qq() is that the latter supports nested brackets, so e.g. print qq{a{b}c}; prints a{b}c, whereas qq|a|b| is not valid (hence the 500 error), as the second | terminates the string - you'd have to write qq|a\|b| to get the string "a|b".

Re: How to print an HTML input tag with its regex pattern in Perl CGI?
by marto (Cardinal) on Mar 29, 2019 at 14:25 UTC

    Take a look at email does email validation, and consider reading CGI::Alternatives if you really want to make web development fun and less difficult on yourself.

Re: How to print an HTML input tag with its regex pattern in Perl CGI?
by RonW (Parson) on Mar 31, 2019 at 04:42 UTC

    Besides what haukex mentioned, there is also q() for non-interpolating quoting.

Re: How to print an HTML input tag with its regex pattern in Perl CGI?
by karlgoethebier (Abbot) on Mar 29, 2019 at 15:32 UTC

    As you like the stone-age CGI you may like a stone-age templating system as well. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1231869]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found