Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

direct data to a socket

by demoralizer (Beadle)
on Oct 22, 2014 at 08:14 UTC ( #1104652=perlquestion: print w/replies, xml ) Need Help??

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

Hi there,

looking at CPAN module IO:All I found some syntax I don't understand, maybe sb. here can help me with that:
perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'

What's driving me crazy is the "<" after $_[0].

If I dump $_[0] I get: bless( \*Symbol::GEN3, 'IO::All::Socket' );

I've been searching through the Internet and perldoc but couldn't find any description what this "<" is doing exactly :(

Thanks a lot for any help (even any keywords to search for will be highly welcomed)

Replies are listed 'Best First'.
Re: direct data to a socket
by Loops (Curate) on Oct 22, 2014 at 08:19 UTC

    The $_[0] is the socket that is passed from the accept into the sub. The filename is looked up and if it is executable, a pipe is created with it, otherwise it is opened directly. In either case the contents of the resulting IO::All object are fed into the $_[0] socket. Very similar to the simplistic use of '<' in:

    $contents < io 'file.txt';

    So this oneline web server can send the contents of simple files, or the generated output from executables.

    Update: The reason you couldn't find anything in perldoc is because this is an operator that has been overridden to act in this way by IO::All which is where the simple example above comes from. Thus, you can only use the '<' operator this way with IO:All objects.

      Thanks for your answer Loops...

      what I'm really looking for is the meaning of this <, the whole rest is quite simple and completely clear.

      I know this redirection from shell command line but not within perl... is it the same meaning here?
      Can you point me to perldoc where this is described, please?

        This is magic employed by IO::All.

        If you want to know the details, IO::All overrides some operators of Perl to make the normal Perl syntax do something different when dealing with IO::All objects.

        The expression is simply $a < $b, the comparison expression. Except that the IO::All objects do something different when "compared" with something, to make this syntax appear like shell redirection.

        Updated the original post, to be more clear about that part, but not quickly enough, sorry. As I said in that update, this is a specific operator overload implemented by IO::All. It is very specific to objects created with IO::All and so you wont see examples of usage like this anywhere else in perldoc.
Re: direct data to a socket
by choroba (Cardinal) on Oct 22, 2014 at 09:03 UTC
    Have you read the DESCRIPTION section in IO::All?

    The module overloads various operators to provide syntax somehow similar to C++.

    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1104652]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2023-01-30 21:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found