Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Question regarding using references with Net::SSH2 in module

by Preceptor (Chaplain)
on Jun 14, 2013 at 20:58 UTC ( #1039036=note: print w/ replies, xml ) Need Help??


in reply to Question regarding using references with Net::SSH2 in module

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


Comment on Re: Question regarding using references with Net::SSH2 in module
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1039036]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2014-12-29 02:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls