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

I have a CGI script that takes in phone numbers. When they are output by the script of the html phone numbers with leading zeros are truncated since I store them in three fields ( EX: (123) 123-0123 comes out as (123) 123-123 ) I tried padding the last field but the html page generated still has the same number shows 123 for the last four digits. Am I missing something?

Replies are listed 'Best First'.
Re: Cgi Phone Number Problem
by rob_au (Abbot) on Jan 07, 2002 at 04:39 UTC
    The command that you need here is printf for which there is an excellent tutorial on this site. eg.
    printf ("(%03d) %03d-%04d", $field1, $field2, $field3)

     

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

Re: Cgi Phone Number Problem
by dws (Chancellor) on Jan 07, 2002 at 01:47 UTC
    Strip this down to a short CGI script that exhibits the failure, and post it for us to look at.

Re: Cgi Phone Number Problem
by catcher (Initiate) on Jan 07, 2002 at 02:05 UTC
    This is due to the scalar nature of the variable you are using to store the number, (ie. PERL thinks it's a number not a string!), try padding with an alpha.
Re: Cgi Phone Number Problem
by dmmiller2k (Chaplain) on Jan 07, 2002 at 19:40 UTC

    The short answer is take rob_au's (s)printf recommendation. The whole scalar is-a scalar is-a scalar (i.e., integer <=> float <=> string) thing has probably bitten all of us at one point or another. (s)printf can go a long way toward setting things right, when nothing else seems to work.

    Be aware that, if you use CGI.pm to retrieve your script's parameters, the value returned by param() will be precisely what the user entered, warts (leading zeroes) and all. It is what happens after this point that determines whether (and when) Perl begins to treat your string of digits as a number. Sometimes it helps to step through the code with the perl debugger to see where things change.

    (For a CGI script, you can obtain an appropriate command line for the debugger from your browser by changing the form's retrieval METHOD to GET, instead of POST. Then, after submitting your form, go to your browser's location bar and copy everything after the question mark (as in, http://your.server.domain/path/mycgiscript.cgi?...), not including the question mark istelf, and paste it onto the end of a command line at the prompt, so that it looks something like this: perl -d mycgiscript.cgi param1=val1&param2=val2. Then step through the script to the location of the problem. Remember, you're not necessarily interested in *debugging* the script--although it's possible--but rather in determining at when and why a particular variable's value starts being treated as a numerical value.)

    When dealing with strings that happen to look like numbers (e.g., zip codes), ensure that they are treated as strings by avoiding (deliberate or otherwise) numerical manipulations. You must be careful to examine what you do with the values subsequent to retrieving them; you may be implicitly forcing Perl to treat them as numbers (which is why dws asked to see your code).

    It's important to do this analysis, because once your awareness of how/when/why Perl differentiates between numbers and strings reaches a critical mass, you may find yourself slapping your forehead, leaping up out of your chair and yelling, "Aha!"

    Or maybe not.

    If you cannot locate and correct where you might be (implicitly or otherwise) treating your fields as numbers, using one of sprintf or printf, to gather and format the three phone number fields into a single output value is perhaps a simpler solution.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime
      Opened a new node to examine this leading 0 thing a little deeper.
Re: Cgi Phone Number Problem
by LogicalChaos (Beadle) on Jan 07, 2002 at 04:57 UTC
    This is a hack, but if you check the length of the field and it's too short, prepend a zero:
    while ( length $field2 < 4 ) { $field2 = "0" . $field2; }
    Update
    Keep in mind that by doing this you can, for instance, add to $field2, and still keep the leading zero. Since I'm new, at least comment on why this node is bad...
      You asked "comment on why this node is bad...", and i think it would help you to know. From what i can tell, the reason people might down vote this is because perl has a set of internal functions which do the same thing much faster. sprintf and printf. The post prior to yours points this out. While i am sure most of us at some point have written code similar to yours, i imagine people down-voted it because the post prior haad a faster, cleaner solution. Don't let this discourage you ... read up on (s)printf for the next time ... this is how we all learn it.
      $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
        Thanks, that's just what I was looking for. I have never used sprintf outside of C, and had not even thought of it in perl. I don't mind the '-' vote as much as not knowing why... Thanks.