reading line from socket

Discussion in 'Perl Misc' started by cerr, Apr 7, 2010.

  1. cerr

    cerr Guest

    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!
    --
    roN
    cerr, Apr 7, 2010
    #1
    1. Advertising

  2. cerr wrote:
    > 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!


    while (defined($line = <SOCKET>) && $count < $POSLIMIT){




    John
    --
    The programmer is fighting against the two most
    destructive forces in the universe: entropy and
    human stupidity. -- Damian Conway
    John W. Krahn, Apr 7, 2010
    #2
    1. Advertising

  3. cerr

    Willem Guest

    cerr wrote:
    ) Hi There,
    )
    ) I'm using something like:
    ) if (defined(<SOCKET>))

    This reads a line from the socket and then throws it away.
    Is this what you want ?

    ) {
    ) while (($line = <SOCKET>) && $count < $POSLIMIT){

    If $count >= $POSLIMIT, then this reads a line from the socket
    and then throws it away. Is this what you want ?


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
    Willem, Apr 7, 2010
    #3
  4. cerr

    Guest

    On Wed, 7 Apr 2010 11:31:22 -0700 (PDT), cerr <> wrote:

    >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
    , Apr 7, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,744
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    938
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    518
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    638
    Steve Holden
    Feb 1, 2009
  5. Steve Holden
    Replies:
    1
    Views:
    691
Loading...

Share This Page