Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <a href="http://search.cpan.org/perldoc?Path%3a%3aClass%3a%3aFile" rel="nofollow">Path::Class::File</a>, 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:</p> <pre><code>package MyApp::Foo; use Moose; has filename =&gt; ( is =&gt; 'ro', isa =&gt; 'Path::Class::File', ); sub process_file { my $this = shift; if (-e $this-&gt;filename) { my $fh = $this-&gt;filename-&gt;openr; while (my $line = &lt;$fh&gt;) { # process file, line by line... } } } package main; my $obj = MyApp::Foo-&gt;new(filename =&gt; '/home/me/foo.txt'); $obj-&gt;process_file; </code></pre> <p>You could also modify the <code>process_file</code> 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.</p> <p>Alternatively, if you like <a href="http://metacpan.org/module/MooseX%3a%3aTypes" rel="nofollow">MooseX::Types</a>, you can do:</p> <pre><code>use MooseX::Types::Path::Class qw(Dir File); has file =&gt; ( ..., isa =&gt; File, coerce =&gt; 1, ... ); </code></pre> <p>This will let you pass a filename to the attribute and it will automatically inflate into a <code>Path::Class::File</code> object internally.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload