Professional Web Applications Themes

empty strings vs nulls - Perl / CGI

How can I test for empty strings and nulls on a particular value. When I get an empty string or a null value I need to do something. Thanks in Advance. BassFool...

  1. #1

    Default empty strings vs nulls

    How can I test for empty strings and nulls on a particular value. When
    I get an empty string or a null value I need to do something.

    Thanks in Advance.

    BassFool



    Eric Walker Guest

  2. #2

    Default Re: empty strings vs nulls

    Eric Walker wrote:
    >How can I test for empty strings and nulls on a particular value. When
    >I get an empty string or a null value I need to do something.
    >
    >Thanks in Advance.
    >
    >BassFool
    >
    >
    >
    >
    >
    # from a unix shell,
    # empty string
    perl -e '$s = "";unless ($s) {print "Empty string\n";}'
    # null value
    perl -e '$n = hex 0; unless ($n) {print "Null\n";}'



    Douglas Lentz Guest

  3. #3

    Default Re: empty strings vs nulls

    On Mon, 2003-11-24 at 13:30, Eric Walker wrote:
    > How can I test for empty strings and nulls on a particular value. When
    > I get an empty string or a null value I need to do something.
    Hi Eric,

    To test for a empty string simply do the following:

    if ($mystring eq "") {
    #string is empty
    }

    For a null or undefined value do:

    if($mystring) {
    #variable is undefined
    }

    the long version is:

    if($mystring eq undef) {
    #variable is undefined
    }

    HTH,
    Kevin
    --
    Kevin Old <koldkold.homelinux.com>

    Kevin Old Guest

  4. #4

    Default Re: empty strings vs nulls

    Eric Walker wrote:
    >
    > How can I test for empty strings and nulls on a particular value. When
    > I get an empty string or a null value I need to do something.
    Hi Eric.

    I assume you're using the DBI module?

    In general, a null value will be passed back as Perl 'undef' and
    an empty string as, well, an empty string! So

    if not defined $field

    will tell you whether it's null and

    if defined $field and length $field == 0

    will tell you whether it's zero-length.

    Beware, though, that you may also want to test
    for all-spaces in a field. Also some databases
    disallow zero-length string fields.

    HTH,

    Rob


    Rob Dixon Guest

  5. #5

    Default Re: empty strings vs nulls

    Kevin Old wrote:
    >
    > On Mon, 2003-11-24 at 13:30, Eric Walker wrote:
    > > How can I test for empty strings and nulls on a particular value. When
    > > I get an empty string or a null value I need to do something.
    >
    > Hi Eric,
    >
    > To test for a empty string simply do the following:
    >
    > if ($mystring eq "") {
    > #string is empty
    > }
    Yep.
    > For a null or undefined value do:
    >
    > if($mystring) {
    > #variable is undefined
    > }
    No. $mystring is true if is defined and is neither an
    empty string nor zero.
    > the long version is:
    >
    > if($mystring eq undef) {
    > #variable is undefined
    > }
    No no no! Because of the string comparison this
    will force 'undef' to an empty string and therefore
    be the same as

    if ($mystring eq '')

    What you mean here is

    if (not defined $mystring) {
    #variable is undefined
    }

    Cheers Kevin.

    Rob


    Rob Dixon Guest

  6. #6

    Default Re: empty strings vs nulls

    Rob Dixon wrote:
    > Beware, though, that you may also want to test
    > for all-spaces in a field. Also some databases
    > disallow zero-length string fields.
    >
    > HTH,
    >
    > Rob
    I don't know about that. Usually I see this constraint as an option applied
    to required fields when they are needed to maintain referential or unit
    integrity. Can you cite any that actually prohibit them globally? I'm
    curious.

    Joseph

    R. Joseph Newton Guest

Similar Threads

  1. PDWordGetString makes empty strings?
    By Daniel_Sheiner@adobeforums.com in forum Adobe Acrobat SDK
    Replies: 2
    Last Post: December 9th, 05:32 PM
  2. #40394 [NEW]: mssql_bind() converts empty strings to null
    By rnerovich at gmail dot com in forum PHP Bugs
    Replies: 3
    Last Post: February 7th, 11:27 PM
  3. Extracting strings delimited by other strings
    By Scott Bass in forum PERL Modules
    Replies: 2
    Last Post: May 7th, 02:26 AM
  4. dealing with empty date strings
    By Lossed in forum Coldfusion Database Access
    Replies: 2
    Last Post: April 11th, 08:01 PM
  5. Problems with empty strings and Properties window
    By Cristian Ionita in forum ASP.NET Building Controls
    Replies: 0
    Last Post: August 10th, 02:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139