Differences
This shows you the differences between the selected revisions of the page.
library_perl 2012-03-02 | library_perl 2022-06-16 (current) | ||
Line 1: | Line 1: | ||
====== Using WinSCP .NET Assembly from Perl (ActivePerl) ====== | ====== Using WinSCP .NET Assembly from Perl (ActivePerl) ====== | ||
- | &beta_feature | + | //This article is based on [[https://www.activestate.com/products/perl/|ActivePerl]] Perl distribution.// |
- | + | ||
- | //This article is based on [[http://www.activestate.com/activeperl|ActivePerl]] Perl distribution.// | + | |
===== Installing and Registering for COM ===== | ===== Installing and Registering for COM ===== | ||
Line 13: | Line 11: | ||
==== [[enums]] Accessing Enumeration Values ==== | ==== [[enums]] Accessing Enumeration Values ==== | ||
- | As Perl accesses COM classes via ''IDispatch'', it does not make use of type library, hence it does not have direct access to constants defined there, like ''[[library_sessionoptions#properties|Protocol.Sftp]]'' for instance. | + | As Perl accesses COM classes via ''IDispatch'', it does not make use of type library, hence it does not have direct access to constants defined there, like ''[[library_sessionoptions#protocol|Protocol.Sftp]]'' for instance. |
One of several ways to use these constants is to use ''%%Win32::OLE::Const->Load%%'' method. It returns hash array of all enum members from type library, with keys like ''<type>_<member>'', e.g. ''Protocol.Sftp'' becomes ''Protocol_Sftp''. | One of several ways to use these constants is to use ''%%Win32::OLE::Const->Load%%'' method. It returns hash array of all enum members from type library, with keys like ''<type>_<member>'', e.g. ''Protocol.Sftp'' becomes ''Protocol_Sftp''. | ||
Line 32: | Line 30: | ||
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'}; | $sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'}; | ||
- | $sessionOptions->{'HostName'} = "example.com"; | + | $sessionOptions->{'HostName'} = 'example.com'; |
- | $sessionOptions->{'UserName'} = "user"; | + | $sessionOptions->{'UserName'} = 'user'; |
- | $sessionOptions->{'Password'} = "mypassword"; | + | $sessionOptions->{'Password'} = 'mypassword'; |
- | $sessionOptions->{'SshHostKey'} = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | $sessionOptions->{'SshHostKeyFingerprint'} = 'ssh-rsa 2048 xxxxxxxxxxx...'; |
</code> | </code> | ||
- | ==== Event Handlers ==== | + | ==== [[event_handlers]] Event Handlers ==== |
The ''[[library_session|Session]]'' class exposes several [[library_session#events|events]]. | The ''[[library_session|Session]]'' class exposes several [[library_session#events|events]]. | ||
Line 48: | Line 46: | ||
See following code snippet: | See following code snippet: | ||
<code perl> | <code perl> | ||
+ | use feature qw(switch); # in order to use given | ||
+ | |||
sub session_Events() | sub session_Events() | ||
{ | { | ||
Line 57: | Line 57: | ||
{ | { | ||
my ($sender, $e) = @args; | my ($sender, $e) = @args; | ||
- | print $e->{'FileName'} . " => " . $e->{'Destination'} . "\n"; | + | printf "%s => %s\n", ( $e->{'FileName'}, $e->{'Destination'} ); |
} | } | ||
} | } | ||
Line 80: | Line 80: | ||
</code> | </code> | ||
- | ===== Example ===== | + | ===== [[example]] Example ===== |
This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]]. | This example is functionally equivalent to [[library#example|overall C# example for WinSCP .NET assembly]]. | ||
Line 86: | Line 86: | ||
use strict; | use strict; | ||
use warnings; | use warnings; | ||
- | use feature qw(switch); | ||
use Win32::OLE; | use Win32::OLE; | ||
Line 97: | Line 96: | ||
FALSE => Variant(VT_BOOL, 0) | FALSE => Variant(VT_BOOL, 0) | ||
}; | }; | ||
- | |||
- | # Required for WithEvents to stop complaining | ||
- | Win32::OLE->Initialize(Win32::OLE::COINIT_OLEINITIALIZE); | ||
my $session = Win32::OLE->new('WinSCP.Session'); | my $session = Win32::OLE->new('WinSCP.Session'); | ||
- | Win32::OLE->WithEvents($session, \&session_Events); | ||
# Load constants | # Load constants | ||
Line 111: | Line 106: | ||
$sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'}; | $sessionOptions->{'Protocol'} = $consts->{'Protocol_Sftp'}; | ||
- | $sessionOptions->{'HostName'} = "example.com"; | + | $sessionOptions->{'HostName'} = 'example.com'; |
- | $sessionOptions->{'UserName'} = "user"; | + | $sessionOptions->{'UserName'} = 'user'; |
- | $sessionOptions->{'Password'} = "mypassword"; | + | $sessionOptions->{'Password'} = 'mypassword'; |
- | $sessionOptions->{'SshHostKey'} = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"; | + | $sessionOptions->{'SshHostKeyFingerprint'} = 'ssh-rsa 2048 xxxxxxxxxxx...'; |
# Connect | # Connect | ||
Line 120: | Line 115: | ||
# Upload files | # Upload files | ||
- | my $transferOptions = WScript.CreateObject("WinSCP.TransferOptions"); | + | my $transferOptions = Win32::OLE->new('WinSCP.TransferOptions'); |
$transferOptions->{'TransferMode'} = $consts->{'TransferMode_Binary'}; | $transferOptions->{'TransferMode'} = $consts->{'TransferMode_Binary'}; | ||
- | my $transferResult = $session->PutFiles("d:\\toupload\\*", "/home/user/", FALSE, $transferOptions); | + | my $transferResult = |
+ | ···$session->PutFiles('d:\\toupload\\*', '/home/user/', FALSE, $transferOptions); | ||
# Throw on any error | # Throw on any error | ||
- | $transferResult.Check(); | + | $transferResult->Check(); |
# Print results | # Print results | ||
my $items = Win32::OLE::Enum->new($transferResult->{'Transfers'}); | my $items = Win32::OLE::Enum->new($transferResult->{'Transfers'}); | ||
my $item; | my $item; | ||
- | |||
while (defined($item = $items->Next)) | while (defined($item = $items->Next)) | ||
{ | { |