Hi all,
Hello,
First post to the group. I have two questions, but they go hand in hand. The first:
Net::FTP documentation reports that get syntax is:
get ( REMOTE_FILE [, LOCAL_FILE [, WHERE]] )
I do the following (with $ftp initialization omitted here):
local $fh = IO::File->new_tmpfile; $ftp->get($dump_path, $fh); print $fh; (or print <$fh>;)
When I run this, I get no errors, but I can not get the text into the temporary file. I have also tried:
open(FILE, "> anything.txt"); $ftp->get($dump_path, FILE);
Here, the file is created, but contains no text.
To show this works otheriwse, if I try:
$ftp->get($dump_path, "> anything.txt");
sure enough, It works great.
What am I doing wrong in the first example?
Wow, so many problems in so few lines of code. :-)
local() is a holdover from old versions of Perl. You should use my() or our() instead.
From IO::File:
new_tmpfile
Creates an "IO::File" opened for read/write on a newly created
temporary file. On systems where this is possible, the temporary
^^^^^^^^^^^^^
file is anonymous (i.e. it is unlinked after creation, but held
^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
open). If the temporary file cannot be created or opened, the
"IO::File" object is destroyed. Otherwise, it is returned to the
caller.
What that means is that you get a file that does not have a name, the only way you can access it is through the filehandle. The point being that once you close the filehandle the data is lost.
And finally, after $ftp->get() prints to the file you are trying to print to the file again with "print $fh;" which is the same as "print $fh $_;" (or "print <$fh>;" which is the same as "print readline $fh;")
In your second example you try to open a file and even if the file could not be opened you try to write data to it.
And secondly you pass a bareword to $ftp->get() instead of a valid filehandle.
In your third example you pass the string "> anything.txt" to $ftp->get() as the file name to use. Do you really want a file name that starts with the two characters '> '?
To pass a lexical filehandle:
open my $fh, '>', 'anything.txt' or die "Cannot open 'anything.txt' $!"; $ftp->get( $dump_path, $fh ) or die "Cannot get $dump_path\n";
To pass a normal filehandle:
open FILE, '>', 'anything.txt' or die "Cannot open 'anything.txt' $!"; $ftp->get( $dump_path, \*FILE ) or die "Cannot get $dump_path\n";
Or to pass a file name:
$ftp->get( $dump_path, 'anything.txt' ) or die "Cannot get $dump_path\n";
Second question:
The following is from the get() subroutine in the FTP Perl Module:
$localfd = ref($local) || ref(\$local) eq "GLOB" ? fileno($local) : undef;
($local = $remote) =~ s#^.*/## unless(defined $local);
where $local comes from:
my($ftp,$remote,$local,$where) = @_;
Could someone explain what this code is doing (several things happening here I havn't seen before, I was looking at this to see what was wrong with how I was using a filehandle.)
The first line uses fileno() to determine if $local is a valid filehandle. The second line copies the path in $remote to $local and then removes everything except the filename.
John -- use Perl; program fulfillment
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>