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

How come the following script:

#!/usr/bin/perl use strict; use warnings; if (defined $ARGV[0]){ open SOURCE, $ARGV[0] or die("$!"); } else{ print "please tell me a file to covert..." and die; } print '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE article PUBLIC +"-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbo +ok/xml/4.1.2/docbookx.dtd\">'; print "\n\n"; my $marker = 0; my $isheader = 0; my $isbreak = 0; my $paranumber; my $articlename; my $line; my $char; $char = getc(SOURCE); while ($char ne "\n"){ track($char); if (defined $articlename){ $articlename = "${articlename}$char"; } else{ $articlename = $char; } $char = getc(SOURCE); } $articlename = "${articlename}$char" if defined $articlename; print "<article> <title>$articlename</title>"; print "<chapter>"; getc(SOURCE); while($char = getc(SOURCE)){ if($char eq "\n" and $marker == 1 and $isbreak == 0){ track("found break"); writeXMLelement($line,$isheader); $line = $char; $isbreak = 1; $isheader = 0; $paranumber++; } elsif($char eq "\n" and $marker == 2 and $isheader == 0){ track("found header"); $isbreak = 0; $isheader = 1; } elsif($char eq "\n"){ $isbreak = 0; track("found newline"); $marker++; $line = "$line " unless (!defined $line); } elsif($char =~ /[\w\s\d\.\,\:\;\"\'\?\!\0]/){ $isbreak = 0; #track("found text"); $marker = 0; if (defined $line){ $line = "${line}$char"; } else{ $line = $char; } } else{ $isbreak = 0; track("text not found"); $marker++; $line = "$line " unless (!defined $line); } } sub writeXMLelement{ my $element = $_[0]; chomp $element; $element = reverse $element; chomp $element; $element = reverse $element; if ($_[1] == 0){ print "\n<para>\n$element\n</para>\n"; } elsif ($_[1] == 1){ print "</chapter>"; print "\n<chapter id=\"$element\">\n"; print "<title>$element</title>\n"; } } sub track{ warn "\ntrack:$_[0] at $paranumber\n"; } print "\n</chapter>"; print "\n</article>";

finishes when it reaches the following paragraph in the communist manifesto:

10. Free education for all children in public schools.

    Abolition of children's factory labour in its present form.

    Combination of education with industrial production, &c., &c.

this is not the only example of something which causes my script to baulk, and I'm stumped, anything to do with the reading process because I can only guess that my while loop isn't being satisfied

Replies are listed 'Best First'.
Re: Formatting The Communist Manifesto
by bart (Canon) on Apr 06, 2007 at 16:55 UTC
    while($char = getc(SOURCE)){
    Make this
    while(defined($char = getc(SOURCE))){
    As suggested, if $char encounters a "0", it'll stop.
Re: Formatting The Communist Manifesto
by Maze (Sexton) on Apr 06, 2007 at 16:46 UTC

    Actually, this could be to do with the finding of a '0' in the data, which could cause the while loop to fail, I think I remember perl treats characters of numbers as real numbers...

    either that or it's the getc, which may pose problems, apparently not a good idea to rely on it in perl

      either that or it's the getc, which may pose problems, apparently not a good idea to rely on it in perl
      Instead, you should stop trying to parse one character at a time. If you split on newlines, just fetch one line at a time.
      either that or it's the getc, which may pose problems, apparently not a good idea to rely on it in perl

      Indeed, I think in Perl, and I was not aware that Perl even had a getc function. With a name like that I'd lay odds ten to one that it just wraps the function of the same name in the C library, and if so, I don't want anything to do with it. Just thinking about the prospect makes me want to take a shower (as does looking at that code -- somebody apparently took six lines of nice clear legible Perl code and ran it through an extreme verbosification cycle).

Re: Formatting The Communist Manifesto
by talexb (Chancellor) on Apr 06, 2007 at 16:35 UTC

    Is this a problem with the &c in the data?

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds