=head1 NAME

Data::Hub::Util - 

=head1 SYNOPSIS



=head1 EXPORTS

Nothing exported by default

=over

=item Exported upon request

RE_ABSTRACT_KEY, RE_ABSTRACT_ADDR, $VALUE_ALL, $VALUE_ALL_REC, $PATTERN_QUERY, $PATTERN_QUERY_SUBKEY, $PATTERN_QUERY_VALUE, $PATTERN_QUERY_RANGE, $PATTERN_QUERY_KEY, %TYPEOF_ALIASES, FS, is_abstract_key, is_abstract_addr, curry, typeof, addr_shift, addr_pop, addr_normalize, addr_parent, addr_name, addr_basename, addr_split, addr_join, addr_ext, addr_base, path_shift, path_pop, path_push, path_normalize, path_parent, path_name, path_split, path_join, path_ext, path_is_absolute, path_basename, fs_handle, file_read, file_read_binary, file_read_crown, file_create, file_write, file_write_binary, file_copy, file_move, file_remove, dir_read, dir_create, dir_copy, dir_copy_contents, dir_is_system, dir_move, dir_remove, dir_remove_contents


=item :all

RE_ABSTRACT_KEY, RE_ABSTRACT_ADDR, $VALUE_ALL, $VALUE_ALL_REC, $PATTERN_QUERY, $PATTERN_QUERY_SUBKEY, $PATTERN_QUERY_VALUE, $PATTERN_QUERY_RANGE, $PATTERN_QUERY_KEY, %TYPEOF_ALIASES, FS, is_abstract_key, is_abstract_addr, curry, typeof, addr_shift, addr_pop, addr_normalize, addr_parent, addr_name, addr_basename, addr_split, addr_join, addr_ext, addr_base, path_shift, path_pop, path_push, path_normalize, path_parent, path_name, path_split, path_join, path_ext, path_is_absolute, path_basename, fs_handle, file_read, file_read_binary, file_read_crown, file_create, file_write, file_write_binary, file_copy, file_move, file_remove, dir_read, dir_create, dir_copy, dir_copy_contents, dir_is_system, dir_move, dir_remove, dir_remove_contents


=back

=head1 DEPENDENCIES

This module requires these other modules and libraries:

    Error::Simple
    File::Copy
    IO::File
    Parse::StringTokenizer
    Error::Programatic
    Perl::Module
    Fcntl
    Exporter
    Encode
    Data::Hub::Address
    IO::Dir

=head1 DESCRIPTION



=head1 EXAMPLES

This example will B<not> abort:

  # Load this module
  use Data::Hub::Util qw(:all);




=head1 PUBLIC INTERFACE

=head2 is_abstract_key

=over

=item Is the key abstract (produces a subset)




TODO The regex RE_ABSTRACT_KEY uses an error-prone search for | and * (these
are allowed in hashfile key names.



=back


=head2 is_abstract_addr

=over

=item Does the address contain an abstract key






=back


=head2 curry

=over

=item Add the courier magic to the provided reference





This example:


  curry({a=>{b=>'c'}})->get('/a/b');



I<will return>:

  c



=back


=head2 typeof

=over

=item Return the logical type for the given file-system or data structure





This example:


  typeof('foo.bar', {});



I<will return>:

  data-hash


This example:


  typeof('/with/path/foo.bar', []);



I<will return>:

  data-array


This example:


  typeof('foo.bar', 'baz');



I<will return>:

  data-scalar-bar



=back


=head2 addr_shift

=over

=item Return the next token and trim the address





This example:


  my $addr = '/one/two/{/i/am/three}/four';
  join(':', addr_shift($addr), addr_shift($addr), addr_shift($addr),
  addr_shift($addr)); 



I<will return>:

  one:two:{/i/am/three}:four



=back


=head2 addr_pop

=over

=item Return the last token and trim the address





This example:


  my $addr = '/one/two';
  my $last = addr_pop($addr);
  "$last,$addr"



I<will return>:

  two,/one



=back


=head2 addr_normalize

=over

=item Normalize form of the given address 



See also L</_normalize>


This example:


  addr_normalize('/');



I<will return>:

  /


This example:


  addr_normalize('/a/');



I<will return>:

  /a


This example:


  addr_normalize('/a/b/..');



I<will return>:

  /a


This example:


  addr_normalize('/a/../b');



I<will return>:

  /b


This example:


  addr_normalize('/a/../../b');



I<will return>:

  /b


This example:


  addr_normalize('/a/.../b');



I<will return>:

  /a


This example:


  addr_normalize('/a/.../');



I<will return>:

  /a


This example:


  addr_normalize('/a/...');



I<will return>:

  /a


This example:


  addr_normalize('/a/.../..');



I<will return>:

  /a


This example:


  addr_normalize('/a/../...');



I<will return>:

  /



=back


=head2 addr_parent

=over

=item Return the parent of the given address





This example will B<not> return a defined value:


  addr_parent();



This example will B<not> return a defined value:


  addr_parent('');



This example:


  addr_parent('/');



I<will return>:

  /


This example:


  addr_parent('/a');



I<will return>:

  /


This example:


  addr_parent('/a/b/..');



I<will return>:

  /


This example:


  addr_parent('a/b/');



I<will return>:

  a


This example:


  addr_parent('a/b.c');



I<will return>:

  a



=back


=head2 addr_name

=over

=item Return the name part of an address





This example will B<not> return a defined value:


  addr_name('');



This example:


  addr_name('/');



I<will return>:

  


This example:


  addr_name('/a');



I<will return>:

  a


This example:


  addr_name('/a/b/..');



I<will return>:

  a


This example:


  addr_name('a/b/');



I<will return>:

  b


This example:


  addr_name('a/b.c');



I<will return>:

  b.c



=back


=head2 addr_split

=over

=item Return each part of an address





This example:


  scalar(addr_split(''));



I<will return>:

  0


This example:


  join ' ', addr_split('/');



I<will return>:

  


This example:


  join ' ', addr_split('/a');



I<will return>:

  a


This example:


  join ' ', addr_split('/a/b/..');



I<will return>:

  a


This example:


  join ' ', addr_split('a/b/');



I<will return>:

  a b


This example:


  join ' ', addr_split('a/b.c');



I<will return>:

  a b.c



=back


=head2 addr_join

=over

=item Normalize form of the given address 



See also L</_normalize>


This example:


  addr_join('/');



I<will return>:

  /


This example:


  addr_join('/a', '/');



I<will return>:

  /a


This example:


  addr_join('/a/', 'b/..');



I<will return>:

  /a


This example:


  addr_join('/a/..', '/b');



I<will return>:

  /b


This example:


  addr_join('/a/../', '../b');



I<will return>:

  /b


This example:


  addr_join('/a/.../', 'b');



I<will return>:

  /a


This example:


  addr_join('/a/', '.../');



I<will return>:

  /a


This example:


  addr_join('/a/', '...');



I<will return>:

  /a


This example:


  addr_join('/a/', 'b/');



I<will return>:

  /a/b


This example:


  addr_join('/a/', 'b');



I<will return>:

  /a/b


This example:


  addr_join('/a', '/b');



I<will return>:

  /a/b


This example:


  addr_join('/a', 'b');



I<will return>:

  /a/b


This example:


  addr_join('/a', 'b', 'c');



I<will return>:

  /a/b/c



=back


=head2 addr_ext

=over

=item Return the extension part of the given address





This example will B<not> return a defined value:


  addr_ext(undef);



This example:


  addr_ext('foo.txt');



I<will return>:

  txt


This example:


  addr_ext('/foo.txt');



I<will return>:

  txt


This example:


  addr_ext('./foo.txt');



I<will return>:

  txt


This example:


  addr_ext('../foo.txt');



I<will return>:

  txt


This example:


  addr_ext('/foo/foo.txt');



I<will return>:

  txt


This example:


  addr_ext('/foo.bar.txt');



I<will return>:

  txt


This example:


  addr_ext('/foo/bar.');



I<will return>:

  


This example will B<not> return a defined value:


  addr_ext('/foo/bar');



This example will B<not> return a defined value:


  addr_ext('.metadata');



This example:


  addr_ext('.metadata.bak');



I<will return>:

  bak



=back


=head2 addr_basename

=over

=item Return the name at the given address without its extension





This example will B<not> return a defined value:


  addr_basename(undef);



This example will B<not> return a defined value:


  addr_basename('/');



This example:


  addr_basename('foo.txt');



I<will return>:

  foo


This example:


  addr_basename('foo.txt/');



I<will return>:

  foo


This example:


  addr_basename('/foo.txt');



I<will return>:

  foo


This example:


  addr_basename('./foo.txt');



I<will return>:

  foo


This example:


  addr_basename('../foo.txt');



I<will return>:

  foo


This example:


  addr_basename('/foo/foo.txt');



I<will return>:

  foo


This example:


  addr_basename('/foo.bar.txt');



I<will return>:

  foo.bar


This example:


  addr_basename('/foo/bar.');



I<will return>:

  bar


This example:


  addr_basename('/foo/bar');



I<will return>:

  bar


This example will B<not> return a defined value:


  addr_basename('.metadata');



This example:


  addr_basename('.metadata.bak');



I<will return>:

  .metadata



=back


=head2 addr_base

=over

=item Return the base (known part) of the given address





This example:


  addr_base('/a/b/{d}');



I<will return>:

  /a/b


This example:


  addr_base('/a/b/d');



I<will return>:

  /a/b/d



=back


=head2 path_shift

=over

=item Return the next token and trim the path





This example:


  my $path = '/one/two/../four';
  join(':', path_shift($path), path_shift($path), path_shift($path),
  path_shift($path)); 



I<will return>:

  one:two:..:four



=back


=head2 path_pop

=over

=item Return the last token and trim the path





This example:


  my $path = '/one/two';
  my $last = path_pop($path);
  "$last,$path"



I<will return>:

  two,/one



=back


=head2 path_push

=over

=item Push a token on to the path





This example:


  my $path = '/one/two';
  my $count = path_push($path, 'three');
  "$path"



I<will return>:

  /one/two/three



=back


=head2 path_split

=over

=item Return each part of a path





This example:


  path_split('');



I<will return>:

  0


This example:


  join ' ', path_split('/');



I<will return>:

  


This example:


  join ' ', path_split('/a');



I<will return>:

  a


This example:


  join ' ', path_split('/a/b/..');



I<will return>:

  a


This example:


  join ' ', path_split('a/b/');



I<will return>:

  a b


This example:


  join ' ', path_split('a/b.c');



I<will return>:

  a b.c



=back


=head2 path_basename

=over

=item Return the name part of the given path without its extension





This example will B<not> return a defined value:


  path_basename(undef);



This example:


  path_basename('foo.txt');



I<will return>:

  foo


This example:


  path_basename('/foo.txt');



I<will return>:

  foo


This example:


  path_basename('./foo.txt');



I<will return>:

  foo


This example:


  path_basename('../foo.txt');



I<will return>:

  foo


This example:


  path_basename('/foo/foo.txt');



I<will return>:

  foo


This example:


  path_basename('/foo.bar.txt');



I<will return>:

  foo.bar


This example:


  path_basename('/foo/bar.');



I<will return>:

  bar


This example:


  path_basename('/foo/bar');



I<will return>:

  bar


This example:


  path_basename('.bashrc');



I<will return>:

  .bashrc


This example:


  path_basename('.bashrc.tmp');



I<will return>:

  .bashrc


This example:


  path_basename('/no-dots');



I<will return>:

  no-dots



=back


=head2 path_ext

=over

=item Return the extension part of the given path





This example will B<not> return a defined value:


  path_ext(undef);



This example:


  path_ext('foo.txt');



I<will return>:

  txt


This example:


  path_ext('/foo.txt');



I<will return>:

  txt


This example:


  path_ext('./foo.txt');



I<will return>:

  txt


This example:


  path_ext('../foo.txt');



I<will return>:

  txt


This example:


  path_ext('/foo/foo.txt');



I<will return>:

  txt


This example:


  path_ext('/foo.bar.txt');



I<will return>:

  txt


This example:


  path_ext('/foo/bar.');



I<will return>:

  


This example will B<not> return a defined value:


  path_ext('/foo/bar');



This example will B<not> return a defined value:


  path_ext('.bashrc');



This example:


  path_ext('.bashrc.tmp');



I<will return>:

  tmp


This example will B<not> return a defined value:


  path_ext('/no-dots');




=back


=head2 path_normalize

=over

=item Normalize form of the given path



See also L</_normalize>


This example:


  path_normalize('/');



I<will return>:

  /


This example:


  path_normalize('/a/');



I<will return>:

  /a


This example:


  path_normalize('/a/b/..');



I<will return>:

  /a


This example:


  path_normalize('/a/../a');



I<will return>:

  /a


This example:


  path_normalize('/a/../../a');



I<will return>:

  /a


This example:


  path_normalize( "../../w/b/../s/x" );



I<will return>:

  ../../w/s/x


This example:


  path_normalize( "u/n/w/" );



I<will return>:

  u/n/w


This example:


  path_normalize( "u/../w/b/../s" );



I<will return>:

  w/s


This example:


  path_normalize( "u//n" );



I<will return>:

  u/n


This example:


  path_normalize( "u//n/./f" );



I<will return>:

  u/n/f


This example:


  path_normalize( "http://t/u//n" );



I<will return>:

  http://t/u/n


This example:


  path_normalize( '/a/b/c/../../../d/e/f' );



I<will return>:

  /d/e/f


This example:


  path_normalize( './a' );



I<will return>:

  ./a


This example:


  path_normalize( '/./a' );



I<will return>:

  /a



=back


=head2 path_parent

=over

=item Return the parent of the given path





This example will B<not> return a defined value:


  path_parent('');



This example:


  path_parent('/');



I<will return>:

  /


This example:


  path_parent('/a');



I<will return>:

  /


This example:


  path_parent('/a/b/..');



I<will return>:

  /


This example:


  path_parent('a');



I<will return>:

  


This example:


  path_parent('a/b/');



I<will return>:

  a


This example:


  path_parent('a/b.c');



I<will return>:

  a


This example:


  path_parent('../../a');



I<will return>:

  ../..



=back


=head2 path_name

=over

=item Return the name part of a path





This example will B<not> return a defined value:


  path_name('');



This example:


  path_name('/');



I<will return>:

  


This example:


  path_name('/a');



I<will return>:

  a


This example:


  path_name('/a/b/..');



I<will return>:

  a


This example:


  path_name('a/b/');



I<will return>:

  b


This example:


  path_name('a/b.c');



I<will return>:

  b.c



=back


=head2 path_is_absolute

=over

=item Is the path an absolute path





This example will return true:


  path_is_absolute('/a');



This example will return true:


  path_is_absolute('A:/b');



This example will return true:


  path_is_absolute('/a/b');



This example will return true:


  path_is_absolute('http://a');



This example will return true:


  path_is_absolute('svn+ssh://a.b');



This example will return false:


  path_is_absolute('');



This example will return false:


  path_is_absolute('a/b');



This example will return false:


  path_is_absolute('a:\\b');



This example will return false:


  path_is_absolute('a/../b');



This example will return false:


  path_is_absolute('../a');



This example will return false:


  path_is_absolute('./a');



This example will return false:


  path_is_absolute('svn+ssh//a.b');



This example will return false:


  path_is_absolute('1://');



This example will return false:


  path_is_absolute('+://');




=back


=head2 dir_read

=over

=item Return directory entries






=back


=head2 file_read

=over

=item Read file contents






=back


=head2 file_read_binary

=over

=item Read file contents (binary)






=back


=head2 file_create

=over

=item Create an empty file if none exists






=back


=head2 file_write

=over

=item Write contents to file




where:


  @contents           Items may be scalars or scalar references

returns:


  1 when successful
  dies if a handle cannot be obtained



=back


=head2 file_write_binary

=over

=item Write contents to file (binary)




where:


  @contents           Items may be scalars or scalar references

returns:


  1 when successful
  dies if a handle cannot be obtained



=back


=head2 file_read_crown

=over

=item Return the first line of a file






=back


=head2 fs_handle

=over

=item Return a file or directory handle




where:


  $path           # Absolute or relative file-system path
  $mode   r|w|rw  # Read or write access (default 'r')



=back


=head2 dir_create

=over

=item Create directory including parents






=back


=head2 file_copy

=over

=item Copy a file



See also: L<File::Copy::copy>



=back


=head2 file_move

=over

=item Move a file



See also: L<File::Copy::move>



=back


=head2 dir_copy

=over

=item Copy a directory recursively






=back


=head2 dir_copy_contents

=over

=item Copy a directory's contents recursively






=back


=head2 dir_remove_contents

=over

=item Remove a directories contents






=back


=head2 dir_is_system

=over

=item The directory is a system folder, such as '.svn'





This example will return true:

  # normal usage
  dir_is_system('.svn', '/tmp');



This example will return true:

  # normal usage
  dir_is_system('.git', '/tmp');



This example will return false:

  # case sensitive
  dir_is_system('.Svn', '/tmp');



This example will B<not> return a defined value:

  # undefined test
  dir_is_system(undef, undef);




=back


=head2 dir_move

=over

=item Move a directory



See also: L<File::Copy::move>



=back


=head2 file_remove

=over

=item Remove a file






=back


=head2 dir_remove

=over

=item Remove a directory recursively






=back


=head1 PACKAGE INTERNALS

=head2 FS

=over

=item Return the package name for the given file-system class





This example:


  FS('Node');



I<will return>:

  Data::Hub::FileSystem::Node



=back


=head2 _normalize

=over

=item Implementation method for L<addr_normalize> and L<path_normalize>






=back



=head1 AUTHORS

    Ryan Gies <ryangies@cpan.org>

=head1 COPYRIGHT

    Copyright (C) 2014-2016 by Ryan Gies. All rights reserved.
    Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved.
    Copyright (C) 2000-2005 by Ryan Gies. All rights reserved.
    Redistribution and use in source and binary forms, with or without 
    modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
    * The origin of this software must not be misrepresented; you must not 
    claim that you wrote the original software. If you use this software in a 
    product, an acknowledgment in the product documentation would be 
    appreciated but is not required.
    * Altered source versions must be plainly marked as such, and must not be 
    misrepresented as being the original software.
    * The name of the author may not be used to endorse or promote products 
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
    EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    To the best of our knowledge, no patented algorithms have been used. However, we
    do not have the resources to carry out a patent search, and therefore cannot 
    give any guarantee of the above statement.

=cut

