undef or zero? (semantic question)

J

John

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
 
F

fifo

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?

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.
 
K

ko

John said:
Hi all,

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

A simple assignment will do, anything except undef.
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;

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.
to me the value should be undefined but zero can play the same role.

How do you think this should be addressed?

Start with:

perldoc -f defined
perldoc perlsyn ('Declarations' and 'Truth and Falsehood' sections)
Thanks for your comments
J

HTH -keith
 
B

Brad Baxter

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.

Perhaps:

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

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

Regards,

Brad
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,087
Members
48,773
Latest member
Kaybee

Latest Threads

Top