Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^4: Net::SFTP::Foreign error

by salva (Canon)
on Sep 26, 2018 at 07:31 UTC ( [id://1223037]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Net::SFTP::Foreign error
in thread Net::SFTP::Foreign error

Frankly, why would you like to use the baby cart when you can use the shorter and easier to understand crab operator? ".."

As in...

$a = "some ".$foo->bar." text";

Replies are listed 'Best First'.
Re^5: Net::SFTP::Foreign error
by AnomalousMonk (Archbishop) on Sep 26, 2018 at 14:59 UTC

    Indeed. Or, in the (list) context of the original question, why would one want to bother even to understand  . (concatenation) or a crab when  , (comma) will do:
        warn "Put failed: ", $sftp->error, "\n";
    (and it looks better, too, IMHO :).


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

      Thanks all for the replies. So now I see 4 ways to do the same thing.

      1. Baby cart

      @{ $sftp->error }

      2.crab

      ".$sftp->error."

      3. reference, deference

      ${ \$sftp->error }

      4. comma

      ", $sftp->error, "

      Can someone explain how each of these work ?

        I assume you didn't understand the explanation given in the perlsecret link. Ok, so consider:

        1. Baby Cart.   An array variable may be accessed using this "canonical" (in the sense that it always works) syntax:
          @{bareword     }
          @{symbolic reference  }
          @{hard reference   }
          (A symbolic reference — sometimes casually called a "soft" reference — only works with package-global variables as used in the example below. That's one reason symbolic references are Officially Frowned Upon; for others, see symbolic references in perlref and also Why it's stupid to `use a variable as a variable name' and the perlfaq7 FAQ "How can I use a variable as a variable name?" Symbolic referencing also only works when  strict 'refs' are disabled.) The "baby cart" just goes one step further and uses an anonymous array constructor  [ ... ] to generate an array hard reference to be dereferenced. E.g.:
          c:\@Work\Perl\monks>perl -wMstrict -le "no strict 'refs'; ;; our @array = qw(One Two Three); ;; my $soft_ref = 'array'; my $hard_ref = \@array; ;; print @{ array }, '*', @{ $soft_ref }, '*', @{ $hard_ref }; print qq/@{ array }-@{ $soft_ref }-@{ $hard_ref }/; ;; print qq/@{[ something_returning_a_list() ]}/; ;; sub something_returning_a_list { return qw(Nine Eight Seven); } " OneTwoThree*OneTwoThree*OneTwoThree One Two Three-One Two Three-One Two Three Nine Eight Seven

          ( Update: Upon further reflection, it occurs to me that the list above of syntactic variations for canonical array access should also have included the following (similar considerations apply to the discussion of  ${ ... } scalar access below):
              @{expression }
          where expression is any expression (not a statement!) whatsoever that evaluates to a bareword, symbolic or hard reference accessing an array. I don't know of any expression other than a bareword that evaluates to a bareword (update: however, see this), and we rightly shun symbolic references, so we can move directly to hard references.

          In this light, it's easy to see that  @{ [ ... ] } is just an expression-based array access in which the  [ ... ] anonymous array constructor expression evaluates to a reference | hard reference to a zero-or-more element array. Now all you need is anything at all inside the  [ ] that creates a zero-or-more element list. In the example above, it's the  something_returning_a_list() subroutine call, but anything will do. )

        2. Crab.   This is just string concatenation using the  . operator (see perlop). Scalars are stringized and concatenated together. Other data is handled in scalar context imposed by the concatenation operator. E.g.:
          c:\@Work\Perl\monks>perl -wMstrict -le "my ($x, $y, $z) = qw(Fie Foe Fum); my @ra = qw(foo bar baz); ;; my $s = $x . $y . $y . @ra; print $s; " FieFoeFoe3
        3. Reference, Deference.   The "canonical" syntax for scalar access is exactly orthogonal to that for arrays (or any other basic Perl data type):
          ${bareword     }
          ${symbolic reference  }
          ${hard reference   }
          Update: (See discussion of  @{ expression  } above):
          ${ expression      }
          The  ${ \$sftp->error } expression simply takes whatever scalar is returned by the method call  $sftp->error and takes a hard reference to it. The hard reference is then dereferenced normally, and the resulting scalar value can be used like any other scalar, e.g., in string interpolation.
        4. Comma.   The  , is just the comma operator (see perlop). In list context (as imposed, e.g., by the print built-in), it separates list items: scalars, arrays, expressions evaluating to scalars or arrays, etc. In
              warn "Put failed: ", $sftp->error, "\n";
          $sftp->error is just one more scalar item in the argument list of warn.
        Which way is best? That's up to you and the circumstances you face.

        Update: Some  [ ] square brackets snuck in where they shouldn't have been; e.g.,  @{[ bareword ]} should have been  @{ bareword } and etc., and likewise for the  ${[bareword]} discussion. Fixed.


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

      Update: This was intended to be a reply to Re^6: Net::SFTP::Foreign error. Please disregard.

      I assume you didn't understand the explanation given in the perlsecret link. Ok, so consider:

      1. Baby Cart.   An array variable may be accessed using this "canonical" (in the sense that it always works) syntax:
        @{bareword     }
        @{symbolic reference  }
        @{hard reference   }
        (A symbolic reference — sometimes casually called a "soft" reference — only works with package-global variables as used in the example below. That's one reason symbolic references are Officially Frowned Upon; see symbolic references in perlref and also Why it's stupid to `use a variable as a variable name' and also the perlfaq7 FAQ "How can I use a variable as a variable name?" for others. Symbolic referencing also only works when  strict 'refs' are disabled.) The "baby cart" just goes one step further and uses an anonymous array constructor  [ ... ] to generate an array hard reference to be dereferenced. E.g.:
        c:\@Work\Perl\monks>perl -wMstrict -le "no strict 'refs'; ;; our @array = qw(One Two Three); ;; my $soft_ref = 'array'; my $hard_ref = \@array; ;; print @{ array }, '*', @{ $soft_ref }, '*', @{ $hard_ref }; print qq/@{ array }-@{ $soft_ref }-@{ $hard_ref }/; ;; print qq/@{[ something_returning_a_list() ]}/; ;; sub something_returning_a_list { return qw(Nine Eight Seven); } " OneTwoThree*OneTwoThree*OneTwoThree One Two Three-One Two Three-One Two Three Nine Eight Seven
      2. Crab.   This is just string concatenation using the  . operator (see perlop). Scalars are stringized and concatenated together. Other data is handled in scalar context imposed by the concatenation operator. E.g.:
        c:\@Work\Perl\monks>perl -wMstrict -le "my ($x, $y, $z) = qw(Fie Foe Fum); my @ra = qw(foo bar baz); ;; my $s = $x . $y . $y . @ra; print $s; " FieFoeFoe3
      3. Reference, Deference.   The "canonical" syntax for scalar access is exactly orthogonal to that for arrays (or any other basic Perl data type):
        ${bareword     }
        ${symbolic reference  }
        ${hard reference   }
        The  ${ \$sftp->error } expression simply takes whatever scalar is returned by the method call to  $sftp->error and takes a hard reference to it. The hard reference is then dereferenced normally, and the resulting scalar value can be used like any other scalar, e.g., in string interpolation.
      4. Comma.   The  , is just the comma operator (see perlop). In list context (as imposed, e.g., by the print built-in), it separates list items: scalars, arrays, epressions evaluating to scalars or arrays, etc. In
            warn "Put failed: ", $sftp->error, "\n";
        $sftp->error is just one more scalar item in the argument list fed to warn.
      Which way is best? That's up to you and the circumstances you face.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-25 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found