3 # This file is part of Stem.
4 # Copyright (C) 1999, 2000, 2001 Stem Systems, Inc.
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.
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.
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
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:
24 # Stem Systems, Inc. 781-643-7504
25 # 79 Everett St. info@stemsystems.com
36 This file includes two subroutines: read_file and write_file.
42 read_file is a utility sub to slurp in a file.
44 It returns a list of lines when called in list context.
45 It returns one big string when called in scalar context.
49 # utility sub to slurp in a file. list/scalar context determines either
50 # list of lines or long single string
54 my( $file_name ) = shift ;
57 open( FH, $file_name ) || carp "can't open $file_name $!" ;
59 return <FH> if wantarray ;
63 sysread( FH, $buf, -s FH ) ;
69 load_file is a utility sub to load a file of data. It reads in a file
70 and converts it to an internal form according to the first line of the
71 file. The default file format is Perl data and eval is used to convert
72 it. These other formats are also supported:
80 my( $file_name ) = shift ;
82 my $text = read_file( $file_name ) ;
86 if ( $text =~ /^.*#YAML/ ) {
91 @load_vals = YAML::Load( $text ) ;
94 return "Load error in file '$file_name' with YAML: $@" if $@ ;
96 # lose the outer anon array wrapper and return the values
98 return $load_vals[0] ;
101 @load_vals = eval "($text)" ;
103 return "Load error in file '$file_name' with eval: $@" if $@ ;
110 write_sub is a utility sub to write a file. It takes a file
111 name and a list of strings. It opens the file and writes
112 all data passed into the file. This will overwrite any data
117 # utility sub to write a file. takes a file name and a list of strings
121 my( $file_name ) = shift ;
125 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;