1 |
adcroft |
1.1 |
package Pod::HtmlPsPdf::Common; |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use Symbol (); |
5 |
|
|
use File::Basename (); |
6 |
|
|
use File::Copy (); |
7 |
|
|
|
8 |
|
|
use Pod::HtmlPsPdf::Config (); |
9 |
|
|
my $config = Pod::HtmlPsPdf::Config->new(); |
10 |
|
|
|
11 |
|
|
### Common functions |
12 |
|
|
|
13 |
|
|
# copy_file($src_filename,$dest_filename); $src_filename will be |
14 |
|
|
# placed into the specified path, if one of the directories in the |
15 |
|
|
# target path doesn't exist -- it'll be created. |
16 |
|
|
############### |
17 |
|
|
sub copy_file{ |
18 |
|
|
my ($src,$dst) = @_; |
19 |
|
|
|
20 |
|
|
# make sure that the directory exist or create one |
21 |
|
|
|
22 |
|
|
my $base_dir = File::Basename::dirname $dst; |
23 |
|
|
create_dir($base_dir) unless (-d $base_dir); |
24 |
|
|
|
25 |
|
|
File::Copy::copy($src,$dst); |
26 |
|
|
|
27 |
|
|
} # end of sub copy_file |
28 |
|
|
|
29 |
|
|
|
30 |
|
|
# write_file($filename,$ref_to_array); |
31 |
|
|
# content will be written to the file from the passed array of |
32 |
|
|
# paragraphs |
33 |
|
|
############### |
34 |
|
|
sub write_file{ |
35 |
|
|
my ($fn,$ra_content) = @_; |
36 |
|
|
|
37 |
|
|
# make sure that the directory exist or create one |
38 |
|
|
|
39 |
|
|
my $base_dir = File::Basename::dirname $fn; |
40 |
|
|
create_dir($base_dir) unless (-d $base_dir); |
41 |
|
|
|
42 |
|
|
my $fh = Symbol::gensym; |
43 |
|
|
open $fh, ">$fn" or die "Can't open $fn for writing: $!\n"; |
44 |
|
|
print $fh @$ra_content; |
45 |
|
|
close $fh; |
46 |
|
|
|
47 |
|
|
} # end of sub write_file |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
# recursively creates a multi-layer directory |
51 |
|
|
############### |
52 |
|
|
sub create_dir{ |
53 |
|
|
my $dir = shift || ''; |
54 |
|
|
return if !$dir or -d $dir; |
55 |
|
|
my $ancestor_dir = File::Basename::dirname $dir; |
56 |
|
|
create_dir($ancestor_dir); |
57 |
|
|
my $mode = $config->get_param('dir_mode'); |
58 |
|
|
mkdir $dir, $mode; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
# read_file_paras($filename,$ref_to_array); |
62 |
|
|
# content will be returned in the passed array of paragraphs |
63 |
|
|
############### |
64 |
|
|
sub read_file{ |
65 |
|
|
my ($fn,$ra_content) = @_; |
66 |
|
|
|
67 |
|
|
my $fh = Symbol::gensym; |
68 |
|
|
open $fh, $fn or die "Can't open $fn for reading: $!\n"; |
69 |
|
|
local $/ = ""; |
70 |
|
|
@$ra_content = <$fh>; |
71 |
|
|
close $fh; |
72 |
|
|
|
73 |
|
|
} # end of sub read_file |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
1; |
77 |
|
|
__END__ |