Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added a new article for the 3rd
- Loading branch information
Tadeusz Sośnierz
committed
Dec 2, 2010
1 parent
9f6b534
commit 6da4b5a
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| =head1 File operations | ||
|
|
||
| =head2 Directories | ||
|
|
||
| Instead of C<opendir> and friends, in Perl 6 there is a single C<dir> | ||
| subroutine, returning a list of the files in a specified directory, | ||
| defaulting to the current dirrectory. A piece of code speaks a thousand | ||
| words: | ||
|
|
||
| # in the Rakudo source directory | ||
| > dir | ||
| build parrot_install Makefile VERSION parrot docs Configure.pl README dynext t src tools CREDITS LICENSE Test.pm | ||
| > dir 't' | ||
| 00-parrot 02-embed spec harness 01-sanity pmc spectest.data | ||
|
|
||
| C<dir> has also an optional named parameter C<test>, used to grep the | ||
| results | ||
|
|
||
| > dir 'src/core', test => any(/^C/, /^P/) | ||
| Parcel.pm Cool.pm Parameter.pm Code.pm Complex.pm CallFrame.pm Positional.pm Capture.pm Pair.pm Cool-num.pm Callable.pm Cool-str.pm | ||
|
|
||
| Directories are created with C<mkdir>, as in C<mkdir('foo')> | ||
|
|
||
| =head2 Files | ||
|
|
||
| The easiest way to read a file in Perl 6 is using C<slurp>. C<slurp> returns the contents of a file, as a String, | ||
|
|
||
| > slurp 'VERSION' | ||
| 2010.11 | ||
|
|
||
| The good, old way of using filehandles is of course still available | ||
|
|
||
| > my $fh = open 'CREDITS' | ||
| IO()<0x1105a068> | ||
| > $fh.getc # reads a single character | ||
| = | ||
| > $fh.get # reads a single line | ||
| pod | ||
| > $fh.close; $fh = open 'new', :w # open for writing | ||
| IO()<0x10f3e704> | ||
| > $fh.print('foo') | ||
| Bool::True | ||
| > $fh.say('bar') | ||
| Bool::True | ||
| > $fh.close; say slurp('new') | ||
| foobar | ||
|
|
||
| =head2 File tests | ||
|
|
||
| Testing the existence and types of files is done with smartmatching (~~). | ||
| Again, the code: | ||
|
|
||
| > 'LICENSE'.IO ~~ :e # does the file exist? | ||
| Bool::True | ||
| > 'LICENSE'.IO ~~ :d # is it a directory? | ||
| Bool::False | ||
| > 'LICENSE'.IO ~~ :f # a file then? | ||
| Bool::True | ||
|
|
||
| Easy peasy. | ||
|
|
||
| =head2 File::Find | ||
|
|
||
| When the standard features are not enough, modules come in handy. | ||
| C<File::Find> (available in the C<File::Tools> package) traverses the | ||
| directory tree looking for the files you need, and generates a lazy | ||
| lists of the found ones. File::Find comes shipped with Rakudo Star, | ||
| and can be easily installed with C<neutro> if you have just a | ||
| bare Rakudo. | ||
|
|
||
| Example usage? Sure. C<<find(:dir<t/dir1>, :type<file>, :name(/foo/))>> | ||
| will generate a lazy list of files (and files only) in a directory named | ||
| t/dir1 and with a name matching the regex C</foo/>. Notice how the | ||
| elements of a list are not just plain strings: they're objects which | ||
| strinigify to the full path, but also provide accessors for the directory | ||
| they're in (C<dir>) and the filename itself (C<name>). For more info | ||
| please refer to the C<documentation>. | ||
|
|
||
| =head2 Useful idioms | ||
|
|
||
| =over 4 | ||
|
|
||
| =item Creating a new file | ||
|
|
||
| open('new', :w).close | ||
|
|
||
| =item "Anonymous" filehandle | ||
|
|
||
| given open('foo', :w) { | ||
| .say('Hello, world!'); | ||
| .close | ||
| } | ||
|
|
||
| =back |