|
|
 |
 |
 |
 |
Perl Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Perl syntax
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
On Jun 1, 12:40 am, James <hslee@yahoo.com> wrote: > 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]);
> 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
On May 31, 7:40 pm, James <hslee@yahoo.com> wrote: > 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- >{code}.
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
James <hslee @yahoo.com> wrote: > 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". -- Tad McClellan SGML consulting t@augustmail.com Perl programming Fort Worth, Texas
|
 |
 |
 |
 |
|