Moose 基础知识:打开文件并解析
我是 Moose
和 OOP 的新手,希望获得一些指导来解决一个非常棘手的问题使用 Moose
的基本文件处理和解析要求。我熟悉 Perl,并且想开始使用 OOP。
本质上,我想做的就是打开一个文本文件,解析它,然后打印到 stdout
。
例如,使用标准 Perl
open (FILE , input.txt);
while (FILE)
{
if (/(\S+)\s+(\d+)/)
{
print "$1-$2";
}
}
,其中 input.txt 是
ABC 20
DEF 10
GHI 50
I am new to Moose
and OOP and would like some guidance on solving a very basic file handling and parsing requirement using Moose
. I am familiar with Perl, and would like to start using OOP.
Essentially, all that I want to do is open a text file, parse it, and print to stdout
.
For instance using standard Perl
open (FILE , input.txt);
while (FILE)
{
if (/(\S+)\s+(\d+)/)
{
print "$1-$2";
}
}
where input.txt is
ABC 20
DEF 10
GHI 50
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
打开文件与 Moose 没有任何关系。但是,如果您正在寻找现有的接口来处理文件,您应该看看 Path::Class::File,这是一个包含文件名的对象,并为您提供许多处理它所代表的文件的方法。在属性中使用此类作为 Moose 类型约束是很常见的:
您还可以修改 process_file 方法,以便它采用从文件接收一行作为参数的 coderef 来处理以更加模块化的方式文件内容。当然,这完全取决于您需要程序做什么。
或者,如果您喜欢 MooseX::Types,您可以这样做:
这会让您通过属性的文件名,它将在内部自动膨胀为
Path::Class::File
对象。Opening files doesn't really relate to Moose in any way. However, if you are looking for existing interfaces to deal with files, you should take a look at Path::Class::File, which is an object that will contain a filename and provide you many methods for dealing with the file it represents. It is quite common to use this class as a Moose type constraint in an attribute:
You could also modify the
process_file
method so it takes a coderef which receives one line from the file as an argument, to process the file contents in a more modular way. It all depends on what you need your program to do, of course.Alternatively, if you like MooseX::Types, you can do:
This will let you pass a filename to the attribute and it will automatically inflate into a
Path::Class::File
object internally.您可能想尝试模拟 Moose::Cookbook 中的示例。
老实说,您自己的示例与 OOP 并不真正相关。
如果您的意思是使用 OOP 版本的 IO,您可以轻松地做到这一点(使用 IO::Handle)模块,但该模块不是基于 Moose 的。
如果您的意思是要将上面的文件代码包装到基于 Moose 的模块中,您当然可以,但您需要澄清您想要的(独立于 Moose 的)OOP 设计。例如,您寻求的实例变量是什么?方法?
You might want to try emulating examples in Moose::Cookbook.
To be honest, your own example is not really OOP related.
If you mean using OOP version of IO, you can easily do that (use
IO::Handle
) module, but that module is not Moose based.If you mean you want to wrap the file code above into a Moose-based module, you certainly can but you need to clarify the (Moose-independent) OOP design you want. E.g. what are the instance variables you seek? methods?
根据 DVK 的回答下的评论,您可能会要求这样的东西?
Based on comment under DVK's answer, you might be asking for something like this?
这是一个非常简单的 Perl 程序,使用
Moose
只会使它变得复杂。在开始编写面向对象的 Perl 之前
使用严格
和使用警告
open
形式,并且始终检查每个open
调用是否成功This is a very simple Perl program and using
Moose
would only complicate it.Before you progress to writing object-oriented Perl
use strict
anduse warnings
at the start of your programopen
and always check the success of everyopen
call