=encoding utf8 =head1 TITLE Synopsis 32: Setting Library - IO =head1 VERSION Created: 19 Feb 2009 Last Modified: 28 Oct 2014 Version: 27 =head1 Overview This synopsis describes in depth the subroutines and methods that were described broadly in S16. Please note that any implementation is free to use multi-method dispatch on both subroutines as well as methods should this increase the performance or maintainability. =head1 Functions =head2 print() X sub print(*@text --> Bool) is export Print the given text on C<$*OUT>. =head2 say() X sub say(*@text --> Bool) is export Print the given text, followed by a new line C<"\n"> on C<$*OUT>. Before printing, call the C<.gist> method on any non-C objects. =head2 note() X sub note(*@text --> Bool) is export Print the given text, followed by a new line C<"\n"> on C<$*ERR>. Before printing, call the C<.gist> method on any non-C objects. =head2 dd() X sub dd(@vars --> Bool) is export Tiny Data Dumper. Takes the C specified and Cs them (on C<$*ERR>) in an easy to read format, along with the C of the variable. So: my $a = 42; dd($a); # notes "$a = 42" =head2 prompt() X sub prompt($msg --> Bool) is export Simple Prompter. Print message on C<$*OUT> and obtains a single line of input from C<$*IN>. =head2 open() X sub open ($name as IO, # mode Bool :$r = True, Bool :$w = False, Bool :$rw = False, Bool :$a = False, # encoding Bool :$bin = False, Str :$enc = "Unicode", # utf-8 unless otherwise # newlines Any :$nl = "EOL", Bool :$chomp = True, --> IO::Handle ) is export A convenience function for opening normal files as text (by default) as specified by its (first) parameter. It returns an instantiated L object. The following named parameters may also be specified: =over 4 =item :r Open file for reading. Default is C. =item :w Open file for writing by creating an empty file with the given name. The original contents of an existing file with that name, will be B. Default is C. =item :rw Open file for reading and writing with the given name. The original contents of an existing file with that name, will be B. Default is C. =item :a Open file for appending, create one if it didn't exist yet. This may or may not inhibit overwriting the original contents when moving the file pointer. Default is C. =item :bin Open file in binary mode (byte mode). A file opened with C<:bin> may still be processed line-by-line, but IO will be in terms of C rather than C types. Default is C, implying text semantics. =item :enc Encoding to use if opened in text mode. Defaults to "Unicode", which implies figuring out which actual UTF is in use, either from a BOM or other heuristics. If heuristics are inconclusive, UTF-8 will be assumed. (No 8-bit encoding will ever be picked implicitly.) =item :nl The marker used to indicate the end of a line of text. Only used in text mode. Defaults to "EOL", which implies accepting any combination of C<"\n">, C<"\r\n"> or C<"\r"> or any other Unicode character that has the C (Separator, Line) property. =item :chomp Whether or not to remove new line characters from text obtained with C<.lines> and C<.get>. Defaults to C. =back =head2 dir() X sub dir($directory as Str = $*CWD, Mu :$test = $*SPEC.curupdir, Bool :$absolute = False, Bool :$Str = False, IO::Path :$CWD = $*CWD, --> List ) is export Returns a lazy list of (relative) paths in the C<$directory> as C objects, by default from the directory pointed to by C<$*CWD>. If dir() fails, it returns an L failure. The following named parameters are optional: =over 4 =item :test Expression against which to smart-match for inclusion in result list. By default excludes C (usually ".") and C (usually "..") only. =item :absolute Boolean indicating to return absolute path names, rather than relative ones. False by default. =item :Str Boolean indicating to return Cings, rather than C objects. False by default. =item :CWD Only important if C<:absolute> is specified with a True value. The directory to pre-pend to the relative file paths. Defaults to C<$*CWD>. =back =head2 slurp() X sub slurp ($what = $*ARGFILES, Bool :$bin = False, Str :$enc = "Unicode", --> Str|Buf ) is export Slurps the contents of the entire file into a C (or C if C<:bin>). Accepts C<:bin> and C<:enc> optional named parameters, with the same meaning as L. The routine will C if the file does not exist, or is a directory. =head2 spurt() X sub spurt ($where, $what, Str :$enc = $*ENC, Bool :append = False, Bool :$createonly = False, --> Bool ) is export Writes the indicated contents (2nd positional parameter) to the location indicated by the first positional parameter (which can either be a string, an C object, or an already opened C object). If a file needs to be opened for writing, it will also be Cd. Returns True on success, or the appropriate C if something went wrong. These named parameters are optional and only have meaning if the first positional parameter was B an C: =over 4 =item :enc The encoding with which the contents will be written. [conjectural] =item :append Boolean indicating whether to append to a (potentially) existing file. If the file did not exist yet, it will be created. Defaults to C. =item :createonly Boolean indicating whether to fail if the file already exists. Defaults to C. =back =head2 mkdir() X sub mkdir($dir as IO, $mode = 0o777 --> Bool) is export Creates the directory as indicated by the positional parameter. Returns C on success or an appropriate C. =head2 rmdir() X sub rmdir($dir as IO --> Bool) is export Removes the (empty) directory as indicated by the positional parameter. Returns C on success or an appropriate C. =head2 chdir() X sub chdir($dir as IO, $CWD = $*CWD, :$test = --> Bool) is export Changes the current working directory to the given directory, for the scope in which C<$*CWD> is active (if no second positional parameter is given) or for the scope of the indicated localized C<$*CWD>. A typical use case: { chdir("foo", my $*CWD); # working directory changed to "foo" } # restored to what it was Returns C if successful, or an appropriate C, e.g if the directory does not exist, or is not a directory, or is not readable. Please note that this directory has B connection with whatever the operating system thinks is the current working directory. The value of C<$*CWD> just will always be prepended to any relative paths in any file operation in Perl 6. Also note that you can use C to set similar dynamic variables, like C<$*TMPDIR> and C<$*HOME> this way: chdir("bar", my $*TMPDIR); # set $*TMPDIR in this scope chdir("bar", my $*HOME); # set $*HOME in this scope =head2 copy() X sub copy ($source as IO, $dest as IO, :$createonly = False, --> Bool ) is export Copies a file, as indicated by the first positional parameter, to the destination specified. If :createonly is set to True, copy fails if a file already exists in the destination. Returns C upon success, or an appropriate C if the operation could not be completed. =head2 rename() X sub rename ($source as IO, $dest as IO, :$createonly = False, --> Bool ) is export Moves a file, as indicated by the first positional parameter, by renaming it to the destination specified. If :createonly is set to True, the rename fails if a file already exists in the destination. Returns C upon success, or an appropriate C if the operation could not be completed. Please use L if a file could not be moved by renaming (usually because the destination is on a different physical storage device). =head2 move() X sub move ($source as IO, $dest as IO, :$createonly = False, --> Bool ) is export Moves a file, as indicated by the first positional parameter, by copying its contents to the destination specified, and then removing the file at the original location. If :createonly is set to True, the move fails if a file already exists in the destination. Returns C upon success, or an appropriate C if the operation could not be completed. Please use L if a file can be moved by renaming (which is usually possible if the destination is on the same different physical storage device). Alternately, the C function is free to try the C first, and if that (silently) fails, do it the hard way. =head2 unlink() X sub unlink(*@files --> @removed) is export Delete all specified ordinary files, links, or symbolic links. Returns the names of the files that were successfully deleted. =head3 chmod() X sub chmod($permission, *@files --> @changed) is export Changes the permissions of a list of files. The first element of the list must be the numerical mode, which should probably be an octal number, and which definitely should I be a string of octal digits: C<0o644> is okay, C<0644> is not. Returns the names of the files that were successfully changed. $count = chmod 0o755, 'foo', 'bar'; chmod 0o755, @executables; $mode = '0644'; chmod $mode, 'foo'; # BAD!!! sets mode to --w----r-T $mode = '0o644'; chmod $mode, 'foo'; # this is better $mode = 0o644 ; chmod $mode, 'foo'; # this is best =head2 link() X sub link($target, $source --> Bool) is export Create a hard link between the target from the given source path. Returns C if successful, or an appropriate C. =head2 symlink() X sub symlink($target, $source --> Bool) is export Create a symbolic link between the target from the given source path. Returns C if successful, or an appropriate C. =head1 IO Types =head2 IO role IO { }; The base role only tags that this is an C object for more generic purposes. It doesn't specify any methods or attributes. =head2 IO::Spec This class is a collection of methods dealing with file specifications (commonly known as file names, though it can include the entire directory path). Most of the methods allow access to lower-level operations on file path strings. These operations are significantly different on some operating systems, so the actual work is being done by subclasses such as C, C and C. The correct C class for your system, is available in the C<$*SPEC> dynamic variable. So typically, you would call methods on that: my $cleanpath = $*SPEC.canonpath("a/.//b/") # gives "a/b" This set of modules was inspired by Perl 5's C. An implementation may choose to inherit from C, or any of its subclasses, if that helps in avoiding code duplication. The C