Hi There,
I'm using something like:
if (defined(<SOCKET>))
{
while (($line = <SOCKET>) && $count < $POSLIMIT){
...
...
and i'm getting following:
"Value of <HANDLE> construct can be "0"; test with defined() at ./
UpdateServer.pl line 831."
How can i get rid of this message? Thank you!
Krahn showed the fix for you.
This is a FAQ somewhere.
$line is evaluated as a conditional.
It is the result of the <HANDLE> read operation.
The read could return a '0' or 0, which equals false in a
conditional and breaks out of the loop.
In terms of defined(), all things are defined unless
its variable has the UNDEF flag set via some way,
$line = undef is one way.
The sucess of a call is usually returned via
undef. Since there is only 1 definition of undef,
most functions sneek it in on failure, otherwise the
test is on the data returned.
Adding a defined(($line = <SOCKET>)) around this really
checks if the read succeded or did not. At EOF, it will
return undef.
These are mostly the built-in functions that return undef
as a special meaning. Sometimes its a pain to have to use
the defined check all the time though.
In general, asignments within conditionals can be tricky.
The trick is to know if undef as a return value is a factor
in the code flow.
Likewise, a test in any code flow:
$line = <HANDLE>;
if (defined( $line)) {
# do other checks
# do something with line, its valid data
}
else {
# $line is undef, HANDLE is at eof
}
if (defined (0) and not 0) # true
if (defined ("0") and not "0") # true
if (defined ("") and not "") # true
Read perlfunc on defined()
-sln