taint has asked for the wisdom of the Perl Monks concerning the following question:
I see it's a bit quiet here today. So I thought I'd throw out a question that I've been meaning to get a difinative answer to, for a long time. But I should probably preface this, with the recognition that while I realize there's a myriad of possible scenarios, has anyone a formula they'd be willing to offer? Maybe this would be better as a Meditation, or become a tutorial?
OK. How does one determine the bast way to quote their data (string, or not)?
Perl offers so many choices:
q - singly quote a string
qq - doubly quote a string
qr - Compile pattern
quotemeta - quote regular expression magic characters
qw - quote a list of words
qx - backquote quote a string
and, of course; ' ' - single quotes
and " " - double quotes
heredocs - heredocs (thanks LanX for catching my omission)
In an effort to limit the scope, so as not to seem to vague, or unreasonable. I'd like to limit this question to situations that only deal with strings; input, and output -- especially as it relates to the web.
How do I know I've made the right, or safest choice? Can I simply just choose
print qq(yada,yada,yada);,
and know I'm fine? Or is
print '<input type="text" action="rm -rf /" />'; better?
perldoc(s) covers all the details of the quoting methods available. So I guess what I'm really asking, is; does anyone have a simple formula they use, that will always result in the perfect answer, in each/every situation?
Note; I probably would have chosen the Meditations section for this. But, as I noted earlier; it was quiet here today, and I thought others might benefit from the answers provided. But please kindly direct this to meditations, if you feel I'm wrong.
Best wishes, and thank you for your consideration.
--Chris
#!/usr/bin/perl -Tw use Perl::Always or die; my $perl_version = (5.12.5); print $perl_version;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: There are a million ways to quote. How can you determine the best/safest?
by LanX (Saint) on Nov 16, 2013 at 21:08 UTC | |
by taint (Chaplain) on Nov 16, 2013 at 21:32 UTC | |
|
Re: There are a million ways to quote. How can you determine the best/safest? (exaggeration)
by Anonymous Monk on Nov 17, 2013 at 02:36 UTC |