Assuming I'm looking at the right lines, you're contemplating the difference between:
#print $channel "$cmd\n"; #while (<$channel>) { print {${$channel}} "$cmd\n"; while (<${$channel}>) {
That's because you're having a problem with passing references to filehandles. $channel is a filehandle. You're passing it by reference:
$self->{_ssh2_channel} = \$channel;Which means when you're trying to use it - as a filehandle - you need to dereference first. Consider if you will:
#!/usr/bin/perl use strict; use warnings; sub print_to_fh { my ( $ref_to_fh ) = @_; #$ref_to_fh is _not_ a filehandle. It's a scalar, that's a reference +. print $ref_to_fh "Some text\n"; } open ( my $filehandle, ">", "testfile.txt" ); &print_to_fh ( \$filehandle ); close ( $filehandle );
This will give you the same error - because what you're passing _into_ the subroutine is not a filehandle, it's a reference to a filehandle.
sub print_to_fh { my ( $ref_to_fh ) = @_; #$ref_to_fh is _not_ a filehandle. It's a scalar, that's a reference +. my $filehandle = $$ref_to_fh; #Filehandle has dereferenced $ref_to_fh, so we can print to it now: print $filehandle "Some more text\n"; } open ( my $filehandle, ">", "testfile.txt" ); &print_to_fh ( \$filehandle ); close ( $filehandle );
This works, because the filehandle has dereferenced. I think this is what is happening in your code - a filehandle is basically a reference to a file, and you are passing a reference _to_ that reference.
Edit: Check 'perldoc -f print': If you're storing handles in an array or hash, or in general whenever you're using any expression more complex than a bareword handle or a plain, unsubscripted scalar variable to retrieve it, you will have to use a block returning the filehandle value instead...
Therefore in the example above, you could instead do:
print {$$ref_to_fh} "Even more stuff\n";
That's the essence of what that 'not a GLOB' message means - print doesn't like (recognise) your filehandle
In reply to Re: Question regarding using references with Net::SSH2 in module
by Preceptor
in thread Question regarding using references with Net::SSH2 in module
by DarthBobToo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |