DocSet

 view release on metacpan or  search on metacpan

lib/DocSet/Util.pm  view on Meta::CPAN

package DocSet::Util;

use strict;
use warnings;

use Symbol ();
use File::Basename ();
use File::Copy ();
use File::Path ();
use File::Find ();
use Data::Dumper;
use Carp;
use Template;
use File::Spec;
use File::Spec::Functions;

require DocSet::RunTime; # interdependency with DocSet::Util

use constant IS_WIN32 => $^O eq 'MSWin32';
use constant PERL_LT_580 => $] < 5.008;

use vars qw(@ISA @EXPORT);
@ISA    = qw(Exporter);
@EXPORT = qw(read_file read_file_paras copy_file gzip_file write_file
             create_dir filename filename_ext require_package dumper
             sub_trace note get_date get_timestamp proc_tmpl
             build_matchmany_sub banner confess cluck carp
             format_bytes expand_dir which path2uri);

# copy_file($src_path, $dst_path);
# copy a file at $src_path to $dst_path, 
# if one of the directories of the $dst_path doesn't exist -- it'll
# be created.
###############
sub copy_file {
    my ($src, $dst) = @_;

    die "$src doesn't exist" unless -e $src;
    my $mode = (stat _)[2];

    # make sure that the directory exist or create one
    my $base_dir = File::Basename::dirname $dst;
    create_dir($base_dir) unless (-d $base_dir);

    # File::Copy::syscopy doesn't preserve the mode :(
    File::Copy::syscopy($src, $dst);
    chmod $mode, $dst;
}

# gzip_file($src_path);
# gzip a file at $src_path
###############
sub gzip_file {
    my ($src) = @_;
    system "gzip -f $src";
}


# write_file($filename, $ref_to_array||scalar);
# content will be written to the file from the passed array of
# paragraphs
###############
sub write_file {
    my ($filename, $content) = @_;

    # make sure that the directory exist or create one
    my $dir = File::Basename::dirname $filename;
    create_dir($dir) unless -d $dir;

    my $fh = Symbol::gensym;
    open $fh, ">$filename" or croak "Can't open $filename for writing: $!";
    print $fh ref $content ? @$content : defined $content ? $content : '';
    close $fh;
}


# recursively creates a multi-layer directory
###############
sub create_dir {
    my $path = shift;
    return if !defined($path) || -e $path;
    # META: mode could be made configurable
    File::Path::mkpath($path, 0, 0755) or croak "Couldn't create $path: $!";
}

# read_file($filename, $ref);
# assign to a ref to a scalar
###############
sub read_file {
    my ($filename, $r_content) = @_;

    my $fh = Symbol::gensym;
    open $fh, $filename  or croak "Can't open $filename for reading: $!";
    local $/;
    $$r_content = <$fh>;
    close $fh;

}

# read_file_paras($filename, $ref_to_array);
# read by paragraph
# content will be set into a ref to an array
###############
sub read_file_paras {
    my ($filename, $ra_content) = @_;

    my $fh = Symbol::gensym;



( run in 1.897 second using v1.01-cache-2.11-cpan-39bf76dae61 )