new( Type => SOCK_STREAM, PeerAddr => "127.0.0.1", Proto => "tcp", PeerPort => $dbase_param{camera_stream}) or die "Cannot open soc" name="description" /> new( Type => SOCK_STREAM, PeerAddr => "127.0.0.1", Proto => "tcp", PeerPort => $dbase_param{camera_stream}) or die "Cannot open soc" />
11984

read sockets in array using in perl

Question:

I want to read my sockets and do a "getline" on them.

my @socket1; $socket1[0] = IO::Socket::INET->new( Type => SOCK_STREAM, PeerAddr => "127.0.0.1", Proto => "tcp", PeerPort => $dbase_param{camera_stream} ) or die "Cannot open socket on port " . $dbase_param{camera_stream} . ".\n"; print { $socket1[0] } "\n"; my $ligne = <$socket1[0]>; while ( !( $ligne =~ /Content-Length:/ ) ) { $ligne = <$socket1[0]>; }

It will tell me Use of uninitialized value $ligne in pattern match (m//) for the second $ligne = <$socket1[0]>;

I don't understand wy

Answer1:

Angle brackets are used also for glob(),

perl -MO=Deparse -e '$ligne = <$socket1[0]>;' use File::Glob (); $ligne = glob($socket1[0]);

so if you're not using plain scalar as socket, you might to be more explicit by using <a href="http://perldoc.perl.org/functions/readline.html" rel="nofollow">readline()</a>,

$ligne = readline($socket1[0]);

Answer2:

The <a href="http://perldoc.perl.org/perlop.html#I%2fO-Operators" rel="nofollow">I/O Operator <EXPR></a> can mean either <a href="http://perldoc.perl.org/functions/readline.html" rel="nofollow">readline</a> or <a href="http://perldoc.perl.org/functions/glob.html" rel="nofollow">glob</a> depending on EXPR.

In this instance, you need to use an explicit readline in order to accomplish what you want.

Additionally, you should always perform the reading of a handle in a while loop and put any additional flow logic inside the loop. This is because a while loop that is reading from a file will automatically check for an eof condition. The way that your code is currently written you could end up with an infinite loop if it never finds that regular expression.

To fix both issues, I would therefore rewrite your processing to the following:

my $ligne; while ( $ligne = readline $socket1[0] ) { last if $ligne =~ /Content-Length:/; }

Recommend

  • Redefinition different basic types => typedef
  • Regular expression, glob, Python
  • php foreach and glob() function
  • Renaming a file extension without specifying
  • Heroku rake task uninitialized constant for MongoMapper model
  • Python: ValueError: Mixing iteration and read methods would lose data
  • Python Raspberry pi - If path doesn't exist, skip the loop
  • Append Filename to RDD
  • How to combine multiple csv into one file in serial manner using python?
  • Saving a dictionary into an .XLSX
  • Using glob() to display images from a directory while echo'ing a unique first image
  • Will volatile protect me from all behind-the-scenes multi-threading hazards?
  • BBC micro:bit - Radio string transfer random carriage returns
  • Array resizing and realloc function
  • How to read data from socket connection - android
  • Cannot send user message with Spring Websocket
  • Show message when Tform2 is created?
  • Error in making a socket connection
  • Prevent Tomcat from caching request during starup
  • List images(01.png) and descriptions(01.txt) from directory
  • Access Android Market through SSH tunnel
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • DotNetZip - Calculate final zip size before calling Save(stream)
  • Can I make an Android app that runs a web view in Chrome 39?
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Return words with double consecutive letters
  • retrieve vertices with no linked edge in arangodb
  • Load html files in TinyMce
  • Free memory of cv::Mat loaded using FileStorage API
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Reading document lines to the user (python)
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal