Greetings, Monks.

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;

In reply to There are a million ways to quote. How can you determine the best/safest? by taint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.