[prev in list] [next in list] [prev in thread] [next in thread] 

List:       perl-beginners
Subject:    Re: Getting error in Net::SFTP with get function
From:       "Tom Phoenix" <tom () stonehenge ! com>
Date:       2007-10-31 18:21:35
Message-ID: 31086b240710311121r528b6485m8e178c9911f11802 () mail ! gmail ! com
[Download RAW message or body]

On 10/31/07, Jay Savage <daggerquill@gmail.com> wrote:

> it looks to me like OP is calling $sftp->get() in a void
> context:
>
>     $sftp->get( $file, $localFile ) || warn("errrrr0rrrrr_--->
> $!".$sftp->status."\n");

It may seem that way, but the left side of the logical or operator
isn't a void context, even if the operator itself is used in a void
context. That's actually a Boolean context (which is to say a scalar
context), no matter how the operator is used. That's because the left
side must be evaluated as true or false to determine whether the right
side will be evaluated, even if the whole thing is used in a void
context. Here comes the code:

  #!/usr/bin/perl

  use warnings;
  use strict;

  sub my_context {
    my $wa = wantarray ? "list" : "scalar";
    $wa = "void" unless defined wantarray;
    my $line = (caller)[2];
    print "Called in $wa context at line $line.\n";
    $wa;
  }

  print "Testing scalar and list context:\n";
  my $scalar = &my_context();
  my($list) = &my_context();
  print "Scalar: $scalar\nList: $list\n\n";

  print "Testing left side of ||-or in scalar context:\n";
  my $left_of_or = &my_context() || "0 but true";
  print "Left of ||-or: $left_of_or\n\n";

  print "Testing left side of ||-or in list context:\n";
  my($list_left_of_or) = &my_context() || "0 but true";
  print "List left of ||-or: $left_of_or\n\n";

  print "Testing left side of ||-or in void context:\n";
  &my_context() || die "no context";
  print "\n";

  print "Testing true void context:\n";
  &my_context();
  print "\n";

> At least I'm not aware that that '||' forces any particular context on
> the lefthand operand. Perlop mentions propagating context WRT the
> righthand operand, but doesn't say anything about the left.

The docs could always be more clear. The right operand of an
or-operator does inherit the context of the operator itself. But the
left operand's context is always Boolean.

> Given that, I would expect, from the docs, for get() to return the
> empty string on success

Where did you get that? From what I read of the docs, get() should
return the downloaded file contents on success. "If get is called in a
non-void context, returns the contents..."

  http://search.cpan.org/~dbrobins/Net-SFTP-0.10/lib/Net/SFTP.pm

Of course, the contents of a file could be the empty string or another
false value, so it would be appropriate to use defined() to test for
success.

Cheers!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic