Diego Buendia
Diego Buendia

Reputation: 97

What does the Perl syntax <$VARIABLE> mean?

Just can't find what this syntax (<$SOCKET>) does, although I guess it is some kind of default evaluation or behavior for the object inside the variable.

I was trying to test my Apache server (after installing the mod_evasive module) with this supposed-to-be DoS probe (this goes inside a 100-times loop), but it always returns a response 400 Bad Request. Trying to decypher the code, I've got stuck with the commented line below.

use IO::Socket;
use strict;

my($response);
my($SOCKET) = new IO::Socket::INET( Proto   => "tcp",
                                  PeerAddr=> "127.0.0.1:80");
if (! defined $SOCKET) { die $!; }
print $SOCKET "GET / HTTP/1.1\n\n";

$response = <$SOCKET>; ## What are those < > signs???

print $response;
close($SOCKET);

Then main interest I have is to be able of playing the test, so I need to know, first of all, why I get the 400 error code to fix it.

But also, BTW, for the sake of curiosity I'd like to understand the <$SOCKET> syntax meaning, because I can't find it explained anywhere.

Upvotes: 0

Views: 94

Answers (2)

ikegami
ikegami

Reputation: 386501

  • <> means readline(ARGV)
  • <IDENTIFIER> means readline(IDENTIFIER)
  • <$IDENTIFIER> means readline($IDENTIFIER)
  • <...> (anything else) means glob(qq<...>)

You are using the third syntax, so <$SOCKET> means readline($SOCKET). You are using it in scalar context, so it reads a line from $SOCKET.

Upvotes: 3

brian d foy
brian d foy

Reputation: 132896

The < > (also known as "the diamond operator") are a shorthand for readline. The thing inside the brackets provides lines of input. That could be a file handle, pipe, socket, or some other such thing. In your case, it's the variable $SOCKET that stores the socket you created.

A call to <$SOCKET> reads the next line from that input.

There's a bit of a trick though. In some other cases the < > is a shorthand for glob. If the thing inside the angles looks like a glob pattern, the < > returns the list of files that match that pattern:

 my @files = <*.txt>;

That's some old syntax that you might see; I prefer to spell it out though:

 my @files = glob( '*.txt' );

Upvotes: 6

Related Questions