File Coverage

blib/lib/IO/All/Securftp/iowrap.pm
Criterion Covered Total %
statement 24 76 31.5
branch 0 26 0.0
condition 0 9 0.0
subroutine 8 17 47.0
pod 5 5 100.0
total 37 133 27.8


line stmt bran cond sub pod time code
1             package IO::All::Securftp::iowrap;
2              
3 2     2   11 use warnings;
  2         2  
  2         68  
4 2     2   10 use strict;
  2         3  
  2         73  
5              
6             =head1 NAME
7              
8             IO::All::Securftp::iowrap - provides I/O wrapper for better integration of Net::SFTP::Foreign into IO::All
9              
10             =head1 DESCRIPTION
11              
12             IO::All::Securftp::iowrap is the handler for IO::All for opened sftp connections for reading and writing.
13              
14             =cut
15              
16 2     2   13 use Carp qw(croak);
  2         3  
  2         137  
17 2     2   2385 use Net::SFTP::Foreign;
  2         119224  
  2         89  
18 2     2   22 use Scalar::Util qw(blessed);
  2         4  
  2         134  
19 2     2   276538 use URI;
  2         12690  
  2         79  
20              
21 2     2   1521 use namespace::clean;
  2         35582  
  2         17  
22              
23             our $VERSION = "0.001";
24              
25             {package # ...
26             URI::securftp;
27              
28 2     2   1910 use parent "URI::ssh";
  2         727  
  2         11  
29             }
30              
31             =head1 METHODS
32              
33             =head2 new
34              
35             Extracts user/port from URI, if any - and passes parsed URI to C<< Net::SFTP::Foreign->new >> together with optional specified C hash in C<%options>:
36              
37             IO::All::Securftp::iowrap->new($self->name, {
38             new => {
39             timeout => ...,
40             },
41             get_content => {
42             conversion => ...,
43             },
44             put_content => {
45             atomic => 1,
46             }
47             });
48              
49             For details regarding the options to new, get_content and put_content see L.
50              
51             =cut
52              
53             sub new
54             {
55 0 0 0 0 1   @_ == 2 or @_ == 3 or croak('IO::All::Securftp::iowrap->new($name, \%options?)');
56 0           my $class = shift;
57 0           my $name = shift;
58 0           my %options;
59 0 0         @_ and %options = %{ shift @_ };
  0            
60 0           my %obj;
61              
62 0 0         $obj{uri} = blessed $name ? $name : URI->new($name);
63 0           $obj{options} = \%options;
64              
65 0 0 0       defined $obj{uri}->userinfo and $options{user} //= $obj{uri}->userinfo;
66 0 0 0       defined $obj{uri}->_port and $options{port} //= $obj{uri}->_port;
67              
68 0 0         my %new_opts = ( $options{new} ? ( %{$options{new}}) : () );
  0            
69 0           $obj{sftp} = Net::SFTP::Foreign->new($obj{uri}->host, %new_opts);
70              
71 0           bless \%obj, $class;
72             }
73              
74             sub _fetch
75             {
76 0     0     my $self = shift;
77 0           my $fh;
78 0 0         my %fetch_opts = ( $self->{options}->{get_content} ? ( %{$self->{options}->{get_content}}) : () );
  0            
79 0           $self->{_cnt} = $self->{sftp}->get_content($self->{uri}->path, %fetch_opts);
80 0           CORE::open $fh, "<", \$self->{_cnt};
81 0           $self->{content} = $fh;
82             }
83              
84             sub _preappend
85             {
86 0     0     my $self = shift;
87 0           my $fh;
88 0 0         my %fetch_opts = ( $self->{options}->{get_content} ? ( %{$self->{options}->{get_content}}) : () );
  0            
89 0           $self->{_cnt} = $self->{sftp}->get_content($self->{uri}->path, %fetch_opts);
90 0           CORE::open $fh, ">>", \$self->{_cnt};
91 0           $self->{dirty} = 1;
92 0           $self->{content} = $fh;
93             }
94              
95             sub _preput
96             {
97 0     0     my $self = shift;
98 0           my $fh;
99 0           $self->{_cnt} = "";
100 0           CORE::open $fh, ">", \$self->{_cnt};
101 0           $self->{dirty} = 1;
102 0           $self->{content} = $fh;
103             }
104              
105             =head2 getline
106              
107             Reads a single line from remote file
108              
109             =cut
110              
111             sub getline
112             {
113 0     0 1   my $self = shift;
114 0 0         $self->{content} or $self->_fetch;
115 0           $self->{content}->getline;
116             }
117              
118             =head2 getlines
119              
120             Reads all lines from remote file
121              
122             =cut
123              
124             sub getlines
125             {
126 0     0 1   my $self = shift;
127 0 0         $self->{content} or $self->_fetch;
128 0           $self->{content}->getlines;
129             }
130              
131             =head2 print
132              
133             Print given lines to remote file
134              
135             =cut
136              
137             sub print
138             {
139 0     0 1   my $self = shift;
140 0 0         $self->{content} or $self->_fetch;
141 0           $self->{content}->print(@_);
142             }
143              
144             =head2 close
145              
146             Closes remote connection, flush buffers before when necessary
147              
148             =cut
149              
150             sub close
151             {
152 0     0 1   my $self = shift;
153 0 0         my %put_opts = ( $self->{options}->{put_content} ? ( %{$self->{options}->{put_content}}) : () );
  0            
154 0 0         $self->{dirty} and $self->{sftp}->put_content($self->{_cnt}, $self->{uri}->path, %put_opts);
155 0           delete @$self{qw(dirty sftp _cnt uri content)};
156 0           return;
157             }
158              
159             =head2 DESTROY
160              
161             =cut
162              
163             sub DESTROY
164             {
165 0     0     $_[0]->close;
166             }
167              
168             =head1 AUTHOR
169              
170             Jens Rehsack, C<< >>
171              
172             =head1 BUGS
173              
174             Please report any bugs or feature requests to
175             C, or through the web interface at
176             L.
177             I will be notified, and then you'll automatically be notified of progress
178             on your bug as I make changes.
179              
180             =head1 SUPPORT
181              
182             You can find documentation for this module with the perldoc command.
183              
184             perldoc IO::All::Securftp
185              
186             You can also look for information at:
187              
188             =over 4
189              
190             =item * RT: CPAN's request tracker
191              
192             L
193              
194             =item * AnnoCPAN: Annotated CPAN documentation
195              
196             L
197              
198             =item * CPAN Ratings
199              
200             L
201              
202             =item * Search CPAN
203              
204             L
205              
206             =back
207              
208             =head1 ACKNOWLEDGEMENTS
209              
210             =head1 LICENSE AND COPYRIGHT
211              
212             Copyright 2015 Jens Rehsack.
213              
214             This program is free software; you can redistribute it and/or modify it
215             under the terms of either: the GNU General Public License as published
216             by the Free Software Foundation; or the Artistic License.
217              
218             See http://dev.perl.org/licenses/ for more information.
219              
220             =head1 SEE ALSO
221              
222             =cut
223              
224             1; # end of IO::All::Securftp::iowrap