Lazy-Utils

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

		- $whitespace argument changed to $nonquoted in shellmeta()
		- commandArgs() and cmdArgs() improvements

1.11	2017-03-03
		- POD improvements

1.10	2017-03-03
		- getPodText improvements

1.09	2017-02-25
		- Added $prefs ref check
		- Little changes of file mode in file_get_contents/file_put_contents

1.08	2017-01-23
		- Added utf8 mode to file_get_contents/file_put_contents

1.07	2017-01-23
		- Edited pre-requirements
		- Fix for file_put_contents
		- Fix for fileCache utf8 problem
		- Fix for getPodText utf8 problem

README  view on Meta::CPAN


VERSION
    version 1.22

SYNOPSIS
            use Lazy::Utils;
         
            trim($str);
            ltrim($str);
            rtrim($str);
            file_get_contents($path, $prefs);
            file_put_contents($path, $contents, $prefs);
            shellmeta($s, $nonquoted);
            system2($cmd, @argv);
            bash_readline($prompt);
            cmdargs($prefs, @argv);
            whereis($name, $path);
            file_cache($tag, $expiry, $coderef);
            get_pod_text($file_name, $section, $exclude_section);
            array_to_hash(@array);

DESCRIPTION
    Collection of utility functions all of exported by default.

FUNCTIONS
  trim($str)

README  view on Meta::CPAN


    return value: *trimmed string*

  rtrim($str)
    trims right given string

    $str: *string will be trimmed*

    return value: *trimmed string*

  file_get_contents($path, $prefs)
    gets all contents of file in string type

    $path: *path of file*

    $prefs: *preferences in HashRef, by default undef*

        utf8: *opens file-handle as :utf8 mode, by default 0*

    return value: *file contents in string type, otherwise undef because of
    errors*

  file_put_contents($path, $contents, $prefs)
    puts all contents of file in string type

    $path: *path of file*

    $contents: *file contents in string type*

    $prefs: *preferences in HashRef, by default undef*

        utf8: *opens file-handle as :utf8 mode, by default 0*

    return value: *success 1, otherwise undef*

  shellmeta($s, $nonquoted)
    escapes metacharacters of interpolated shell string

    $s: *interpolated shell string*

README  view on Meta::CPAN


  bash_readline($prompt)
    bashReadLine($prompt) *OBSOLETE*

    reads a line from STDIN using Bash

    $prompt: *prompt, by default ''*

    return value: *line*

  cmdargs([$prefs, ]@argv)
    commandArgs([$prefs, ]@argv) *OBSOLETE*

    cmdArgs([$prefs, ]@argv) *OBSOLETE*

    resolves command line arguments

    $prefs: *preferences in HashRef, optional*

        valuableArgs: *accepts option value after option if next argument is
        not an option, by default 0*

        noCommand: *use first parameter instead of command, by default 0*

        optionAtAll: *accepts options after command or first parameter
        otherwise evaluates as parameter, by default 1*

    @argv: *command line arguments*

README.md  view on Meta::CPAN


version 1.22

# SYNOPSIS

        use Lazy::Utils;
         
        trim($str);
        ltrim($str);
        rtrim($str);
        file_get_contents($path, $prefs);
        file_put_contents($path, $contents, $prefs);
        shellmeta($s, $nonquoted);
        system2($cmd, @argv);
        bash_readline($prompt);
        cmdargs($prefs, @argv);
        whereis($name, $path);
        file_cache($tag, $expiry, $coderef);
        get_pod_text($file_name, $section, $exclude_section);
        array_to_hash(@array);

# DESCRIPTION

Collection of utility functions all of exported by default.

# FUNCTIONS

README.md  view on Meta::CPAN

return value: _trimmed string_

## rtrim($str)

trims right given string

$str: _string will be trimmed_

return value: _trimmed string_

## file\_get\_contents($path, $prefs)

gets all contents of file in string type

$path: _path of file_

$prefs: _preferences in HashRef, by default undef_

> utf8: _opens file-handle as :utf8 mode, by default 0_

return value: _file contents in string type, otherwise undef because of errors_

## file\_put\_contents($path, $contents, $prefs)

puts all contents of file in string type

$path: _path of file_

$contents: _file contents in string type_

$prefs: _preferences in HashRef, by default undef_

> utf8: _opens file-handle as :utf8 mode, by default 0_

return value: _success 1, otherwise undef_

## shellmeta($s, $nonquoted)

escapes metacharacters of interpolated shell string

$s: _interpolated shell string_

README.md  view on Meta::CPAN

## bash\_readline($prompt)

**bashReadLine($prompt)** _OBSOLETE_

reads a line from STDIN using Bash

$prompt: _prompt, by default ''_

return value: _line_

## cmdargs(\[$prefs, \]@argv)

**commandArgs(\[$prefs, \]@argv)** _OBSOLETE_

**cmdArgs(\[$prefs, \]@argv)** _OBSOLETE_

resolves command line arguments

$prefs: _preferences in HashRef, optional_

> valuableArgs: _accepts option value after option if next argument is not an option, by default 0_
>
> noCommand: _use first parameter instead of command, by default 0_
>
> optionAtAll: _accepts options after command or first parameter otherwise evaluates as parameter, by default 1_

@argv: _command line arguments_

        -a -b=c -d e --f g --h --i=j k l -- m n

lib/Lazy/Utils.pm  view on Meta::CPAN


version 1.22

=head1 SYNOPSIS

	use Lazy::Utils;
	 
	trim($str);
	ltrim($str);
	rtrim($str);
	file_get_contents($path, $prefs);
	file_put_contents($path, $contents, $prefs);
	shellmeta($s, $nonquoted);
	system2($cmd, @argv);
	bash_readline($prompt);
	cmdargs($prefs, @argv);
	whereis($name, $path);
	file_cache($tag, $expiry, $coderef);
	get_pod_text($file_name, $section, $exclude_section);
	array_to_hash(@array);

=head1 DESCRIPTION

Collection of utility functions all of exported by default.

=cut

lib/Lazy/Utils.pm  view on Meta::CPAN

return value: I<trimmed string>

=cut
sub rtrim
{
	my ($s) = @_;
	$s =~ s/\s+$//;
	return $s
}

=head2 file_get_contents($path, $prefs)

gets all contents of file in string type

$path: I<path of file>

$prefs: I<preferences in HashRef, by default undef>

=over

utf8: I<opens file-handle as :utf8 mode, by default 0>

=back

return value: I<file contents in string type, otherwise undef because of errors>

=cut
sub file_get_contents
{
	my ($path, $prefs) = @_;
	$prefs = {} unless ref($prefs) eq 'HASH';
	my $result = do
	{
		local $/ = undef;
		my $mode = "";
		$mode .= " :utf8" if $prefs->{utf8};
		open my $fh, "<$mode", $path or return;
		my $result = <$fh>;
		close $fh;
		$result;
	};
	return $result;
}

=head2 file_put_contents($path, $contents, $prefs)

puts all contents of file in string type

$path: I<path of file>

$contents: I<file contents in string type>

$prefs: I<preferences in HashRef, by default undef>

=over

utf8: I<opens file-handle as :utf8 mode, by default 0>

=back

return value: I<success 1, otherwise undef>

=cut
sub file_put_contents
{
	my ($path, $contents, $prefs) = @_;
	return if not defined($contents) or ref($contents);
	$prefs = {} unless ref($prefs) eq 'HASH';
	my $result = do
	{
		local $\ = undef;
		my $mode = "";
		$mode .= " :utf8" if $prefs->{utf8};
		open my $fh, ">$mode", $path or return;
		my $result = print $fh $contents;
		close $fh;
		$result;
	};
	return $result;
}

=head2 shellmeta($s, $nonquoted)

lib/Lazy/Utils.pm  view on Meta::CPAN

	local $/ = "\n";
	my $cmd = '/usr/bin/env bash -c "read -p \"'.shellmeta(shellmeta($prompt)).'\" -r -e && echo -n \"\$REPLY\" 2>/dev/null"';
	$_ = `$cmd`;
	return (not $?)? $_: undef;
}
sub bashReadLine
{
	return bash_readline(@_);
}

=head2 cmdargs([$prefs, ]@argv)

B<commandArgs([$prefs, ]@argv)> I<OBSOLETE>

B<cmdArgs([$prefs, ]@argv)> I<OBSOLETE>

resolves command line arguments

$prefs: I<preferences in HashRef, optional>

=over

valuableArgs: I<accepts option value after option if next argument is not an option, by default 0>

noCommand: I<use first parameter instead of command, by default 0>

optionAtAll: I<accepts options after command or first parameter otherwise evaluates as parameter, by default 1>

=back

lib/Lazy/Utils.pm  view on Meta::CPAN


	{ -a => '', -b => 'c', -d => '', --f => '', --h => '', --i => 'j', command => undef, parameters => ['e', 'g', 'k', 'l'], late_parameters => ['m', 'n'] }

if optionAtAll is off, return value:

	{ -a => '', -b => 'c', -d => '', command => 'e', parameters => ['--f', 'g', '--h', '--i=j', 'k', 'l', '--','m', 'n'], late_parameters => [] }

=cut
sub cmdargs
{
	my $prefs = {};
	$prefs = shift if @_ >= 1 and ref($_[0]) eq 'HASH';
	my @argv = @_;
	my %result;
	$result{command} = undef;
	$result{parameters} = undef;

	my @parameters;
	my @late_parameters;
	my $late;
	my $opt;
	while (@argv)
	{
		my $argv = shift @argv;
		next unless defined($argv) and not ref($argv);

		if (not (not defined($prefs->{optionAtAll}) or $prefs->{optionAtAll}) and @parameters)
		{
			push @parameters, $argv;
			next;
		}

		if ($late)
		{
			push @late_parameters, $argv;
			next;
		}

lib/Lazy/Utils.pm  view on Meta::CPAN

			my @arg = split('=', $argv, 2);
			$result{$arg[0]} = $arg[1];
			unless (defined($result{$arg[0]}))
			{
				$result{$arg[0]} = "";
				$opt = $arg[0];
			}
			next;
		}

		if ($prefs->{valuableArgs} and $opt)
		{
			$result{$opt} = $argv;
			$opt = undef;
			next;
		}
		$opt = undef;

		push @parameters, $argv;
	}

	$result{command} = shift @parameters unless $prefs->{noCommand};
	$result{parameters} = \@parameters;
	$result{late_parameters} = \@late_parameters;

	return \%result;
}
sub commandArgs
{
	return cmdargs(@_);
}
sub cmdArgs



( run in 0.934 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )