undef or zero? (semantic question)

Discussion in 'Perl Misc' started by John, Mar 2, 2004.

  1. John

    John Guest

    Hi all,


    I have been thinking about how to define a value. That's the idea.


    If you have one String, you can get the length of the String. But what
    happens if there is no String??? you would say:

    $string_length = undef;

    or

    $string_length = 0;

    to me the value should be undefined but zero can play the same role.

    How do you think this should be addressed?


    Thanks for your comments
    J
     
    John, Mar 2, 2004
    #1
    1. Advertisements

  2. John

    fifo Guest

    Well

    $ perl -le'print length(undef)'
    0

    which I suppose is indeed the number of characters in an undefined
    value. If you're writing your own String_length function, it could
    return 0 or undef (each with or without an accompanying warning) or it
    could throw an exception. What you choose depends on how you intend to
    use it.
     
    fifo, Mar 2, 2004
    #2
    1. Advertisements

  3. John

    ko Guest

    A simple assignment will do, anything except undef.
    They are not the same. First, try the length() function to verify that
    the lengths are different. Second, try the defined() function to verify
    that assignment of 0 results in a defined value.
    Start with:

    perldoc -f defined
    perldoc perlsyn ('Declarations' and 'Truth and Falsehood' sections)
    HTH -keith
     
    ko, Mar 2, 2004
    #3
  4. John

    Brad Baxter Guest

    If you have one String, you can get the length of the String. But what
    Perhaps:

    $string_length = defined $string ? length $string : undef;

    Then the definedness of $string_length would parallel that of $string.

    Regards,

    Brad
     
    Brad Baxter, Mar 3, 2004
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.