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

#!/usr/bin/perl print qq|<?xml version="1.0"?>|; print "\n"; while($_=<DATA>){ my $save; if (/^\{FILE\}/ .. /^\{NUMBER\}/) { unless (/^\{(FILE|NUMBER)\}/) { my $filename = $_; print "The filename is $filename\n"; } } open FH,">$filename.xml"; print FH '<?xml version="1.0"?>'; print FH "\n"; $_ =~ s/[\cA-\cZ]//g; if ( /^{(\w+)}/ ... /^\w+/ ) { my $tag = $1 if $1; if( ! /^{/ ){ print FH "<$tag>$_</$tag>\n"; # print FH "<\$tag\>$_\</$tag\>\n"; } } } close FH; __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author staff {FILE} sourcetag2 {NUMBER} 00001 {SOURCE} sourcenam {KEYWORD} {AUTHOR} author1 staff1
The contents from {FILE} should be the filename.
Here I have used the filename as sourcetag1.
But file contains '?' in the filename and contents of
print FH "<$tag>$_</$tag>\n"; is not written to a file. The content should write to the file until another {FILE} is read.
When {FILE} is read the file should be closed and another file should be opened with the value as sourcetag2
Can you please tell me why the contents are written to the file.

Replies are listed 'Best First'.
Re: Writing to a file
by cdarke (Prior) on Aug 13, 2009 at 11:47 UTC
    use strict; use warnings;
    $filename is being created in an inner lexical scope. Create it in the same scope as the open.

    Always test the result of the open, something like this:
    open FH,'>',"$filename.xml" or die "Unable to open $filename.xml: $!";
      The files are not been created. How to open a file as the filename for the value inside {FILE}. When it finds another {FILE} the previous file name should be closed and another file for the {FILE} should be opened. Please tell me how to do it
        Please tell us the name of the format?
Re: Writing to a file
by alexm (Chaplain) on Aug 13, 2009 at 15:06 UTC
    But file contains '?' in the filename

    $filename contains a \n that you should chomp.

Re: Writing to a file
by Anonymous Monk on Aug 13, 2009 at 11:46 UTC
    What is the name of this format?

    Here, i perltidy'd your code for you

    #!/usr/bin/perl print qq|<?xml version="1.0"?>|; print "\n"; while ( $_ = <DATA> ) { my $save; if ( /^\{FILE\}/ .. /^\{NUMBER\}/ ) { unless (/^\{(FILE|NUMBER)\}/) { my $filename = $_; print "The filename is $filename\n"; } } open FH, ">$filename.xml"; print FH '<?xml version="1.0"?>'; print FH "\n"; $_ =~ s/[\cA-\cZ]//g; if ( /^{(\w+)}/ ... /^\w+/ ) { my $tag = $1 if $1; if ( !/^{/ ) { print FH "<$tag>$_</$tag>\n"; # print FH "<\$tag\>$_\</$tag\>\n"; } } } close FH; __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author staff {FILE} sourcetag2 {NUMBER} 00001 {SOURCE} sourcenam {KEYWORD} {AUTHOR} author1 staff1
Re: Writing to a file
by alexm (Chaplain) on Aug 13, 2009 at 16:09 UTC

    I don't think that the bistable op is the best way to solve this kind of problem, as you can see from the responses on this node and on convert to XML.

Re: Writing to a file
by ig (Vicar) on Aug 13, 2009 at 18:33 UTC

    Perhaps the following will give you some ideas you can use:

    use strict; use warnings; my $fh; my $tag; while(<DATA>){ chomp; if(/^{(.*)}/) { $tag = $1; } else { if($tag eq 'FILE') { my $filename = $_; print "The filename is $filename\n"; open($fh, '>', $filename) or die "$filename: $!"; print $fh '<?xml version="1.0"?>',"\n"; } elsif(defined($fh)) { print $fh "<$tag>$_</$tag>\n"; } } } close($fh); exit(0); __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author staff {FILE} sourcetag2 {NUMBER} 00001 {SOURCE} sourcenam {KEYWORD} {AUTHOR} author1 staff1
      #!/usr/bin/perl use strict; use warnings; my $fh; my $tag; while(<DATA>){ chomp $_; $_ =~ s/[\cA-\cZ]//g; #$_ =~ s/^\s+$//g; #$_ =~ s/\n//g; #$_=~ s/[\r]//gs; if(/^{(.*)}/) { $tag = $1; print $tag."\n";; } else { if($tag eq 'FILE') { my $filename = $_; # print "The filename is $filename\n"; open($fh, '>', "$filename.xml") or die "$filename: $!"; print $fh '<?xml version="1.0"?>',"\n"; print $fh "<root>\n"; print $fh "<FILE>$filename</FILE>"; } elsif(defined($fh)) { if( $_ ne ''){ #### Remove the blnak lines chomp $_; print $fh "<$tag>$_</$tag>\n"; } #print $fh "</root>"; } } } close($fh); exit(0); __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2
      Please can you tell me why the output in sourcetag1.xml is displayed as below:
      <?xml version="1.0"?> <root> <FILE>sourcetag1</FILE><NUMBER>00000</NUMBER> <SOURCE>source1</SOURCE> <AUTHOR>author1</AUTHOR> <AUTHOR>staff1</AUTHOR> <HEADLINE>DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\</HEADLINE> <HEADLINE>STYLE AT A SPEED</HEADLINE> <HEADLINE>USUALLY ASSOCIATED WITH WARDROBE ITEMS.</HEADLINE>
      The <HEADLINE> is repeated 3 times
        Please can you tell us the name of the format?

        In order to understand why the output to sourcetag1.xml is the way that it is - why the <HEADLINE> tag is repeated three times - you must carefully read and understand the program and follow what it does, step by step, to produce the output.

        Here is a version of your program with some extra print statements. If you study the output carefully you should be able to follow the execution of the program. This will show you why the <HEADLINE> tag surrounds each of the last three lines.

        use strict; use warnings; my $fh; my $tag; while(<DATA>){ print "\nNext iteration of while loop\n"; print "\$tag = $tag\n"; print "\$_ = $_"; chomp $_; $_ =~ s/[\cA-\cZ]//g; #$_ =~ s/^\s+$//g; #$_ =~ s/\n//g; #$_=~ s/[\r]//gs; if(/^{(.*)}/) { print "\tThis line is a tag line\n"; $tag = $1; print "\t\$tag has been set to $tag\n";; } else { print "\tThis line is not a tag line.\n"; if($tag eq 'FILE') { print "\t\t\$tag is equal to 'FILE', so this line gives a +new filename\n"; my $filename = $_; # print "The filename is $filename\n"; open($fh, '>', "$filename.xml") or die "$filename: $!"; print $fh '<?xml version="1.0"?>',"\n"; print $fh "<root>\n"; print $fh "<FILE>$filename</FILE>"; print "\t\tAny previously open output file has been closed +\n"; print "\t\tand new output file $filename has been opened.\ +n"; print "\t\tand some initial text has been written to it.\n +"; } elsif(defined($fh)) { print "\t\t\$tag is not equal to 'FILE' and we have an ope +n output file.\n"; if( $_ ne ''){ #### Remove the blnak lines print "\t\t\tand the line is not blank\n"; print "\t\t\tso, write it to the output file\n"; print "\t\t\tas '<$tag>$_</$tag>'\n"; chomp $_; print $fh "<$tag>$_</$tag>\n"; } #print $fh "</root>"; } } } close($fh); exit(0); __DATA__ ^B^B^B^B^B^B {FILE} sourcetag1 {NUMBER} 00000 {SOURCE} source1 {KEYWORD} {AUTHOR} author1 staff1 {HEADLINE} DISPOSABLE DECOR: THE CUTTING EDGE DULLS FAST\ STYLE AT A SPEED USUALLY ASSOCIATED WITH WARDROBE ITEMS. {FILE} sourcetag2 {NUMBER} 00002 {SOURCE} sourcenam2 {KEYWORD} {AUTHOR} author2 staff2

        An alternative to adding print statements to you program is to use the debugger (see perldebug). With the debugger you can execute your program one statement at a time, examine and even change variables as it executes. In the beginning it may be easier to use print statements, but the debugger is a very powerful tool that can be very easy and helpful if you take the time to learn how to use it.