Perl 隱式關閉重置 $. 多變的 (Perl implicit close resets the $. variable)


問題描述

Perl 隱式關閉重置 $. 多變的 (Perl implicit close resets the $. variable)

The documentation for Perl's close states that $. isn't reset if you use the implicit close done by open. I was trying to see exactly what this meant, but couldn't get it to happen. Here's my script:

use strict;
use warnings;
use autodie;
my @files = qw(test1.txt test2.txt test3.txt);

#try with implicit close
for my $file (@files){
    open my $fh, '<', $file;
    while(<$fh>){
        chomp;
        print "line $. is $_\n";
    }
    #implicit close here
}

And here are the contents of all three of the files that are read in:

line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

Since I don't explicitly call close, the implicit close should be used (I think) and $. shouldn't be reset. However, when I run the script, I get this output, showing that $. is reset:

line 1 is line 1
line 2 is line 2
line 3 is line 3
line 4 is line 4
line 5 is line 5
line 6 is line 6
line 7 is line 7
line 8 is line 8
line 9 is line 9
line 10 is line 10
line 1 is line 1
line 2 is line 2
line 3 is line 3
line 4 is line 4
line 5 is line 5
line 6 is line 6
line 7 is line 7
line 8 is line 8
line 9 is line 9
line 10 is line 10
line 1 is line 1
line 2 is line 2
line 3 is line 3
line 4 is line 4
line 5 is line 5
line 6 is line 6
line 7 is line 7
line 8 is line 8
line 9 is line 9
line 10 is line 10

It sure looks like it's being reset to me. Is my understanding of the documentation wrong? Can someone show me under what circumstance the implicit close will not reset $.?

By the way, I'm using Strawberry 5.16.1.

‑‑‑‑‑

參考解法

方法 1:

$. is not actually a global variable, it is an attribute of the most recently read filehandle.  And you are using a new filehandle in each iteration through the loop.

Modifying your code like so "fixes" it:

my $fh;
for my $file (@files){
    open $fh, '<', $file;

(by Nate Glennysth)

參考文件

  1. Perl implicit close resets the $. variable (CC BY‑SA 3.0/4.0)

#perl #file-handling #special-variables






相關問題

保持特定位數的簡單 Perl 數學 (simple Perl math while keeping a specific number of digits)

如何在 Windows 批處理腳本或 Perl 中將文件移動到回收站? (How can I move files to the Recycle Bin in a Windows batch script or Perl?)

從子程序返回數組 (Return array from subroutine)

我可以以與操作系統無關的方式限制 Perl 進程使用的內存嗎? (Can I Iimit the memory used by a Perl process in an OS-agnostic way?)

$# 在 perl 中接受什麼作為輸入? (what does $# accept as input in perl?)

Perl Text::CSV_XS 從字符串中讀取 (Perl Text::CSV_XS read from string)

使用 excel 2010 更新批處理文件中的變量 (use excel 2010 to update variables in batch file)

在 perl 中為哈希添加值 (Adding value to an hash in perl)

為什麼 perl 會忽略我的正則表達式中的多餘字符? (Why does perl ignore extra characters in my regex?)

boost::regex - \bb? (boost::regex - \bb?)

如果小於 X 天,如何從磁盤讀取文件,如果舊,則重新獲取 html 文件 (How to read a file from the disk if less than X days old, if older, refetch the html file)

使用 Devel-Cover 獲取覆蓋率報告 (Using Devel-Cover to get coverage reports)







留言討論