ankit.tayal560 has asked for the wisdom of the Perl Monks concerning the following question:

use strict; #use warnings; use Data::Dumper; use XML::DOM; my $parser=new XML::DOM::Parser; my $doc = $parser->parsefile('C:\perl\perl_tests\AirbgICIndPer2.xml')o +r die$!; my $root=$doc->getDocumentElement(); print("Enter the number of flags which needs to be removed...\n\n"); my $num2=<STDIN>; if($num2>0) { for(my $p=0;$p<$num2;$p++) { print("type the signal id for which flags need to be removed..\n\n +"); my $sig_id=<STDIN>; print("type the flag name which needs to be removed according to t +he instructions\n\n"); print("State on flag remove : type StateOn\n"); print("Indication Function remove : type IndicationFunctions\n"); print("First value flags remove : type FirstValueFlags\n"); print("timeout notification flag remove : type TimeoutFlags\n\n"); my $flag_to_be_modified=<STDIN>; print("processing geny file data\n"); ClearSignalAttribute($sig_id,$flag_to_be_modified); } sub ClearSignalAttribute($$) { my $sigid=$_[0]; my $attribute=$_[1]; my $sigattributes; my $sig; my @attributes; my @signals; my $signal_id; $sig=$root->getElementsByTagName("signal")->item(0); $signal_id=$sig->getAttribute("sigid"); print("the input signal id is : $sigid\n"); print("the other signal id is : $signal_id\n"); if($sigid==$signal_id) { print("signal id matched\n"); if($sig->getElementsByTagName("attributes")) { $sigattributes=$sig->getElementsByTagName("attribu +tes")->item(0); @attributes=$sigattributes->getElementsByTagName(" +attribute"); print("got the attributes\n"); } foreach my $attr(@attributes) { my $a=$attr->getAttribute("name"); print("into the attribute array: $a\n"); my $c="VIlRx".$attribute; print("the target is : $c\n"); #if($attr->getAttribute("name") eq "VIlRx" +.$attribute) if($a eq $c) { print("attribute matched:$a\n"); if(my $temp1=$attr->getElementsByTagNa +me("item")->item(0)) { print("got the item element\n"); if(my $temp2=$temp1->getElementsBy +TagName("data")->item(0)) { print("got the data element\ +n"); if ($temp1->getAttribute("used +")=='1') { $b=$temp1->getAttribute("used" +); print("Attribute->used : $b\n" +); $temp1->setAttribute("used","0 +"); $temp1->setAttribute("order"," +-1"); $temp2->setAttribute("flags"," +4"); my $w=$temp1->getAttribute("us +ed"); my $x=$temp1->getAttribute("or +der"); my $y=$temp2->getAttribute("fl +ags"); print("used : $w\n"); print("order : $x\n"); print("flags : $y\n"); } } } } } } } #$doc->setXMLDecl($doc->createXMLDecl('1.0','UTF-8')); #$doc->printToFile("C:/perl/perl_tests/AirbgICIndPer2.xml"); } XML FILE : <?xml version="1.0" encoding="UTF-8"?> <ecuconfig id="1" name="ECU: IPC_LS" FrameworkVersion="1.4.48.0" Compa +tibleVersion="1.4.0.0"> <signal sigid="3464" id="3490"> <attributes> <creator name="Il_Vector_Gm"> <attribute name="Il_Vector_Gm_StateOn" typeid="3" value="1"/> </creator> <creator name="VectorIL"> <attribute name="VIlRxSigType" typeid="4" flags="4" set="0" value=""/> <attribute name="VIlRxSet" typeid="3" flags="1"/> <attribute name="VIlRxGet" typeid="3" flags="1"/> <attribute name="VIlRxRDS" typeid="3" flags="4" set="0" value="0"> <sharing flags="7" value="0"/> <textinfos> <textinfo fromid="3"> This attribute has been disabled by IL_Vector component.</textinfo> </textinfos> </attribute> <attribute name="VIlRxTimeoutTime" typeid="2" flags="4" set="0" value= +"0"/> <attribute name="VIlRxIndicationFlags" typeid="12" set="0"> <item index="0" order="-1" used="0"> <data typeid="4" flags="4" value="AirbgICIndPer"/> </item> </attribute> <attribute name="VIlRxIndicationFunctions" typeid="12" set="0"> <item index="0" order="0" used="1"> <data typeid="4" value="AirbgICIndPer"/> </item> </attribute> <attribute name="VIlRxFirstValueFlags" typeid="12" set="0"> <item index="0" order="0" used="1"> <data typeid="4" value="AirbgICIndPer"/> </item> </attribute> <attribute name="VIlRxDataChangedFlags" typeid="12" set="0"> <item index="0" order="-1" used="0"> <data typeid="4" flags="4" value="AirbgICIndPer"/> </item> </attribute> <attribute name="VIlRxTimeoutFlags" typeid="12" set="0"> <item index="0" order="0" used="1"> <data typeid="4" value="AirbgICIndPer"/> </item> <item index="1" order="1" used="1"> <data typeid="4" value="AirbgICIndPer_1"/> </item> </attribute> <attribute name="VIlRxTimeoutFunctions" typeid="12" set="0"> <item index="0" order="-1" used="0"> <data typeid="4" flags="4" value="AirbgICIndPer"/> </item> </attribute> <attribute name="VIlRxInitFunction" typeid="4" value=""/> <attribute name="VIlRxStartFunction" typeid="4" value=""/> <attribute name="VIlRxStopFunction" typeid="4" value=""/> <attribute name="VIlRxInitDefault" typeid="3" flags="1"/> <attribute name="VIlRxStartDefault" typeid="3" value="0"/> <attribute name="VIlRxStopDefault" typeid="3" value="0"/> <attribute name="VIlRxDefaultValue" typeid="4" flags="1"/> <attribute name="VIlRxTimeoutDefault" typeid="3" set="0" value="0"/> <attribute name="VIlRxTimeoutDefaultValue" typeid="4" flags="5"/> <attribute name="VIlRxTimeoutDynamicApi" typeid="3" flags="2" set="0" +value="0"> <sharing flags="7" value="0"/> </attribute> <attribute name="VIlRxRouting" typeid="3" flags="1"/> <attribute name="VIlRxTimeoutSupervisor" typeid="6" flags="4" orefid=" +1306"/> </creator> </attributes> <sources> <source srcid="19264"/> </sources> </signal> </ecuconfig>

I've written the above script to modify the various attributes of "item" and "data" element. when I try to run this script it is not showing any error but not performing the required actions. it is not going inside the loop "if($a eq $c)" and simply printing all the names ignoring this loop . any suggestions about what is the bug in this script and how I can handle it?:

Replies are listed 'Best First'.
Re: How to Debug my perl script? (updated)
by haukex (Archbishop) on Sep 28, 2016 at 10:55 UTC

    Hi ankit.tayal560,

    Please see the Basic debugging checklist - particularly item Nr. 3 on that list (unexpected whitespace) will solve your problem.

    I'd also suggest you limit the scope of your my declarations, don't declare them until the scope where you need them. In particular, look at @attributes: It can be declared just before if ( $sig->getElementsByTagName("attributes") ). In this case it's not a problem, but if you were to place that chunk of code into a loop, @attributes would not get cleared. Only declaring your lexical (my) variables until the scope where they are needed is a very good habit to get into.

    Update: Another important debugging technique is reducing your code to as little as necessary to reproduce the problem. This helps you in narrowing down where the problem is occurring, and it helps us in having less code to wade through. See also Short, Self Contained, Correct (Compatible) Example.

    Also: Don't turn off warnings, they can be very useful!

    Hope this helps,
    -- Hauke D

      worked! thanks a lot