Perl syntax

J

James

Need help to understand the following syntax.

Net::SSH::perl package, in Channel.pm, line 142,

142: $r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );

in sub process_buffers,

sub process_buffers {
my $c = shift;
my($rready, $wready) = @_;

my %fd = (output => $c->{wfd}, extended => $c->{efd});
for my $buf (keys %fd) {
if ($fd{$buf} && grep { $fd{$buf} == $_ } @$wready) {
if (my $r = $c->{handlers}{"_${buf}_buffer"}) {
$r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );
}
else {
#warn "No handler for '$buf' buffer set up";
}
$c->{local_consumed} += $c->{$buf}->length
if $buf eq "output";
$c->{$buf}->empty;
}
}

if ($c->{rfd} && grep { $c->{rfd} == $_ } @$rready) {
my $buf;
sysread $c->{rfd}, $buf, 8192;
($buf) = $buf =~ /(.*)/s;
$c->send_data($buf);
}
}


TIA
James
 
E

eishbut

Need help to understand the following syntax.

Net::SSH::perl package, in Channel.pm, line 142,

142: $r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );

I believe a coderef is being used. $r->{code} contains a subroutine
and ( $c, $c->{$buf}, @{ $r->{extra} } ) are the parameters being
passed to the sub.

e.g.

#!/usr/bin/perl -w

use strict;

my $hash_ref;

$hash_ref->{add} = sub{
my $ten = shift;
my $twenty = shift;
my $numbers = shift;
my $sum;

$sum += $_ for @{$numbers};

print "$ten $twenty $sum\n";
};

$hash_ref->{add}->(10, 20, [1, 2, 3, 4, 18]);
 
P

Paul Lalli

Need help to understand the following syntax.

Net::SSH::perl package, in Channel.pm, line 142,

$r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );

$r is a reference to a hash. The hash that $r references contains (at
least) two keys: 'code' and 'extra'.

$r->{code} is the value of the hash that $references, at the key
'code'. This value is a reference to a subroutine. You can call a
subroutine via its reference by using the arrow-parentheses notation.
So $r->{code}->(...) says to call the subroutine referenced by $r-

Arguments are being passed to this subroutine. The first, $c, is a
reference to a hash. The second $c->{$buf}, is the value of this hash
at the key $buf. Finally, $r->{extra} is a reference to an array, and
@{$r->{extra}} is the array that $r->{extra} references. All of the
elements of this array (which could be 0 or more) are being passed as
the final arguments to the subroutine that $r->{code} references.

For more information:
perldoc perlreftut
perldoc perlref

Hope this helps,
Paul Lalli
 
T

Tad McClellan

James said:
Need help to understand the following syntax.

Net::SSH::perl package, in Channel.pm, line 142,

142: $r->{code}->( $c, $c->{$buf}, @{ $r->{extra} } );


$r is a reference to a hash.

$r->{code} is a reference to a subroutine.

$c is a reference to a hash.

$r->{extra} is a reference to an array.

See:

perldoc perlreftut

$c->{$buf} is an example of "Use Rule 2".

@{ $r->{extra} } is an example of "Use Rule 1".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,245
Latest member
Evelyne64L

Latest Threads

Top