Can I "trick" Perl into thinking a variable is a file?

U

usenet

I have an ordinary scalar variable. I want to sFTP that to a remote
host, placing the value of my variable ($foo) into a file on the remote
host.

The Net::SFTP module uses this syntax:

my $sftp = Net::SFTP->new($host);
$sftp->put($local, $remote [, \&callback ])

where $local and $remote specify filenames.

But I don't want to bother with creating a temporary file on my local
host - I just want to send the value of my scalar variable $foo.

Is there a way I can make Perl treat $foo (some data) as $local (a file
identifier) so I can transfer the value of $foo without creating a
temporary file?

Thanks!
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I have an ordinary scalar variable. I want to sFTP that to a remote
host, placing the value of my variable ($foo) into a file on the
remote host.

The Net::SFTP module uses this syntax:

my $sftp = Net::SFTP->new($host);
$sftp->put($local, $remote [, \&callback ])

where $local and $remote specify filenames.

IO::Scalar,

<URL:http://search.cpan.org/~dskoll/IO-stringy-2.110/lib/IO/Scalar.pm>

in combination with the do_open, do_write, and do_close methods of
Net::SFTP, might be useful, but I have not tested this.

Sinan.
 
J

Jay Tilton

(e-mail address removed) wrote:

: I have an ordinary scalar variable. I want to sFTP that to a remote
: host, placing the value of my variable ($foo) into a file on the remote
: host.
:
: The Net::SFTP module uses this syntax:
:
: my $sftp = Net::SFTP->new($host);
: $sftp->put($local, $remote [, \&callback ])
:
: where $local and $remote specify filenames.
:
: But I don't want to bother with creating a temporary file on my local
: host - I just want to send the value of my scalar variable $foo.
:
: Is there a way I can make Perl treat $foo (some data) as $local (a file
: identifier) so I can transfer the value of $foo without creating a
: temporary file?

Net::SFTP doesn't appear to support it. That is, $local can't be anything
but a filename.

Pity, that. If Net::SFTP was savvy enough to recognize when $local is a
filehandle, you could have your wish with an often overlooked ability of
open(): opening a reference to a scalar treats the scalar like it's a
file, e.g.

open my $local, '<', \$string;

Might be worth mentioning to the author of Net::SFTP. Adding that
capability shouldn't be much trouble.

Best idea I can come up with that avoids a temporary file is to create a
named pipe you can stuff with your data, then feed its name to the put()
method.
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top