在 Perl 中,如何将整个文件读入字符串?

编程入门 行业动态 更新时间:2024-10-28 12:17:42
本文介绍了在 Perl 中,如何将整个文件读入字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将 .html 文件作为一个大长字符串打开.这就是我所拥有的:

I'm trying to open an .html file as one big long string. This is what I've got:

open(FILE, 'index.html') or die "Can't read file 'filename' [$!] "; $document = <FILE>; close (FILE); print $document;

导致:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN

但是,我希望结果如下:

However, I want the result to look like:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

这样我可以更轻松地搜索整个文档.

This way I can search the entire document more easily.

推荐答案

添加:

local $/;

在从文件句柄读取之前.请参阅如何我可以一次读取整个文件吗? 或

before reading from the file handle. See How can I read in an entire file all at once?, or

$ perldoc -q "entire file"

参见与文件句柄相关的变量在 perldoc perlvar 和 perldoc -f local.

顺便说一句,如果你能把你的脚本放在服务器上,你就可以拥有你想要的所有模块.请参阅如何保持我的自己的模块/库目录?.

Incidentally, if you can put your script on the server, you can have all the modules you want. See How do I keep my own module/library directory?.

此外,Path::Class::File 允许您slurp 和 spew.

In addition, Path::Class::File allows you to slurp and spew.

Path::Tiny 提供了更方便的方法,例如 slurp、slurp_raw、slurp_utf8 以及他们的 spew 同行.

Path::Tiny gives even more convenience methods such as slurp, slurp_raw, slurp_utf8 as well as their spew counterparts.

更多推荐

在 Perl 中,如何将整个文件读入字符串?

本文发布于:2023-07-16 13:27:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1122190.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何将   文件   Perl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!