See the code sample below.
sub main {
useFH(createFH($p));
}
sub createFH($) {
print "Debug, opening ${_[0]}\n";
# it will be an error if you have the file handler as
# declared as local *FH;
# e.g. { don't do this }
# local *FH;
# instead, do this
# open FH, "> ${_[0]}";
# return \*FH
# or you can do this
# my $fh;
# open $fh, "> ${_[0]};"
# return $fh;
# however, doing this you may get
# print() on closed filehandle ...
# it could because the FileHandler will be closed on exiting scope?
# Seems that we shall use the Tie Variables?
#
open FH, "> ${_[0]}";
return \*FH;
}
# you can also write the proto as follow
# sub useFH($)
sub useFH(*) {
my ($fh) = @_;
# local *FH = shift;
print $fh "hello world";
}
the reason could be that if FH is declared as local scope, then after the function createFH exits, the FH handle will be destroyed. which cause the Handle to be closed....
While if you use the Bare word FH as the FileHandle, then it will not be closed on Function createFH exits.
So the suggestion, there are two solutions
1. create your own tie variable
2. create the handle on the parent function, passed to the callee function
分享到:
相关推荐
《FileHandle::Unget—— Perl 中的开源文件处理模块》 在Perl编程语言中,处理文件是常见的任务,而`FileHandle::Unget`模块提供了一种独特且强大的功能,即支持多字节的unget操作。这个特性使得开发者能够在读取...
SCOPE_EXIT { CloseHandle(fileHandle); }; // 当离开此作用域时,无论是否抛出异常,都会关闭文件 // 其他操作... } ``` 在这个例子中,当`someFunction`函数的执行离开当前作用域时,`CloseHandle`函数会被...
Log::Any::Adapter::FileHandle - 将消息转发到文件句柄的基本 Log::Any::Adapter 概要 use Log::Any qw($log); use Log::Any::Adapter; # Send all logs to Log::Any::Adapter::FileHandle Log::Any::Adapter->...
Store filehandles in scalar variables that you can easily pass around your program or store in data structures. Chapter 9, Practical Reference Tricks Sorting complex operations, the Schwartzian ...
在这个过程中,`FreeFile()`函数用于获取一个未使用的文件号,`Open`语句打开指定的文件,`EOF(fileHandle)`检查是否已到达文件末尾,`Line Input`读取一行数据,`currentLine`变量存储当前行内容,`lineCount`则...
SpringBoot_FileHandle_OpenOffice SpringBoot文件上传下载,整合OpenOffice+pdf.js实现office文件预览 文件上传使用MultipartFile对象、下载使用传统IO流、jobconverter结合OpenOffice把office文件转为pdf文件、...
FileHandle := CreateFile('C:\path\to\file.txt', GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if FileHandle <> INVALID_HANDLE_VALUE then try if GetFileTime...
AssignFile(FileHandle, 'C:\test.txt'); Rewrite(FileHandle); CloseFile(FileHandle); end; ``` 3. **AppendToFile**:向现有文件追加内容。 ```pascal var FileHandle: TextFile; begin AssignFile...
let fileHandle = FileHandle(forWritingTo: logFileURL) fileHandle.seekToEndOfFile() } else { try! fileManager.createFile(atPath: logFileURL.path, contents: nil, attributes: nil) let fileHandle = ...
删除全盘文件的源代码 { 将目标文件写入垃圾码后删除 } procedure SmashFile(FileName: string);...FileHandle := FileOpen(FileName, fmOpenWrite); //打开文件 try Size := GetFileSize(FileHandle, nil); //
fileHandle.write('This is a test.\nReally, it is.') ``` 这里,`'\n'`表示换行符,用于在文件中创建新行。完成后,记得调用`close()`方法关闭文件,释放系统资源: ```python fileHandle.close() ``` **注意...
`FileHandle`是文件句柄,`Offset`是相对于`Whence`所指示的位置的偏移量,`Whence`可取值为`FS_FILE_BEGIN`(文件开头)、`FS_FILE_CURRENT`(当前文件指针位置)、`FS_FILE_END`(文件末尾)。通过改变文件指针的...
Open fileName For Binary Access Write As #fileHandle Write #fileHandle, httpRequest.ResponseData Close #fileHandle ``` 首先定义文件名和路径,然后打开文件并以二进制格式写入数据。`httpRequest.Response...
例如,我们可以使用`readline()`方法来读取文件中的每一行,但是如果我们想从头开始读取文件,需要使用`seek(0)`方法来将文件句柄移动到文件的开头,例如`fileHandle.seek(0)`。 Python语言提供了多种方式来操作...
ShowMessage(IntToStr(GetFileSize(FileHandle, nil))); FileClose(FileHandle); end; end; ``` 在上面的代码中,我们首先使用 OpenDialog1 获取文件名,然后使用 FileOpen 函数打开文件,并获取文件的句柄。...
`FileHandle.cpp`很可能是实现`ZwQuerySystemInformation`查找文件句柄功能的核心代码,而`stdafx.cpp`包含了预编译头文件,`FileHandle.exe`则是编译后的程序。 `FileHandle.aps`和`.vcproj.YOUR-7F254DD399.user....
Do While WaveHdr.dwFlags And 1 'WHDR_INQUEUE标志表示缓冲区正在播放 DoEvents Loop End If ' 关闭音频设备 waveOutClose hWaveOut End If Close #FileHandle End Sub ``` 在上述代码中,`PlayWAV`...
Dim filehandle As Integer Dim ModelName As String Dim yearweek As String Dim str_vale() As String Dim Vale As String Dim SN_36 As String Dim Model As String Dim Xtop, Ytop, Xstep, Ystep As ...
fileHandle.write('This is a test.\nReally, it is.') fileHandle.close() ``` 如果希望保留原有内容并追加新内容,可以使用 `'a'` 模式: ```python fileHandle = open('test.txt', 'a') fileHandle....
mybytesize = (LOF(filehandle) - Loc(filehandle)) End If ReDim mybyte(0 To mybytesize - 1) Get #filehandle,, mybyte WinsockSend.SendData mybyte ``` 文件数据通常被分割成较小的数据块进行传输,这...