Link to home
Start Free TrialLog in
Avatar of parlepoint

asked on

need help in perl script

hi guys,

i kind of stuck and i am beginer in perl so help me to solve my problem

i have to project listed in xml file and each project has own env variable which need to be export.
now so far i made script using xml parser to read project list but then i kind of help me out.

perl script===


# use module
use XML::Simple;
use Data::Dumper;

# create object
$xml = new XML::Simple (KeyAttr=>[]);

# read XML file
$data = $xml->XMLin("projectlist.xml");

# dereference hash ref
# access <cml> array
foreach $e (@{$data->{list}})
      print "$one. $e->{project}", "\n";
      print "\n";
print " -> Enter selection number and press ENTER:", "\n";

$a = <STDIN>;
print "operator entered $a " ;

XML file


1. SCAL-Choice

2. SCAL-Menu

 -> Enter selection number and press ENTER:
operator entered 1

Avatar of Adam314

What kind of output do you want?
Avatar of parlepoint


i need if i enterd option no 1 then what ever the project name is assign to no 1 is to be displayed & what are the variable related to that project in xml file that  has to be export & display.

1. SCAL-Choice
2. SCAL-Menu
 -> Enter selection number and press ENTER:
operator entered 1 which is SCAL-Choice

(this variable has to export & display)

my $xml = XMLin("projectlist.xml", ForceArray => ['list']);
for(my $i=0; $i<=$#{$xml->{list}}; $i++) {
	printf("%d: %s\n", $i+1, $xml->{list}->[$i]->{project});
my $sel = <STDIN>;
foreach (keys %{$xml->{list}->[$sel]}) {
	next if /project/;
	print "$_ = $xml->{list}->[$sel]->{$_}\n";

Open in new window

Thank you for your quick reply.
i really appreciate for your help and quick response.

i need to show which project selected (project name should be displayed) & project related  variable should be exported as environment variable.
so if i do echo i should get that value.

i got this output:
1: SCAL-Choice
2: SCAL-Menu
builddir = ~/BUILD/SCAL-Choice
mavenhome = ~/CMUTILITIES/maven
javahome = /usr/jdk1.2
To display the project, remove line 9:
    next if /project/;

As to environment variables...
    Are you on windows, or unix?
    Do you want the values of those variables to exist after your perl script finishes?

If you are on windows, or you don't need the variables after your perl script finishes, add this after line 10 (inside the foreach loop):
    $ENV{$_} = $xml->{list}->[$sel]->{$_};

If you are on unix, and need the values after your script exists, you will have a problem.  The perl script runs as a child of the shell that started it.  On unix, the children cannot affect the environment of their parents.  If you need this, then you will have to figure out another way to do what you want.  If you post what you are trying to achieve, people here can help figure out the best way.
Thanks for your expert comment.

i have Linux 2.6.9 version.
i have to export env variable as those are necessary for set up individual projects.

If you can help me in this, that will be great help.

If you need the env vars in the shell, you could write the output to a file, then source the file in your shell:

open(OUT,"$ENV{HOME}/project_vars") or die "Could not open file: $!\n";
foreach (keys %{$xml->{list}->[$sel]}) {
    print "$_ = $xml->{list}->[$sel]->{$_}\n";
    print OUT "export $_=$xml->{list}->[$sel]->{$_}\n";

Then after running, enter one of these
    . ~/project_vars
    source ~/project_vars

If you don't need the vars in the shell, but just need to start some other program, you could have perl start that program using system
hi Adam,

all the time i am getting error of
Could not open file:

i checked permission which is 755  but some how not able to open
i run script as &  as per your instruction, but both time same error..
can you help me out to debug this situation....

This is change in script according to your advice...............

open(OUT,"home/cmbuild3/project_vars") or die "Could not open file: $!\n";
foreach (keys %{$xml->{list}->[$sel]}) {
    print "$_ = $xml->{list}->[$sel]->{$_}\n";
    print OUT "export $_=$xml->{list}->[$sel]->{$_}\n";

Avatar of Adam314

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adam,  Thanks for your all help.