CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Fun with regular expressions

 



Strider
Novice

Sep 27, 2002, 8:33 AM

Post #1 of 2 (3049 views)
Fun with regular expressions Can't Post

Here is a useful little package for retrieving values from a text file:

Code
package Properties; 
#####################################################################
# File : Properties.pm
# Description :
#####################################################################
use strict;
use Carp;
use FileHandle;
use vars qw($properties);

#====================================================================
# Method: addProperties
#====================================================================
sub addProperties
{
my $package = shift();
my $propFile = shift();
my $name = shift() || '__default__';
my $fh = FileHandle->new($propFile) || die($!);

#-----------------------------------------------------------------
# Read into a hash, referenced by $name, the contents of the file
# We assume a properties file consists of a number of lines of the
# form key = value. White space is ignored
#-----------------------------------------------------------------
#-----------------------------------------------------------------
# First strip comments ... then empty lines
#-----------------------------------------------------------------
my @lines = grep !/^\s*$/, map { s/\s*#.*//; $_ } <$fh>;
#-----------------------------------------------------------------
# First strip comments ... then empty lines
#-----------------------------------------------------------------
@lines = map { s/\$(\w+)/$ENV{$1}/g; $_ } @lines;
#-----------------------------------------------------------------
# ... then parse the key value pairs
#-----------------------------------------------------------------
$properties->{$name} = { map { $_ =~ m/(\S+)\s*=\s*(.*)/} @lines };
$fh->close();
}

#====================================================================
# Method: get
#====================================================================
sub get
{
my $package = shift();
my $property = shift();
my $name = shift() || '__default__';

return $properties->{$name}{$property};
}

#====================================================================
# Method: set
#====================================================================
sub set
{
my $package = shift();
my $property = shift();
my $value = shift();
my $name = shift() || '__default__';

return $properties->{$name}{$property} = $value;
}

1;



If I have a text file containing this:

Code
##################################################################### 
# File: properties.txt
#####################################################################
valueOne = 1 # number
valueTwo = $HOME/bin # path



I can use the values in another perl script like this:

Code
##################################################################### 
# File : sample.pl
# Description :
#####################################################################
use strict;
use Properties;

#====================================================================
# Get the properties
#====================================================================
my $propertyFile = shift() or die("Usage: $0 <propertyFile>");
Properties->addProperties($propertyFile);

printf("%d : %s",
Properties->get('valueOne'),
Properties->get('valueTwo'));



This can be a useful little tool for setting values on-the-fly. Just thought I would share.

David


thebitch
User

Oct 1, 2002, 10:31 AM

Post #2 of 2 (3034 views)
Re: [Strider] Fun with regular expressions [In reply to] Can't Post

A few style notes,
if you're going to bother putting () on a 'shift'
call, which is a perl built-in, you might as well put in @_,
so it becomes shift(@_).

it's 'shift' or shift(@_), mmkay ;D

Also, since you have more than 2, it might make more sense to

Code
my( $package, $propFile, $name ) = @_; 
$name ||= '__default__';


instead of

Code
   my $package  = shift();  
my $propFile = shift();
my $name = shift() || '__default__';


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives