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;

In reply to Writing a file from foreach loop by rahulruns

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.