filehandle

Why does Programming Perl use local (not my) for filehandles?

こ雲淡風輕ζ 提交于 2019-11-29 00:52:04
问题 When I read through Programming Perl , 2nd Edition, Page 51, something confuses me : sub newopen { my $path = shift; local *FH; #not my! open (FH, $path) || return undef; return *FH; } $fh = newopen('/etc/passwd'); My I know, why we are not recommenced to use my? So far, I cannot see anything will go wrong if we use my(). Thanks! 回答1: The trite answer is that you have to use local because my *FH is a syntax error. The "right" (but not very enlightening) answer is that you're doing it wrong.

How can I use __DATA__ twice?

拟墨画扇 提交于 2019-11-28 22:15:32
问题 How can I use __DATA__ twice? #!/usr/local/bin/perl use warnings; use 5.012; while ( <DATA> ) { print; } while ( <DATA> ) { chomp if $. == 1; print scalar reverse; print "\n" if eof; } __DATA__ one two three four five six 回答1: To use the DATA filehandle twice you need to rewind it. The tricky bit is that if you do seek(DATA, 0, 0) , it'll be positioned to the first source line, not the line after __DATA__ . Therefore you need to save the position first: my $data_start = tell DATA; # save the

Delphi - finding the process that is accessing a file from my program

狂风中的少年 提交于 2019-11-28 20:42:31
I have a Delphi app that regularly writes to a local disk file. Occasionally it is unable to access the file - a sharing violation results when it tries to open it. A retry after a short delay is all that is needed, but when it occurs, I would like to report the process that prevented the access. Is it feasible when a sharing violation occurs for my program to enumerate all the file handles in use, inspect the filename, and if it matches the name of my data file, retrieves the process name associated with that handle? Some example code would be nice. You have basically two ways The Easy Way if

Can I find a filename from a filehandle in Perl?

六月ゝ 毕业季﹏ 提交于 2019-11-28 09:43:00
open(my $fh, '>', $path) || die $!; my_sub($fh); Can my_sub() somehow extrapolate $path from $fh? A filehandle might not even be connected to a file but instead to a network socket or a pipe hooked to the standard output of a child process. If you want to associate handles with paths your code opens, use a hash and the fileno operator, e.g. , my %fileno2path; sub myopen { my($path) = @_; open my $fh, "<", $path or die "$0: open: $!"; $fileno2path{fileno $fh} = $path; $fh; } sub myclose { my($fh) = @_; delete $fileno2path{fileno $fh}; close $fh or warn "$0: close: $!"; } sub path { my($fh) = @_

I can create filehandles to strings in Perl 5, how do I do it in Perl 6?

孤者浪人 提交于 2019-11-27 23:32:54
问题 In Perl 5, I can create a filehandle to a string and read or write from the string as if it were a file. This is great for working with tests or templates. For example: use v5.10; use strict; use warnings; my $text = "A\nB\nC\n"; open(my $fh, '<', \$text); while(my $line = readline($fh)){ print $line; } How can I do that in Perl 6? The following doesn't work for Perl 6 (at least not for my instance of Perl6 running on MoarVM 2015.01 from the January 2015 release of Rakudo Star on 64-bit

How can I test if I can write to a filehandle?

走远了吗. 提交于 2019-11-27 13:20:33
I have some subroutines that I call like this myWrite($fileName, \@data) . myWrite() opens the file and writes out the data in some way. I want to modify myWrite so that I can call it as above or with a filehandle as the first argument. (The main reason for this modification is to delegate the opening of the file to the calling script rather than the module. If there is a better solution for how to tell an IO subroutine where to write, i'd be glad to hear it.) In order to do this, I must test whether the first input var is a filehandle. I figured out how to do that by reading this question .

copying the contents of a binary file

筅森魡賤 提交于 2019-11-27 09:22:30
I am designing an image decoder and as a first step I tried to just copy the using c. i.e open the file, and write its contents to a new file. Below is the code that I used. while((c=getc(fp))!=EOF) fprintf(fp1,"%c",c); where fp is the source file and fp1 is the destination file. The program executes without any error, but the image file(".bmp") is not properly copied. I have observed that the size of the copied file is less and only 20% of the image is visible, all else is black. When I tried with simple text files, the copy was complete. Do you know what the problem is? Make sure that the

Can I find a filename from a filehandle in Perl?

非 Y 不嫁゛ 提交于 2019-11-27 03:09:06
问题 open(my $fh, '>', $path) || die $!; my_sub($fh); Can my_sub() somehow extrapolate $path from $fh? 回答1: A filehandle might not even be connected to a file but instead to a network socket or a pipe hooked to the standard output of a child process. If you want to associate handles with paths your code opens, use a hash and the fileno operator, e.g. , my %fileno2path; sub myopen { my($path) = @_; open my $fh, "<", $path or die "$0: open: $!"; $fileno2path{fileno $fh} = $path; $fh; } sub myclose {

Why does Image.FromFile keep a file handle open sometimes?

对着背影说爱祢 提交于 2019-11-26 22:33:18
I am doing a lot of image processing in GDI+ in .NET in an ASP.NET application. I frequently find that Image.FromFile() is keeping a file handle open. Why is this? What is the best way to open an image without the file handle being retained. NB: I'm not doing anything stupid like keeping the Image object lying around - and even if I was I woudlnt expect the file handle to be kept active jamiecon I went through the same journey as a few other posters on this thread. Things I noted: Using Image.FromFile does seem unpredictable on when it releases the file handle. Calling the Image.Dispose() did

How can I test if I can write to a filehandle?

拟墨画扇 提交于 2019-11-26 22:22:57
问题 I have some subroutines that I call like this myWrite($fileName, \@data) . myWrite() opens the file and writes out the data in some way. I want to modify myWrite so that I can call it as above or with a filehandle as the first argument. (The main reason for this modification is to delegate the opening of the file to the calling script rather than the module. If there is a better solution for how to tell an IO subroutine where to write, i'd be glad to hear it.) In order to do this, I must test