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: Beginner:
How to emulate the given shell script in perl

 



Tejas
User

Oct 19, 2016, 4:16 AM

Post #1 of 5 (2353 views)
How to emulate the given shell script in perl Can't Post


Code
 
TEST_COMMON_CFG_FILE=ARGV[0];
# Return the value of the specific field from the test common configuration file
getTestCommonConfigValue() {
grep "$1=" $TEST_COMMON_CFG_FILE | cut -d '=' -f2 | sed -e 's/\s*$//'
}


# Return the value of the specific field from the test configuration file
getTestConfigValue() {
grep "$1=" $TEST_SPECIFIC_CFG_FILE | cut -d '=' -f2 | sed -e 's/\s*$//'
}

# Read the following items from the Test common configuration file

WSSM_INSTALL_DIR=$(getTestCommonConfigValue "WSSM_INSTALL_DIR");
MA_INSTALL_DIR=$(getTestCommonConfigValue "MA_INSTALL_DIR");
MA_DATA_DIR=$(getTestCommonConfigValue "MA_DATA_DIR");


iam trying to read a file and copying the dir path in a variable.

But if i copy the same in perl script , its failing to compile

How to do the same in perl ?


Thanks
Tejas


Laurent_R
Veteran / Moderator

Oct 19, 2016, 5:07 AM

Post #2 of 5 (2348 views)
Re: [Tejas] How to emulate the given shell script in perl [In reply to] Can't Post

Come on Tejas, it's not your first question here, you know at least some Perl. You should guess that a shell script won't pass the Perl compiler.

Some help for the main function:


Code
getTestCommonConfigValue { 
my $regex = shift;
open my $IN, "<", $TEST_COMMON_CFG_FILE or die "cannot open $TEST_COMMON_CFG_FILE $!";
while ($IN) {
chomp;
next unless /$regex/;
my $field = (split /=/, $_)[1];
$field = s/\s+//;
return $field;
}
}


But I would really rewrite the whole thing differently in order to read the input file only once.


Tejas
User

Oct 19, 2016, 12:06 PM

Post #3 of 5 (2334 views)
Re: [Laurent_R] How to emulate the given shell script in perl [In reply to] Can't Post

Laurent
Sorry for that
I tried below code and is helpful

Code
 use Data::Dumper; 

my $hash =
{
map
{
my $row = $_;
$row =~ s/\s*$//;
split /=/, $row;
}
<DATA>
};

print Dumper $hash;

__DATA__
one=1
two=2
three=3


Thanks


FishMonger
Veteran / Moderator

Oct 19, 2016, 2:25 PM

Post #4 of 5 (2330 views)
Re: [Tejas] How to emulate the given shell script in perl [In reply to] Can't Post

Why not use a proper config parsing module?


Code
#!/usr/bin/perl 

use strict;
use warnings FATAL => 'all';
use Config::General;
use Data::Dumper;

my $conf = Config::General->new(\*DATA);
my %conf = $conf->getall;
print Dumper \%conf;

__DATA__
one=1
two=2
three=3


c:\test>Perl-1.pl

Code
$VAR1 = { 
'two' => '2',
'three' => '3',
'one' => '1'
};



(This post was edited by FishMonger on Oct 19, 2016, 2:26 PM)


FishMonger
Veteran / Moderator

Oct 19, 2016, 2:32 PM

Post #5 of 5 (2328 views)
Re: [FishMonger] How to emulate the given shell script in perl [In reply to] Can't Post

You can even do method chaining to reduce the parsing to 1 line.


Code
my %conf = Config::General->new(\*DATA)->getall(); 
print Dumper \%conf;


 
 


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

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