Skip to content
Nathan Waddell edited this page Jun 28, 2017 · 4 revisions

First, some safe examples:

use IO::All;

# Some of the many ways to read a whole file into a scalar
$contents = io->file('file.txt')->slurp;    # Read an entire file
@files    = io->dir('lib')->all;            # Get a list of files
$tail     = io->pipe('-| tail app.log');    # Open a pipe to a command
$line     = $tail->getline;                 # Read from the pipe

That said, there are a lot more things that are very convenient and will help you write code very quickly, though they should be used judiciously:

use IO::All;                                # Let the madness begin...
 
# Some of the many ways to read a whole file into a scalar
io('file.txt') > $contents;                 # Overloaded "arrow"
$contents < io 'file.txt';                  # Flipped but same operation
$io = io 'file.txt';                        # Create a new IO::All object
$contents = $$io;                           # Overloaded scalar dereference
$contents = $io->all;                       # A method to read everything
$contents = $io->slurp;                     # Another method for that
$contents = join '', $io->getlines;         # Join the separate lines
$contents = join '', map "$_\n", @$io;      # Same. Overloaded array deref
$io->tie;                                   # Tie the object as a handle
$contents = join '', <$io>;                 # And use it in builtins
# and the list goes on ...
 
# Other file operations:
@lines = io('file.txt')->slurp;             # List context slurp
$content > io('file.txt');                  # Print to a file
io('file.txt')->print($content, $more);     # (ditto)
$content >> io('file.txt');                 # Append to a file
io('file.txt')->append($content);           # (ditto)
$content << $io;                            # Append to a string
io('copy.txt') < io('file.txt');            $ Copy a file
io('file.txt') > io('copy.txt');            # Invokes File::Copy
io('more.txt') >> io('all.txt');            # Add on to a file
io('dir/') < io('file.txt');                $ Copy a file to a directory
io('file.txt') > io('dir/');                # Invokes File::Copy
io('more.txt') >> io('dir/');               # Add on to a file in the dir
 
# UTF-8 Support
$contents = io('file.txt')->utf8->all;      # Turn on utf8
use IO::All -utf8;                          # Turn on utf8 for all io
$contents = io('file.txt')->all;            #   by default in this package.
 
# General Encoding Support
$contents = io('file.txt')->encoding('big5')->all;
use IO::All -encoding => 'big5';            # Turn on big5 for all io
$contents = io('file.txt')->all;            #   by default in this package.
 
# Print the path name of a file:
print $io->name;                            # The direct method
print "$io";                                # Object stringifies to name
print $io;                                  # Quotes not needed here
print $io->filename;                        # The file portion only
$io->os('win32');                           # change the object to be a
                                            # win32 path
print $io->ext;                             # The file extension only
print $io->mimetype;                        # The mimetype, requires a
                                        #  working File::MimeType
 
 
# Read all the files/directories in a directory:
$io = io('my/directory/');                  # Create new directory object
@contents = $io->all;                       # Get all contents of dir
@contents = @$io;                           # Directory as an array
@contents = values %$io;                    # Directory as a hash
push @contents, $subdir                     # One at a time
  while $subdir = $io->next;
 
# Print the name and file type for all the contents above:
print "$_ is a " . $_->type . "\n"          # Each element of @contents
  for @contents;                            # is an IO::All object!!
 
# Print first line of each file:
print $_->getline                           # getline gets one line
  for io('dir')->all_files;                 # Files only
 
# Print names of all files/dirs three directories deep:
print "$_\n" for $io->all(3);               # Pass in the depth. Default=1
 
# Print names of all files/dirs recursively:
print "$_\n" for $io->all(0);               # Zero means all the way down
print "$_\n" for $io->All;                  # Capitalized shortcut
print "$_\n" for $io->deep->all;            # Another way

# There are some special file names:
print io('-');                              # Print STDIN to STDOUT
io('-') > io('-');                          # Do it again
io('-') < io('-');                          # Same. Context sensitive.
"Bad puppy" > io('=');                      # Message to STDERR
$string_file = io('$');                     # Create string based filehandle
$temp_file = io('?');                       # Create a temporary file
     
# Socket operations:
$server = io('localhost:5555')->fork;       # Create a daemon socket
$connection = $server->accept;              # Get a connection socket
$input < $connection;                       # Get some data from it
"Thank you!" > $connection;                 # Thank the caller
$connection->close;                         # Hang up
io(':6666')->accept->slurp > io->devnull;   # Take a complaint and file it
     
# DBM database operations:
$dbm = io 'my/database';                    # Create a database object
print $dbm->{grocery_list};                 # Hash context makes it a DBM
$dbm->{todo} = $new_list;                   # Write to database
$dbm->dbm('GDBM_file');                     # Demand specific DBM
io('mydb')->mldbm->{env} = \%ENV;           # MLDBM support
     
# Tie::File support:
$io = io 'file.txt';
$io->[42] = 'Line Forty Three';             # Change a line
print $io->[@$io / 2];                      # Print middle line
@$io = reverse @$io;                        # Reverse lines in a file
 
# Stat functions:
printf "%s %s %s\n",                        # Print name, uid and size of
  $_->name, $_->uid, $_->size               # contents of current directory
    for io('.')->all;
print "$_\n" for sort                       # Use mtime method to sort all
  {$b->mtime <=> $a->mtime}                 # files under current directory
    io('.')->All_Files;                     # by recent modification time.
 
# File::Spec support:
$contents < io->catfile(qw(dir file.txt));  # Portable IO operation
 
# Miscellaneous:
@lines = io('file.txt')->chomp->slurp;      # Chomp as you slurp
@chunks =
  io('file.txt')->separator('xxx')->slurp;  # Use alternnate record sep
$binary = io('file.bin')->binary->all;      # Read a binary file
io('a-symlink')->readlink->slurp;           # Readlink returns an object
print io('foo')->absolute->pathname;        # Print absolute path of foo
 
# IO::All External Plugin Methods
io("myfile") > io->("ftp://store.org");     # Upload a file using ftp
$html < io->http("www.google.com");         # Grab a web page
io('mailto:[email protected]')->print($spam); # Email a "friend"
 
# This is just the beginning, read on...`
Clone this wiki locally