Sending data via ftp

  • Thread starter Marshall Dudley
  • Start date
M

Marshall Dudley

Is there any way to send a variable via ftp with perl? I am trying to
use NET::FTP, but I cannot find any way to tell it to send the data to
the other end. All I can find is to send a file from the hard drive to
the remote, but the data is not in a file, but created by the script
that is attempting to send it and in a simply text variable

Thanks,

Marshall
 
T

Tony Curtis

Marshall said:
Is there any way to send a variable via ftp with perl? I am trying to
use NET::FTP, but I cannot find any way to tell it to send the data to
the other end. All I can find is to send a file from the hard drive to
the remote, but the data is not in a file, but created by the script
that is attempting to send it and in a simply text variable

Have you ever pondered what the "F" in "FTP" stands for? :)

Write the data into a temporary file and send that, e.g. through File::Temp.

hth
t
 
X

xhoster

Marshall Dudley said:
Is there any way to send a variable via ftp with perl? I am trying to
use NET::FTP, but I cannot find any way to tell it to send the data to
the other end. All I can find is to send a file from the hard drive to
the remote, but the data is not in a file, but created by the script
that is attempting to send it and in a simply text variable

Net::FTP (case matters) can send either files, or the contents read from a
file handle.

Either save the variable to a temp file and then send the file, or
make the variable look like a filehandle. For doing the latter, see
IO::Scalar.

Xho
 
A

anno4000

Marshall Dudley said:
Is there any way to send a variable via ftp with perl? I am trying to
use NET::FTP, but I cannot find any way to tell it to send the data to
the other end. All I can find is to send a file from the hard drive to
the remote, but the data is not in a file, but created by the script
that is attempting to send it and in a simply text variable

Did you read this paragraph?

put ( LOCAL_FILE [, REMOTE_FILE ] )
Put a file on the remote server. "LOCAL_FILE" may be a name or a
filehandle. If "LOCAL_FILE" is a filehandle then "REMOTE_FILE"
must be specified. If "REMOTE_FILE" is not specified then the file
will be stored in the current directory with the same leafname as
"LOCAL_FILE".

The local file doesn't have to be on the hard drive, you can specify
a file handle. Then the data could come from anywhere, for instance
from print statements in your code.

I don't think that FTP is the right tool for what you want to do,
but just for fun, here is how it *could* be done:

You have one problem: The call to ->put only returns after the
transfer is finished, so you don't get a chance to print the data
in the same process that runs ->put. You'll need to fork another
process for that and use a pipe to communicate between the two.

use Net::FTP;

my $ftp = Net::FTP->new( 'host') or die 'new';
$ftp->login( 'user', 'password') or die 'login';
pipe( my ( $r, $w)) or die 'pipe';

if ( my $pid = fork ) {
defined( $pid) or die 'fork';
close $r;
print $w "$_\n" for qw( hihi haha hoho);
} else {
close $w;
$ftp->put( $r, '/remote/file') or die 'put';
}

Anno
 
A

anno4000

Net::FTP (case matters) can send either files, or the contents read from a
file handle.

Either save the variable to a temp file and then send the file, or
make the variable look like a filehandle. For doing the latter, see
IO::Scalar.

Ah... very good idea. Much more compact than the rather low-level
pipe/fork solution I posted.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top