1. File Handle Reading Blues: Rereading a File Handle for Input
Hi.
I can't reread a file handle after I have copied it on another file
handle for output. Here is the code snippet. Please let me know if
you need more.
Lines 26 & 27 are my attempts to get read the input file again. Line
18, where I copy the input file handle to the output file handle works
fine. I am using Perl 5.6.1 on HP-UX 11.00.
18 print INVARS_NW <INVARS_READ>;
19
20 chomp ($DB_MAIN_NAME=`echo \$profile`);
21 #system "echo \$profile";
22 print "DB_MAIN_NAME = '$DB_MAIN_NAME'";
23
24 print "\nGeorge W.";
25
26 while (<INVARS_READ>)
27 #while (print INVARS_NW <INVARS_READ>)
28 {
Thank you.
2. [ANN] reference 0.5 (was object reference handle (like perl's reference to scalar))
"Dave Burt" < XXXX@XXXXX.COM > schrieb im Newsbeitrag
news:gobfe.6148$ XXXX@XXXXX.COM ...
>
> "Robert Klemme" < XXXX@XXXXX.COM > wrote:
>>>> <soapbox>
>>>> It still might be worthwile, I just get the feeling that people try to
>>>> stuff things from other programming languages into Ruby because they
>>>> found them cute / useful / the only way to solve certain things in that
>>>> language. IMHO programming languages are different for a reason and
>>>> often it's much better to adhere to the old saying "when in Rome do as
>>>> the Romans do"... :-)
>>>> </soapbox>
>
>> "Dave Burt" < XXXX@XXXXX.COM > schrieb:
>>> I don't think what I've done here is anything like that.
>
>> I had to place that comment in some of these threads that I feel we're
>> seing more and more recently. I probably didn't pick the most
>> appropriate one... :-)
>
> There are two reasons I like these threads.
>
> The first is that when Perl- or C- or Whatever-thinkers ask these
> questions to this list, they will be exposed to the Ruby Way of solving a
> class of problems, and helped to free their mind.
I heartily agree here. But of course this needs some form of openness on
both sides, careful listening and considering arguments. Pure evangelism
doesn't help.
> Secondly, Ruby's a great language to push around and mould to your own
> whim as a programmer. It's a great language to create DSLs in. These
> interesting hacks are not going to displace the Ruby Way, but they help
> expand my thinking in a similar way to learning Ruby (where I learned to
> routinely use closures and meta-programming).
Yes, of course. However there's a difference between building a DSL and
modifying fundamental behavior of the language. The second approach is the
one that arouses my suspicion.
>>> ... all I'm proposing is a new container, for a single object.
>>>
>>> And I think Austin's PDF::Writer example is one case where it's useful
>>> (that is, passing an object by reference into a method so the method can
>>> replace the object).
>>
>> Yuck, may well be. On a side note, the fact that ruby methods can return
>> an arbitrary number of objects is a really great thing, that can also
>> help with some of these replacement problems.
>
> Heh, I think that's Austin's view, too. He seems to badly want to rewrite
> it :) Anyway, multiple returns are interesting. You get an extra container
> there, too (My Ref is just an extra container). They're often quite
> disobvious, and I'm sure there are cases where it would seem more obvious
> to modify an argument "in-place", even though "that's not possible in
> Ruby".
Personally I prefer to not have in place modification. If you view a method
as some kind of (mathematical) function, then there's an input and an
output. Returning values via arguments (= input) feels a bit awkward to me.
It has to be done in other languages because they don't provide multiple
return values; unfortunately this can make code hard to read. But since
Ruby actually has mutliple returns, I personally prefer to do it that way.
Kind regards
robert
4. object reference handle (like perl's reference to scalar)
5. File handling with subroutines and references
I am processing multiple files and am trying to create routines that
will efficiently read/write files. I am using variable names for the
FILEHANDLE to prevent any conflicts during processing (Is this a good
practice or is there a *better* way?).
use strict; use warnings;
no strict 'refs';
use Fcntl qw(:DEFAULT :flock);
sub file_open {
my ($fh, $file, $mode) = @_;
open($fh, "$mode $file") || die("Cannot open $file: $!");
flock($fh, LOCK_EX);
return \*$fh;
}
sub file_close {
my $fh = shift;
close($fh);
}
my $file = "data.txt";
my $fh = file_open("TEST_OUT", $file, '+<');
my $out = file_open("TEST_IN", "data_out.txt", '+>');
while (<$fh>) {
# process data
# ...
print $out $_;
}
file_close($out);
file_close($fh);
__END__
I tried:
sub file_open {
my ($fh, $file, $mode) = @_;
my $filehandle = do { local *$fh }; # <-- ADDED
open($filehandle, "$mode $file") || die("Cannot open $file: $!");
flock($filehandle, LOCK_EX);
return \*$filehandle;
}
which I saw in an example. I've seen it used for file "slurping", but I
am not sure if it is something I should use. My understanding is that
it is used to make the FILEHANDLE local, but in the context that I am
using, it doesn't seem necessary.
I know there are modules out there to use, but I am not using them
because I need the code to work with the standard 5.004 Perl install.
I'm looking for any code/design comments. Anything that I'm not doing
or that I should be doing?
Thanks,
Eric
6. reading from scalar File::Temp handle - Perl
7. Read quicker than a line per time from a file handle
Hi
I try to measure how fast an ftp download is going on the
fly. I do something like:
$|=1;
my $count =0;
open (CMD, "ftp.script 2>&1 |") or die "Can't execute: $!";
while (<CMD>){
print "Result: $_ \n";
if ($_ =~ /\#/) {
$count ++;
}
The problem is that the 'ftp.script' returns me the hashes, but all
toghter. So basically $count is only 1 after execution. In other words
the output that I get is:
Result: Connected to... bla bla
Result: Downloading ...
Result: ######################################################
Result: Download complete
What I want is:
Result: Connected to... bla bla
Result: Downloading ...
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
Result: #
....
Result: Download complete
I have tried $|=1; but it does not help. The original script does
write the hashes one by one to STDOUT. Is there any way to force
'ftp.script' to pass me the hashes one by one? I use a windows
machine.
Thanks
Carlo
8. Assigning one file handle to another file handle... - Perl