charan1717 has asked for the wisdom of the Perl Monks concerning the following question:

I am new to perl & below is my problem statement: 1.Need to create an xml file (parent($a)/child($b) in code) taking user input.Need to check if number of lines exceeds 4000.If so, close the file checking if proper parent -child are created ,close the xml file with proper tag & write the content in new file until $a times.Below is my code ========================================================================================================================
#!/usr/bin/perl use strict; use Fcntl; use File::Basename; print "Deploy adaptations (y/n)?\n"; $s=<STDIN>; chomp ($s); if ($s eq 'y'){ print "Please enter apadtation path\n"; $path=<>; chomp($path); @files = <$path/*.zip>; foreach $file (@files) { $base=basename($file); print "$base\n"; } } my @user; do{ print "Select adaptations to be deployed\n"; $userinput = <STDIN>; chomp($userinput); push(@user ,"$userinput"); print "Select more adaptations ? (y/n)\n"; $result=<STDIN>; chomp ($result); }while ($result eq 'y'); #print "@user\n"; foreach (@user){ print "Hello\n"; } print "Enter number of objects to be created\n"; $a =<STDIN>; chomp ($a); print "Enter the name of Managed object\n"; $m =<STDIN>; chomp ($m); print "Do you want child objects to be created?(y/n)\n"; $r=<STDIN>; chomp ($r); if ($r eq 'y') { print "Enter number of child objects present under par +ent DN\n"; $b =<STDIN>;#children hiearchy chomp ($b); } my ($ext)=0; #$file_count=0; while ($a >=1) { sysopen (FILE,"/opt/robot/testcases/gep/tools/sample_$file_count++.xml +", O_RDWR|O_EXCL|O_CREAT,0755); printf FILE qq(<?xml version="1.0" encoding="UTF-8"?>\n); printf FILE qq(<!--DOCTYPE raml SYSTEM "ocos20.dtd"-->\n); printf FILE qq (<ocos version="2.0" xmlns:xsi="http://www.w3.org/2001/ +XMLSchema-instance" xsi:noNamespaceSchemaLocation='ocos20.xsd'>\n); printf FILE qq (<!--ocos version="2.0" xmlns="ocos20.xsd"-->\n); printf FILE qq(<cmData type="actual" scope="changes">\n); #printf FILE qq (<managedObject class="PLMN" version="1" operation="cr +eate" distName="PLMN-$a"/>\n); #printf FILE qq (</managedObject>\n); $linecount=0; do { if ($b==1) { printf FILE qq (<managedObject class="$m +" version="1" operation="create" distName="$m-$a"/>\n); printf FILE qq (<managedObject class="PL +MN" version="1" operation="create" distName="PLMN-$a"/>\n); printf FILE qq (</managedObject>\n); printf FILE qq (<managedObject class="GPBB" version= +"1" operation="create" distName="$m-$m/GPBB-$a">\n); printf FILE qq (</managedObject>\n); } elsif($b==2){ printf FILE qq (<mana +gedObject class="$m" version="1" operation="create" distName="$m-$a"/ +>\n); printf FILE qq (<managedObject class="GPBB +" version="1" operation="create" distName="$m-$a/GPBB-$a">\n); printf FILE qq (</managedObject>\n); printf FILE qq (<managedObject class="GPST +" version="1" operation="create" distName="$m-$a/GPBB-$a/GPST-$a">\n) +; printf FILE qq (</managedObject>\n); } elsif($b==3){ +printf FILE qq (<managedObject class="$m" version="1" operation="crea +te" distName="$m-$a"/>\n); printf FILE qq (<managedObject + class="GPBB" version="1" operation="create" distName="$m-$a/GPBB-$a" +>\n); printf FILE qq (</managedObjec +t>\n); printf FILE qq (<managedObject + class="GPST" version="1" operation="create" distName="$m-$a/GPBB-$a/ +GPST-$a">\n); printf FILE qq (</managedObjec +t>\n); printf FILE qq (<managedObject + class="GGSN" version="6.0" operation="create" distName="$m-$a/GPBB-$ +a/GPST-$a/GGSN-$a">\n); printf FILE qq (</managedObjec +t>\n); } $linecount= system ("wc -l < sample_$file_count++.xml"); }while ($linecount <=100); printf FILE qq (</cmData>\n); printf FILE qq (</ocos>\n); close (FILE); $a--; }

Replies are listed 'Best First'.
Re: open a new xml file if line count exceeds 4000 lines
by NetWallah (Canon) on May 29, 2012 at 06:06 UTC
    Your code does not compile under "use strict;" (which is part of your code).

    What was your question again ?

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

Re: open a new xml file if line count exceeds 4000 lines
by bitingduck (Deacon) on May 29, 2012 at 06:41 UTC

    thanks (note: you can also edit the original post to the same effect and spare people's eyes on the first look

    re the code: use an XML module out of CPAN-- XML::Writer is a good simple one, or XML::Simple. I started posting here with a problem where I was generating XML with print statements and concatenation and the XML reader would crash on what I thought was just copies of what was coming in. "Use an already debugged module to generate XML" was some of the best advice I've gotten here and it will save you a lot of headaches downstream.

Re: open a new xml file if line count exceeds 4000 lines
by bitingduck (Deacon) on May 29, 2012 at 05:20 UTC

    ouch.

    You need to go back and wrap that in <code> tags and repaste it so it's readable

Re: open a new xml file if line count exceeds 4000 lines
by charan1717 (Initiate) on May 29, 2012 at 06:03 UTC
    #!/usr/bin/perl use strict; use Fcntl; use File::Basename; print "Deploy adaptations (y/n)?\n"; $s=<STDIN>; chomp ($s); if ($s eq 'y'){ print "Please enter apadtation path\n"; $path=<>; chomp($path); @files = <$path/*.zip>; foreach $file (@files) { $base=basename($file); print "$base\n"; } } my @user; do{ print "Select adaptations to be deployed\n"; $userinput = <STDIN>; chomp($userinput); push(@user ,"$userinput"); print "Select more adaptations ? (y/n)\n"; $result=<STDIN>; chomp ($result); }while ($result eq 'y'); #print "@user\n"; foreach (@user){ print "Hello\n"; } print "Enter number of objects to be created\n"; $a =<STDIN>; chomp ($a); print "Enter the name of Managed object\n"; $m =<STDIN>; chomp ($m); print "Do you want child objects to be created?(y/n)\n"; $r=<STDIN>; chomp ($r); if ($r eq 'y') { print "Enter number of child objects present under par +ent DN\n"; $b =<STDIN>;#children hiearchy chomp ($b); } my ($ext)=0; #$file_count=0; while ($a >=1) { sysopen (FILE,"/opt/robot/testcases/gep/tools/sample_$file_count++.xml +", O_RDWR|O_EXCL|O_CREAT,0755); printf FILE qq(<?xml version="1.0" encoding="UTF-8"?>\n); printf FILE qq(<!--DOCTYPE raml SYSTEM "ocos20.dtd"-->\n); printf FILE qq (<ocos version="2.0" xmlns:xsi="http://www.w3.org/2001/ +XMLSchema-instance" xsi:noNamespaceSchemaLocation='ocos20.xsd'>\n); printf FILE qq (<!--ocos version="2.0" xmlns="ocos20.xsd"-->\n); printf FILE qq(<cmData type="actual" scope="changes">\n); #printf FILE qq (<managedObject class="PLMN" version="1" operation="cr +eate" distName="PLMN-$a"/>\n); #printf FILE qq (</managedObject>\n); $linecount=0; do { if ($b==1) { printf FILE qq (<managedObject class="$m +" version="1" operation="create" distName="$m-$a"/>\n); printf FILE qq (<managedObject class="PL +MN" version="1" operation="create" distName="PLMN-$a"/>\n); printf FILE qq (</managedObject>\n); printf FILE qq (<managedObject class="GPBB" version= +"1" operation="create" distName="$m-$m/GPBB-$a">\n); printf FILE qq (</managedObject>\n); } elsif($b==2){ printf FILE qq (<mana +gedObject class="$m" version="1" operation="create" distName="$m-$a"/ +>\n); printf FILE qq (<managedObject class="GPBB +" version="1" operation="create" distName="$m-$a/GPBB-$a">\n); printf FILE qq (</managedObject>\n); printf FILE qq (<managedObject class="GPST +" version="1" operation="create" distName="$m-$a/GPBB-$a/GPST-$a">\n) +; printf FILE qq (</managedObject>\n); } elsif($b==3){ +printf FILE qq (<managedObject class="$m" version="1" operation="crea +te" distName="$m-$a"/>\n); printf FILE qq (<managedObject + class="GPBB" version="1" operation="create" distName="$m-$a/GPBB-$a" +>\n); printf FILE qq (</managedObjec +t>\n); printf FILE qq (<managedObject + class="GPST" version="1" operation="create" distName="$m-$a/GPBB-$a/ +GPST-$a">\n); printf FILE qq (</managedObjec +t>\n); printf FILE qq (<managedObject + class="GGSN" version="6.0" operation="create" distName="$m-$a/GPBB-$ +a/GPST-$a/GGSN-$a">\n); printf FILE qq (</managedObjec +t>\n); } $linecount= system ("wc -l < sample_$file_count++.xml"); }while ($linecount <=100); printf FILE qq (</cmData>\n); printf FILE qq (</ocos>\n); close (FILE); $a--; }

      :) For starters

      You should replace all printf calls (each one of those is an error) with print

      You should also replace each instance of print/reading from STDIN with a prompt function call, as in

      my $value = prompt($message);

      Whereas prompt resembles the prompt from here

      Actually, you should switch from using STDIN to using @ARGV, its much more user friendly

      Running the code through perltidy couldn't hurt either

Re: open a new xml file if line count exceeds 4000 lines
by charan1717 (Initiate) on May 29, 2012 at 06:53 UTC
    thanks for suggestion ! i shall try to put my queries in more matured fashion :)For now am unable to think logic on file spliting /writing into a new file once it exceeds sum count rather number of lines !Your help is greatly appreciated !

      For now am unable to think logic on file spliting /writing into a new file once it exceeds sum count rather number of lines !Your help is greatly appreciated !

      Its hard to devise new logic when the logic you already have is hard to determine.

      It is easier to see the current logic once you cleanup the code. Then you can work on changing the logic because its easier to see what is missing.

      If you want something new, here you go

      while( $xfactor > 0 ){ my $newfile = sprintf 'new-x-%d-%d', $xfactor, $nimblefoot; MahFourThousand( $newfile, $xfactor, $nimblefoot ); $xfactor--; } sub MahFourThousand { my $counter = 0; while( $counter < 4000 ){ ## print one line $counter++; ## print 10 lines $counter += 10; ... } }

      That is how I'd start changing your program, but I'm not 100% sure what you're after.

      If you go ahead and make the changes we suggest, we'll help you make the next set of changes.

Re: open a new xml file if line count exceeds 4000 lines
by charan1717 (Initiate) on May 29, 2012 at 06:16 UTC
    Need to create an xml file (parent($a)/child($b) in code) taking user input.Need to check if number of lines exceeds 4000.If so, close the file checking if proper parent -child are created ,close the xml file with proper tag & write the content in new file until $a times.You are right ! for now you can comment out strict!
      Don't even think about commenting out use strict;

      strict will help you catch your mistakes. Without it, you're back to debugging by print statements (if you're lucky) and your Mark I eyeballs, if you're not.

        Debugging exclusively with print is so 1980s, doesn't everyone use a step through debugger today?