为什么我的文件内容/用户输入不匹配?(缺少 chomp 规范) [英] Why does my file content/user input not match? (missing chomp canonical)

查看:24
本文介绍了为什么我的文件内容/用户输入不匹配?(缺少 chomp 规范)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取文件/STDIN 并寻找特定值:

I'm reading from a file/STDIN and looking for a specific value:

use strict;
use warnings;

my $find = 'def';    
while (<DATA>) {
    if ($_ eq $find) {
        print "Found: $_
"; # Never reached!
    }
}

__DATA__
abc
def
xyz

为什么条件永远不匹配?

Why is the condition never matched?

推荐答案

数据::Dumper 可用于更仔细地检查变量:

Data::Dumper can be used to more closely examine the variables:

use Data::Dumper;
local $Data::Dumper::Useqq = 1;

print Dumper $_, $find;

输出,例如

$VAR1 = "def
";
$VAR2 = "def";

您必须删除 读入$_ 字符.最简单的方法是 chomp 函数>

You have to remove the character that <DATA> reads into $_. The simplest way to do it is the chomp function

use strict;
use warnings;

my $find = 'def';    
while (<DATA>) {
    chomp;
    if ($_ eq $find) {
        print "Found: $_
"; # Never reached!
    }
}

__DATA__
abc
def
xyz

这篇关于为什么我的文件内容/用户输入不匹配?(缺少 chomp 规范)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆