in reply to Writing a file from foreach loop

Hello rahulruns,

Also let me know if you have any other tips and comment for my code

This line:

my %hash_for_db_connect = ("companyname " => "contactnames", "contactn +ame" => "contactnames", "title" => "contactnames");

looks like a potential bug to me: do you really want “companyname” followed by a space as a hash key? I’m assuming not, in which case you can avoid the problem by taking advantage of the fact that Perl’s “fat comma” operator => quotes its left operand (see perlop#Comma-Operator), allowing you to write:

my %hash_for_db_connect = (companyname => "contactnames", contactname +=> "contactnames", title => "contactnames");

It’s also a good idea to delay the declaration of variables until they are actually needed, to avoid unnecessary variables altogether, and to use statement modifiers where possible. So this:

... 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 (/,/, $_); ...

would be better written as the shorter and simpler:

... print $test_build_file_handle $_ for @module_file_lines; my %hash_for_db_connect = (companyname => 'contactnames', contactname +=> 'contactnames', title => 'contactnames'); for my $config_forms (qw( createnewcid customerform )) { for (get_child_node_value($config_forms)) { my ($locatory_fields_id, $locator_type, $event_type, $data_fil +l) = split /,/; ...

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Writing a file from foreach loop
by rahulruns (Scribe) on Sep 20, 2017 at 04:07 UTC

    Thank you for your review, it helps. But I still have question on entering data in a file in foreach loop

      Reduce your script down to the problem area and use Data::Dumper to debug it.

      #!/usr/bin/perl; use strict; use warnings; use Data::Dumper; use lib '/home/rahul/Documents/'; use script::readxmlcid qw(get_child_node_value); use XML::LibXML; my $dom = XML::LibXML->load_xml(location => $ARGV[0]); my @forms_required = ("createnewcid", "customerform"); foreach my $config_forms (@forms_required) { my @child_node_values_fill = get_child_node_value($config_forms); print Dumper $config_forms,\@child_node_values_fill; }
      poj