Merge branch 'master' of steve@erxz.com:/home/uri/git_repo/stem
[urisagit/Stem.git] / lib / Stem / Util.pm
CommitLineData
4536f655 1# File: Stem/Util.pm
2
3# This file is part of Stem.
4# Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
5
6# Stem is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10
11# Stem is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with Stem; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20# For a license to use the Stem under conditions other than those
21# described here, to purchase support for this software, or to purchase a
22# commercial warranty contract, please contact Stem Systems at:
23
24# Stem Systems, Inc. 781-643-7504
25# 79 Everett St. info@stemsystems.com
26# Arlington, MA 02474
27# USA
28
29package Stem::Util ;
30
31use strict ;
32use Carp ;
33
34=head1 Stem::Util
35
36This file includes two subroutines: read_file and write_file.
37
38=cut
39
40=head2 read_file
41
42read_file is a utility sub to slurp in a file.
43
44It returns a list of lines when called in list context.
45It returns one big string when called in scalar context.
46
47=cut
48
49# utility sub to slurp in a file. list/scalar context determines either
50# list of lines or long single string
51
52sub read_file {
53
54 my( $file_name ) = shift ;
55
56 local( *FH ) ;
57 open( FH, $file_name ) || carp "can't open $file_name $!" ;
58
59 return <FH> if wantarray ;
60
61 my $buf ;
62
63 sysread( FH, $buf, -s FH ) ;
64 return $buf ;
65}
66
67=head2 load_file
68
69load_file is a utility sub to load a file of data. It reads in a file
70and converts it to an internal form according to the first line of the
71file. The default file format is Perl data and eval is used to convert
72it. These other formats are also supported:
73
74 YAML
75
76=cut
77
78sub load_file {
79
80 my( $file_name ) = shift ;
81
82 my $text = read_file( $file_name ) ;
83
84 my @load_vals ;
85
86 if ( $text =~ /^.*#YAML/ ) {
87
88 require YAML ;
89
90 eval {
91 @load_vals = YAML::Load( $text ) ;
92 } ;
93
94 return "Load error in file '$file_name' with YAML: $@" if $@ ;
95
96# lose the outer anon array wrapper and return the values
97
98 return $load_vals[0] ;
99 }
100
101 @load_vals = eval "($text)" ;
102
103 return "Load error in file '$file_name' with eval: $@" if $@ ;
104 return \@load_vals ;
105}
106
107
108=head2 write_file
109
110write_sub is a utility sub to write a file. It takes a file
111name and a list of strings. It opens the file and writes
112all data passed into the file. This will overwrite any data
113in the file.
114
115=cut
116
117# utility sub to write a file. takes a file name and a list of strings
118
119sub write_file {
120
121 my( $file_name ) = shift ;
122
123 local( *FH ) ;
124
125 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
126
127 print FH @_ ;
128}
129
1301 ;