| line | stmt | bran | cond | sub | pod | time | code | 
| 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 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | package Stem::Util ; | 
| 30 |  |  |  |  |  |  |  | 
| 31 | 3 |  |  | 3 |  | 14 | use strict ; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 92 |  | 
| 32 | 3 |  |  | 3 |  | 16 | use Carp ; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 1435 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | =head1 Stem::Util | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | This file includes two subroutines: read_file and write_file. | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | =cut | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | =head2 read_file | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | read_file is a utility sub to slurp in a file. | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | It returns a list of lines when called in list context. | 
| 45 |  |  |  |  |  |  | It 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 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub read_file { | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 |  |  | 0 | 1 |  | my( $file_name ) = shift ; | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  |  | local( *FH ) ; | 
| 57 | 0 | 0 |  |  |  |  | open( FH, $file_name ) || carp "can't open $file_name $!" ; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 0 | 0 |  |  |  |  | return  if wantarray ; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 0 |  |  |  |  |  | my $buf ; | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 0 |  |  |  |  |  | sysread( FH, $buf, -s FH ) ; | 
| 64 | 0 |  |  |  |  |  | return $buf ; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | =head2 load_file | 
| 68 |  |  |  |  |  |  |  | 
| 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: | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | YAML | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =cut | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub load_file { | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 0 |  |  | 0 | 1 |  | my( $file_name ) = shift ; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  |  | my $text = read_file( $file_name ) ; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | my @load_vals ; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 | 0 |  |  |  |  | if ( $text =~ /^.*#YAML/ ) { | 
| 87 |  |  |  |  |  |  |  | 
| 88 | 0 |  |  |  |  |  | require YAML ; | 
| 89 |  |  |  |  |  |  |  | 
| 90 | 0 |  |  |  |  |  | eval { | 
| 91 | 0 |  |  |  |  |  | @load_vals =  YAML::Load( $text ) ; | 
| 92 |  |  |  |  |  |  | } ; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 | 0 |  |  |  |  | return "Load error in file '$file_name' with YAML: $@" if $@ ; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # lose the outer anon array wrapper and return the values | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 0 |  |  |  |  |  | return $load_vals[0] ; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 0 |  |  |  |  |  | @load_vals = eval "($text)" ; | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 0 | 0 |  |  |  |  | return "Load error in file '$file_name' with eval: $@" if $@ ; | 
| 104 | 0 |  |  |  |  |  | return \@load_vals ; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head2 write_file | 
| 109 |  |  |  |  |  |  |  | 
| 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 | 
| 113 |  |  |  |  |  |  | in the file. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =cut | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # utility sub to write a file. takes a file name and a list of strings | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | sub write_file { | 
| 120 |  |  |  |  |  |  |  | 
| 121 | 0 |  |  | 0 | 1 |  | my( $file_name ) = shift ; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  |  | local( *FH ) ; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 0 | 0 |  |  |  |  | open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  |  | print FH @_ ; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | 1 ; |