/File-Slurp-Affix

File::Slurp::Affix opens a file and creates an array with an optional prefix, suffix, or both with an optional separator in your preferred encoding.

Primary LanguagePerlArtistic License 2.0Artistic-2.0

File::Slurp::Affix

File::Slurp::Affix opens a file and creates an array with an optional prefix, suffix, or both with an optional separator in your preferred encoding.

Version

This document describes File::Slurp::Affix version 0.01_01.

Synopsis

use File::Slurp::Affix qw(slurp_affix);

my @plain_array      = slurp_affix($file);

my @prefix_array     = slurp_affix($file, { 'prefix' => 'foo' });

my @suffix_array     = slurp_affix($file, { 'suffix' => 'bar' });

my @both_array       = slurp_affix($file, { 'prefix' => 'foo', 'suffix' => 'bar' });

my @separator_array  = slurp_affix($file, { 'prefix' => 'foo', 'suffix' => 'bar', 'separator' => ' ' });

my @empty_line       = slurp_affix($file, { 'empty' => 'fill' });

my @encoded_array    = slurp_affix($file, { 'encoding' => 'ascii' });

Description

slurp_affix can be exported and returns a list of values. These values can be modified if the optional parameters prefix, suffix, or both are used. There is the additional option to choose your encoding, the default is utf-8.

If the open fails, slurp_affix will die.

my @fancy_array = slurp_affix(
  $file,
  {
    'prefix' => $prefix_string,
    'suffix' => $suffix_string,
    'separator' => $separator_string,
    'empty'  => $empty_option,
    'encoding' => $encoding_option
  }
);

The file is also closed by slurp_affix.

File::Slurp::Affix requires Perl version 5.6 or better.

Parameters

slurp_affix has two parameters.

Note: all sample returned arrays are the results from Data::Dump.

Sample file contents.

red
orange
yellow
spring
green
teal
cyan
azure
blue
violet
magenta
pink
white
black
gray

file

my @plain_array = slurp_affix($file);

The first parameter is the file to be opened. If this is the only parameter specified, the file will be opened, encoded to utf-8, and returned as a list.

Options

The second parameter are the options: prefix, suffix, separator, encoding, and empty.

prefix
my @prefix_array = slurp_affix($file, { 'prefix' => 'solid' });

The prefix option is the string you want prepended to each item in the list. Using the example, all items on the list will be returned with foo prepended to them.

(
  "solidred",
  "solidorange",
  "solidyellow",
  "solidspring",
  "solidgreen",
  "solidteal",
  "solidcyan",
  "solidazure",
  "solidblue",
  "solidviolet",
  "solidmagenta",
  "solidpink",
  "solidwhite",
  "solidblack",
  "solidgray",
)
suffix
my @suffix_array = slurp_affix($file, { 'suffix' => 'bead; });

The suffix option is the string you want to appear appended to each item in the list. Using the example, all items on the list will be returned with bar appended to them.

(
  "redbead",
  "orangebead",
  "yellowbead",
  "springbead",
  "greenbead",
  "tealbead",
  "cyanbead",
  "azurebead",
  "bluebead",
  "violetbead",
  "magentabead",
  "pinkbead",
  "whitebead",
  "blackbead",
  "graybead",
)
prefix and suffix
my @both_array = slurp_affix($file, { 'prefix' => 'solid', 'suffix' => 'bead' });

Using both the prefix and suffix options together will prepend and append the associated strings to the items in the list.

(
  "solidredbead",
  "solidorangebead",
  "solidyellowbead",
  "solidspringbead",
  "solidgreenbead",
  "solidtealbead",
  "solidcyanbead",
  "solidazurebead",
  "solidbluebead",
  "solidvioletbead",
  "solidmagentabead",
  "solidpinkbead",
  "solidwhitebead",
  "solidblackbead",
  "solidgraybead",
)
separator
my @separator_array = slurp_affix($file, { 'prefix' => 'solid', 'suffix' => 'bead', 'separator' => ' ' });

The separator option will add a string between the prefix, the line from the file, and the suffix. In this case, a single space.

(
  "solid red bead",
  "solid orange bead",
  "solid yellow bead",
  "solid spring bead",
  "solid green bead",
  "solid teal bead",
  "solid cyan bead",
  "solid azure bead",
  "solid blue bead",
  "solid violet bead",
  "solid magenta bead",
  "solid pink bead",
  "solid white bead",
  "solid black bead",
  "solid gray bead",
)
empty
my @empty_line = slurp_affix($file, { 'empty' => 'fill' });

The empty option has three possible values for what to do with empty lines in the file: fill, blank, or undefined. If empty is not used or is any value than the three listed, the empty line will be ignored.

Sample file contents with an empty line.

red
orange
yellow
spring
green
teal
cyan
azure

blue
violet
magenta
pink
white
black
gray
  • fill will prefix and suffix the value as it does with all other lines.

    my @empty_line = slurp_affix($file, { 'prefix' => 'solid', 'empty' => 'fill' });

    The array returned will be:

    (
      "solidred",
      "solidorange",
      "solidyellow",
      "solidspring",
      "solidgreen",
      "solidteal",
      "solidcyan",
      "solidazure",
      "solid",
      "solidblue",
      "solidviolet",
      "solidmagenta",
      "solidpink",
      "solidwhite",
      "solidblack",
      "solidgray",
    )
  • blank will return a zero length but defined value.

    my @empty_line = slurp_affix($file, { 'prefix' => 'solid', 'empty' => 'blank' });

    The array returned will be:

    (
      "solidred",
      "solidorange",
      "solidyellow",
      "solidspring",
      "solidgreen",
      "solidteal",
      "solidcyan",
      "solidazure",
      "",
      "solidblue",
      "solidviolet",
      "solidmagenta",
      "solidpink",
      "solidwhite",
      "solidblack",
      "solidgray",
    )
  • undefined will return an undefined value.

    my @empty_line = slurp_affix($file, { 'prefix' => 'solid', 'empty' => 'undefined' });

    The array returned will be:

    (
      "solidred",
      "solidorange",
      "solidyellow",
      "solidspring",
      "solidgreen",
      "solidteal",
      "solidcyan",
      "solidazure",
      undef,
      "solidblue",
      "solidviolet",
      "solidmagenta",
      "solidpink",
      "solidwhite",
      "solidblack",
      "solidgray",
    )
encoding
my @encoded_array = slurp_affix($file, { 'encoding' => 'ascii' });

The encoding option is the encoding you want to use to open the file. The above file will be opened ascii encoded.

Dependency

File::Slurp::Affix depends on Exporter.

Installation

To install this module use your preferred CPAN installer or run the following commands:

perl Makefile.PL make make test make install

Support

You can find documentation for this module with the perldoc command.

perldoc File::Slurp::Affix

You can also look for information at:

Author

Lady Aleena

License and copyright

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

Copyright © 2020, Lady Aleena (aleena@cpan.org). All rights reserved.