School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,363 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,430 people online right now. Registration is fast and FREE... Join Now!




Using Hash element as variable

 

Using Hash element as variable

Sun751

1 Jul, 2009 - 12:50 AM
Post #1

D.I.C Head
**

Joined: 11 Dec, 2008
Posts: 57



Thanked: 1 times
My Contributions
To get rid of variables I am trying to use hash elements as following,

CODE

use strict;
use warnings;

my $config_file = 'release_config.ini';
my %config;

initilize_config(\%config,$config_file);
release_web(\%config);

sub initilize_config
{
    my ($HR_config,$file) = @_;
    open (my $fr, '<', "$file") || die "Unable to open configuration file: $file $!";
    while (my $line = <$fr>)
    {
        $line =~ tr/\r\n//d;
        next unless $line;
        if ($line =~ /=/)
        {
            my ($key,$val)=$line=~ /^(.*?)=(.*?)$/;
            $$HR_config{$key} = $val;
        }
    }
}

sub release_web
{
    my $HR_config = shift;
    change_dir($HR_config->{Distribution});
}

sub change_dir
{
    my $dir = shift;
    chdir($dir)|| die "chdir failed: Unable to change to directory, $dir $!\n";
}


Can any one suggest me if its smart way of write program or not ? Any suggestion please!!!

Cheers

User is offlineProfile CardPM
+Quote Post


KevinADC

RE: Using Hash Element As Variable

1 Jul, 2009 - 10:20 AM
Post #2

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
Only going by the code I see, I would probably code it more like this:

CODE

use strict;
use warnings;

my $config_file = 'release_config.ini';
my $HR_config = initilize_config($config_file);
release_web($HR_config->{Distribution});

sub initilize_config {
    my ($file) = @_;
    my %temp;
    open (my $fr, '<', $file) || die "Unable to open configuration file: $file $!";
    while (my $line = <$fr>) {
         if ($line=~ /^(.*?)=(.*?)$/) {
           $temp{$1} = $2;
        }
    }
    return(\%temp);
}

sub release_web {
    my ($dir) = @_;
    chdir($dir)|| die "chdir failed: Unable to change to directory, $dir $!\n";;
}


The regexp might be better written like so:

CODE
if ($line=~ /^(.+?)=(.+?)$/) {


otherwise it could return undefined values for $1 and $2 since .*? can match nothing




This post has been edited by KevinADC: 1 Jul, 2009 - 10:22 AM
User is offlineProfile CardPM
+Quote Post

dsherohman

RE: Using Hash Element As Variable

2 Jul, 2009 - 02:32 AM
Post #3

D.I.C Head
**

Joined: 29 Mar, 2009
Posts: 184



Thanked: 35 times
My Contributions
QUOTE(KevinADC @ 1 Jul, 2009 - 06:20 PM) *

The regexp might be better written like so:

CODE
if ($line=~ /^(.+?)=(.+?)$/) {


otherwise it could return undefined values for $1 and $2 since .*? can match nothing


Better still:
CODE
if ($line=~ /^([^=]+)=(.*)$/) {

For the first match, if you only want non-= characters, then ask for "only non-= characters", not "the shortest possible sequence of characters which is followed by an =". Aside from being more explicit about what you want, using the negated character class also completely eliminates any possibility that the regex engine could get confused on certain input strings and start backtracking in search of a better match.

For the second match, there's no point in making a match-to-end-of-line non-greedy. It can't match without consuming everything anyhow.

(Perhaps I should add "campaigner against abuse of non-greedy . in regex" to my forum signature... Or maybe "Knight of the Negated Character Class" sounds better...)
User is offlineProfile CardPM
+Quote Post

KevinADC

RE: Using Hash Element As Variable

2 Jul, 2009 - 10:49 AM
Post #4

D.I.C Regular
Group Icon

Joined: 23 Jan, 2007
Posts: 401



Thanked: 25 times
Dream Kudos: 50
My Contributions
QUOTE(dsherohman @ 2 Jul, 2009 - 02:32 AM) *



Better still:
CODE
if ($line=~ /^([^=]+)=(.*)$/) {





I totally agree. I was just trying to stick with his code as much as I was comfortable with. But I would also write it like you suggest using a negated character class.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 08:35PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month