sdl3 has asked for the wisdom of the Perl Monks concerning the following question:

Howdy,
I'm a client reading the SQL output from a socket connection(using the socket module) and I am returned multiple lines from a SQL select:
I currently have the line : $response = <$remote>;
Which kinda works for one line but I need to read multiple lines and find the end of the data returned. Seems the word SQLEND would do nicely. I would like this data to be in an array that so that I can play with the data.
Note that the connection does not drop at the end data. I have to close it myself.... That does not seem to be a problem as I am using "close($remote);" and once I see the SQLEND I can invoke it. SO me thinks...

Thanks

Replies are listed 'Best First'.
Re: Creating Arrays on the Fly
by davorg (Chancellor) on Sep 11, 2001 at 17:01 UTC
      push(@data, $response) while(($response = <$remote>) ne 'SQLEND');
      Sorry, but infinite loops do give me the fear(tm) ;o)

      broquaint

      I used this method excpet I modified it slightly. I changed if ($response eq 'SQLEND') { to if ($response =~ "SQLEND") { That seemes to clear up the infinite loop it produced. I assumed that it was due to a CR-LF at the end and tried cleaning it up with chomp but it didn't help. So in the interest of getting this project wrapped up I just searched for the SQLEND. Thanks for the help!
        Just be real sure the string "SQLEND" is never in the returned data...
Re: Creating Arrays on the Fly(boo)
by boo_radley (Parson) on Sep 11, 2001 at 17:26 UTC
    Try using
    while ($response=<$remote>){ #manipulate response here }

    or if you want it in array form for later processing, something similar to :

    while ($foo=<DATA>){chomp $foo;push @ary, $foo}; print "@ary"; __DATA__ foo bar baz quud quux

    Question : why doesn't 1 while {push @ary,<DATA>} do what's expected?That is, take a line from <DATA> and push it onto @ary. I seem to get stuck in a infinite loop... What behavior of <DATA> am I missing?

      Try this:

        perl -le 'print push @foo, undef'

      It will return 1, the number of elements successfully pushed onto the array. And undef is what you get when you try to read from an exhausted file handle, hence, your push is always true, at least until you run out of swap space...

      --
      g r i n d e r
Re: Creating Arrays on the Fly
by ariels (Curate) on Sep 12, 2001 at 16:03 UTC
    $/ was created especially for you.

    Try this:

    $/ = 'SQLEND'; my $response = <$remote>;
    This reads in $response everything up to the next appearance of SQLEND.

    Since you want an array of lines, you'd do well to add the code

    my @response_line = split /\n/,$response;