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

When using Net::SSH2 as backend to SFTP:Foreign. I get a HASH ref where I'm expecting to get the error condition. How do I get the error string?
use Net::SSH2; use Net::SFTP::Foreign; my $ssh2 = Net::SSH2->new( timeout => '30000' ); $ssh2->connect( $host, $port ) $ssh2->auth( username => $user, password => $pass ) my $sftp = Net::SFTP::Foreign->new( ssh2 => $ssh2, backend => 'Net_SSH +2', timeout => '30' ); $sftp->put( "$file", "$destfile", best_effort => 1, atomic => $atomic +) or ( warn( "Put failed: $sftp->error\n" ) && exit 1 );
When the put fails I get the warn error as shown below Error: Put failed: Net::SFTP::Foreign=HASH(0x123b3c0)->error

Replies are listed 'Best First'.
Re: Net::SFTP::Foreign error
by haukex (Archbishop) on Sep 25, 2018 at 18:32 UTC

    Method calls don't interpolate, try: warn( "Put failed: ".$sftp->error."\n" ).

Re: Net::SFTP::Foreign error
by Perlbotics (Archbishop) on Sep 25, 2018 at 18:45 UTC

    ... as haukex said or hack your way around it:

    ... warn( "Put failed: @{[$sftp->error]} \n" ) ...

    Update to response below:

    The @{ ... } dereferences an array-ref. By default, the resulting string is a space separated (see $" in perlvar) concatenation of the array elements. However, $sftp->error() returns a scalar (string). Here, the square brackets come into play. They generate an anonymous array - with one single element - that finally is dereferenced and interpolated.

    It's somewhat equivalent to: warn( "Put failed: " . join($", @{ [ $sftp->error() ] }) . " \n" );

      Ok thanks
      @{[$sftp->error]}
      worked I was trying with @{$sftp->error} Can you explain what the brackets are doing exactly?
        @{[$sftp->error]}

        ... what the brackets are doing ...

        See the "Baby cart" idiom in perlsecret.

        Update:

        @{[$sftp->error]}
        Note also that as  $sftp->error (I assume; haven't checked) returns a scalar, not a list,
            ${ \$sftp->error }
        would have worked as well in a string interpolation.


        Give a man a fish:  <%-{-{-{-<