in reply to Using Perl FTP to write a string variable as a file

The perldoc for Net::FTP says that you can give put a filehandle. All you'd have to do is something like this:
open(FH, "<", \$string) or die "Whoops!"; $ftp->put(FH, "remote_file.name");
I have not tested this at all.
Update: Added \$string as per Roy.

Replies are listed 'Best First'.
Re^2: Using Perl FTP to write a string variable as a file
by Roy Johnson (Monsignor) on Dec 27, 2004 at 22:20 UTC
    I think you want \$string there. Opening a reference to a variable reads the string in the variable as if it were a file. If you just have a scalar variable, it will try to open a file by the name of whatever is in the variable.

    Caution: Contents may have been coded under pressure.
Re^2: Using Perl FTP to write a string variable as a file
by pvilleSE (Novice) on Aug 24, 2006 at 17:09 UTC
    I know this is an old post just thought I'd comment that we needed to do this, so we tried the above code and it worked flawlessly.
Re^2: Using Perl FTP to write a string variable as a file
by toohoo (Beadle) on Dec 22, 2014 at 08:53 UTC

    Hello wise men,

    may I ask please, if this topic was solved? I have the same issue her but may be under other circumstances.

    My situation is as follows, I did in the past a save of a file first and then trasfered it via Net::FTP. Now I get the content of the file and a file name and have to do FTP without saving it first.

    I did understand that the first hint was to open a file handle with the pointer to a scalar variable. I will test this but I also would appreciate to know, if some reader of this had tested it and was successfully.

    thanks in advance, regards, Thomas

      Hello again ...

      I have tested it with a testscript. It does not work. The error message is:

      Cannot open Local file FH: No such file or directory at test-ftpscalar.pl line 54 put failed (Passive=1) Type set to I ; localfilename(FH): [d:/Dokumente2/Server2/cgi-bin/out//test-ftpscala +r.xml], remotefilename: [test-ftpscalar.xml] at test-ftpscalar.pl lin +e 54.

      The script is as follows:

      #!/usr/bin/perl print "Content-type: text/html"; print "<html>\n<head>\n<title>Test FTP Sclar</title>\n</head>\n<body>\ +n<p>___ hier gehts los ___:</p>\n<p>\n"; use Net::FTP; my $content = <<__CONTENT__; <inno> <antrag> <tester>THOFMANN(TH)</tester> </antrag> <hinweis> <ziel>Test-Server</ziel> </hinweis> </inno> __CONTENT__ my $server = 'www.lly5.de'; my ($login, $passwd) = ('theoneandonly', 'g1v3m3s0m35h17'); my $localdir = "d:/Dokumente2/Server2/cgi-bin/out/"; my $datei = "test-ftpscalar.pl"; $datei = "test-ftpscalar.xml"; my $remotefile = ''; my $remotefilename; my $passive; my $remotedir = 'public_html/inno/'; my $localfilename; $localfilename = "$localdir\/$datei"; if ($remotefile ne '') { $remotefilename = $remotefile; } else { $remotefilename = $datei; } if ($server =~ m/(www.lly5.de)/i) { # fies, name wurde i +n IP geaendert $passive = 1; $ftp = Net::FTP->new($server, Passive => 1) or die "Cannot connect to $server (Passive=$passiv +e): $@"; } else { $ftp = Net::FTP->new($server) or die "Cannot connect to $server: $@"; } open( FH, "<", \$content) or die "Kann File Handle nic +ht zum lesen aus content oeffnen"; $ftp->login($login, $passwd) or die "Cannot login (Passive=$passive) ", $ftp->m +essage; $ftp->cwd($remotedir) or die "Cannot change working directory (Passive=$ +passive) ", $ftp->message; $ftp->binary or die "Cannot change to binary mode (Passive=$pas +sive) ", $ftp->message; $ftp->put(FH, $remotefilename) or die "put failed (Passive=$passive) ", $ftp->mes +sage, "; localfilename(FH): [$localfilename], remotefilename: [$remot +efilename]"; $ftp->quit; close( FH ); print "*** Fertig FTP ***\n";

      Thanks in advance, Thomas

        Try using a lexical filehandle

        open( my $FH, "<", \$content ) or die "Kann File Handle nicht zum lesen aus content oeffnen"; . . $ftp->put( $FH, $remotefilename ) or die "put failed (Passive=$passive) ", $ftp->message, "; local +filename(FH): [$localfilename], remotefilename: [$remotefilename]"; $ftp->quit; close( $FH );
        poj