Common-CodingTools

 view release on metacpan or  search on metacpan

lib/Common/CodingTools.pm  view on Meta::CPAN

          HAPPY UNHAPPY SAD ANGRY
          WANTED UNWANTED
          PI
          TRUE FALSE
        )
    ],
    'functions' => [
        qw(
          slurp_file
          ltrim rtrim trim uc_lc leet_speak
          schwartzian_sort
          center
          tfirst
        )
    ],
    'all' => [
        qw(
          ON OFF
          SUCCESS SUCCESSFUL SUCCEEDED FAILURE FAILED FAIL
          ACTIVE INACTIVE
          HEALTHY UNHEALTHY EXPIRED NOTEXPIRED
          CLEAN DIRTY
          HAPPY UNHAPPY SAD ANGRY
          WANTED UNWANTED
          PI
          TRUE FALSE
          slurp_file
          ltrim rtrim trim uc_lc leet_speak
          schwartzian_sort
          center
          tfirst
        )
    ],
);

=head1 FUNCTIONS

X<slurp_file>
X<ltrim>
X<rtrim>
X<trim>
X<center>
X<uc_lc>
X<leet_speak>
X<schwartzian_sort>

=head2 slurp_file

Reads in a text file and returns the contents of that file as a single string.  It returns undef if the file is not found.

 my $string = slurp_file('/file/name');

=cut

sub slurp_file {
    my $file = shift;

    # Read in a text file without using open
    if (-e $file) {
        return (
            do { local (@ARGV, $/) = $file; <> }
        );
    }
    return (undef);
} ## end sub slurp_file

=head2 ltrim

Removes any spaces at the beginning of a string (the left side).

 my $result = ltrim($string);

=cut

sub ltrim {
    my $string = shift;
    if (defined($string) && $string ne '') {
        $string =~ s/^\s+//g;
    }
    return ($string);
} ## end sub ltrim

=head2 rtrim

Removes any spaces at the end of a string (the right side).

 my $result = rtrim($string);

=cut

sub rtrim {
    my $string = shift;
    if (defined($string) && $string ne '') {
        $string =~ s/\s+$//g;
    }
    return ($string);
} ## end sub rtrim

=head2 trim

Removes any spaces at the beginning and the end of a string.

 my $result = trim($string);

=cut

sub trim {
    my $string = shift;
    if (defined($string) && $string ne '') {
        $string =~ s/^\s+|\s+$//g;
    }
    return ($string);
} ## end sub trim

=head2 center

Centers a string, padding with leading spaces, in the middle of a given width.

 my $result = center($string, 80); # Centers text for an 80 column display

=cut



( run in 0.750 second using v1.01-cache-2.11-cpan-a9496e3eb41 )