rahulruns has asked for the wisdom of the Perl Monks concerning the following question:
I have an pl file that reads an xml and create another pl file. File is created by looping through an array. Issue is it writes for first element of array but not for further elements. I understand that it is perl features that it opens a file only once but how do I get past this issue and be able to write a file through out loop Also let me know if you have any other tips and comment for my code
use strict; use warnings; use diagnostics; use lib '/home/rahul/Documents/'; use script::filldataforlocator qw(clickfield send_keys_field); use script::readxmlcid qw(get_child_node_value); use script::getdata qw(connectdatabase); use XML::LibXML; my $module_used_file = $ARGV[1]; open(my $module_build_file_handle, '<', $module_used_file)or die "Coul +d not open file $module_used_file: $!"; my @module_file_lines = <$module_build_file_handle>; close $module_build_file_handle; my $dom = XML::LibXML->load_xml(location => $ARGV[0]); my $test_run_file_create = $dom->findvalue('//scenario/runname'); open(my $test_build_file_handle, '>', $test_run_file_create) or die "C +ould not open file '$test_run_file_create' $!"; foreach (@module_file_lines){ print $test_build_file_handle $_; } my @forms_required = ("createnewcid", "customerform"); my @child_node_values_fill; my $locatory_fields_id; my $locator_type; my $event_type; my $data_fill; my %hash_for_db_connect = ("companyname " => "contactnames", "contactn +ame" => "contactnames", "title" => "contactnames"); foreach my $config_forms (@forms_required) { @child_node_values_fill = get_child_node_value($config_forms); foreach (@child_node_values_fill) { ($locatory_fields_id, $locator_type, $event_type, $dat +a_fill) = split (/,/, $_); if (defined $locatory_fields_id && defined $locator_ty +pe && defined $event_type) { if ($event_type eq "click"){ print "My locator is $locatory_fields_id\n"; print $test_build_file_handle clickfield($loca +tory_fields_id, $locator_type)."\n"; print $test_build_file_handle "sleep(20)\;"."\ +n"; } elsif($event_type eq "send"){ if (defined $data_fill){ if (defined $hash_for_db_conne +ct{$data_fill}){ my $input_field_value += connectdatabase("$data_fill", "smartcidtest.$hash_for_db_connect{$d +ata_fill}"); print $test_build_file +_handle send_keys_field($locatory_fields_id, $locator_type, "$input_f +ield_value")."\n"; print $test_build_file +_handle "sleep(20)\;"."\n"; } } } } } } close $test_build_file_handle; chmod 0755, $test_run_file_create;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Writing a file from foreach loop
by Athanasius (Archbishop) on Sep 19, 2017 at 07:33 UTC | |
by rahulruns (Scribe) on Sep 20, 2017 at 04:07 UTC | |
by poj (Abbot) on Sep 20, 2017 at 06:37 UTC | |
|
Re: Writing a file from foreach loop
by Anonymous Monk on Sep 19, 2017 at 06:27 UTC | |
by rahulruns (Scribe) on Sep 20, 2017 at 04:02 UTC |