Tie a file handle to a variable

C

Carlo Filippini

Hi
how can I tie a file handle to a variable (probably tie is not
the right perl terminology in this case)? Basically I want that
everything that is written to a file is copied into a variable as
well. Of course I could read the file again, something like

while (<FH>){
....
}

but I need to chenge the variable dinamically.
Is it possible somehow?

Thanks
Carlo
 
K

Kris Wempa

It sounds to me like you just want to store everything currently read from
the file into a string variable. I've actually had to do this in the past.
The easiest way is to just keep appending lines to a string variable. Then
when you close the filehandle you have the entire thing stored in a string.
Here's an example:

$entire_file = "";

open(FILE, "< myfile.txt");
while ($line = <FILE>) {
$entire_file .= $line; # append new line to your string

## do whatever else you need to do
}
close(FILE);

## now $entire_file contains the entire contents of myfile.txt

A very quick way to do the same thing is to just undef the record separator
and only read from the filehandle once such as:

undef $/;
open(FILE, "< myfile.txt");
$entire_file = <FILE>;

### again, now $entire_file has the entire contents of myfile.txt

The first way is better if you actually need to do other things while you
are reading the file. Let me know if this is what you were asking.
 
C

Carlo Filippini

Thanks Kris
but this is not what I meant.
I need to do something mode dynamic. My Problem is that I have a
method (the hash method for the Net::Ftp class) that writes to a file
handle. So basically I have a method that writes to a file like:

$ftp = Net::FTP->new("some.host.name", Debug => 0);
$ftp->login("anonymous",'-anonymous@');
$ftp->hash (\*MYFILE);
$ftp->get("that.file");

Normally you could display the hashes on the \*STDERR or on the
\*STDOUT.
But in my case, in the meantime that I download the file, I want to
display that something is happening for the user in a Tk window, not
on the STDOUT. So I need a variable that will be my -textvariable
attribute for some widget. Is it clear what I need?

Thanks for any help
Carlo
 
K

Kris Wempa

OK, that seems to be quite different than what I originally thought.
Although I'm not familiar with the Net::Ftp class, I read the documentation
at CPAN and I understand what the hash() method is doing. Unfortunately,
I'm not familiar with Tk at all. If you explain what the -textvariable
attribute means, I may be able to help you. Is this variable supposed to
show the hash marks ? If so, how does it update as the file contents change
? I'm not sure if this helps you, but you could always assign the fileref
to another variable and use that in your script:

$hash_output = \*MYFILE;
$ftp->hash ($hash_output);
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top