Devel::PerlySense::Util - Utility routines


Devel-PerlySense documentation Contained in the Devel-PerlySense distribution.

Index


Code Index:

NAME

Top

Devel::PerlySense::Util - Utility routines

ROUTINES

Top

aNamedArg($raParam, @aArg)

Return list of argument valies in $rhArg for the param names in $raParam.

Die on missing arguments.

slurp($file)

Read the contents of $file and return it, or undef if the file couldn't be opened.

spew($file, $text)

Crete a new $file a and print $text to it.

Return 1 on success, else 0.

filePathNormalize($file)

Return the normalized path of $file, i.e. with "dir/dir2/../dir3" becoming "dir/dir3".

The path doesn't have to exist.

textRenderTemplate($template, $rhParam)

Replace the keys in $rhParam with the values in $rhParam, for everything in $template that looks like

  ${KEY_NAME}

Return the rendered template.

AUTHOR

Top

Johan Lindström, <johanl[ÄT]DarSerMan.com>

BUGS

Top

Please report any bugs or feature requests to bug-devel-perlysense@rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-PerlySense. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

ACKNOWLEDGEMENTS

Top

COPYRIGHT & LICENSE

Top


Devel-PerlySense documentation Contained in the Devel-PerlySense distribution.


use strict;
use warnings;

package Devel::PerlySense::Util;
use base "Exporter";

our @EXPORT = (
    qw/
       slurp
       spew
       textRenderTemplate
       filePathNormalize
       /);

our $VERSION = '0.01';




use Carp;
use Data::Dumper;
use File::Basename;
use Path::Class 0.11;
use File::Spec::Functions qw/ splitdir /;





sub aNamedArg {
	my ($raParam, @aArg) = @_;
    my %hArg = @aArg;

    my @aResult;
    for my $param (@$raParam) {
        exists $hArg{$param} or do {
            local $Carp::CarpLevel = 1;
            croak("Missing argument ($param). Arguments: (" . join(", ", sort keys %hArg) . ")");
        };
        push(@aResult, $hArg{$param});
    }

    return(@aResult);
}





sub slurp {
	my ($file) = @_;
    open(my $fh, "<", $file) or return undef;
    local $/;
    return <$fh>;
}





sub spew {
	my ($file, $text) = @_;
    open(my $fh, ">", $file) or return 0;
    print $fh $text or return 0;
    return 1;
}





sub filePathNormalize {
	my ($filePath) = @_;

    my @aDirNew;
    for my $dir (splitdir($filePath)) {
        if($dir eq "..") {
            ###TODO: @aDirNew or die("Malformed file ($filePath). Too many parent dirs ('sample_dir/../..')\n");
            pop(@aDirNew);
        }
        else {
            push(@aDirNew, $dir);
        }
    }
    
    return file(@aDirNew) . "";
}





sub textRenderTemplate {
    my ($template, $rhParam) = @_;

    my $rex = join("|", map { quotemeta } sort keys %$rhParam);

    $template =~ s/\${($rex)}/ $rhParam->{$1} || "" /eg;  ###TODO: should be //

    return $template;
}





1;





__END__