Apart from that using placeholders is almost certainly the way to go, I think that your reasoning is slightly flawed.

Either you know that this $var contains the value that is maintained as a string field, or you don't. Perl doesn't store the quotes internally and whenever you load a variable, the initial value of the variable is always a string. It doesn't get converted to its binary form until you use it in a numeric context, at which point perl will convert the ascii representation to the binary numeric form. If you then imediately use it in a string context, perl will convert it back. Any simple test you tried to apply for either form is going to always be true because perl will supply the form you are asking for.

You don't say at which point in the process you are trying to make the determination.

When you are writing it to the text file, or when you read it back?

In the former case, you should be able to determine the type of the field from the table schema.

In the latter case, assuming you wrote the string quoted, you could simply not strip them off when you read the file in, and then you would know which was which. ie.

if( substr($var, 0, 1) eq "'" ) { print "It's a string\n" }

HTH?


In reply to Re: Re: Test for number or string by Anonymous Monk
in thread Test for number or string by nedals

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.