http://www.perlmonks.org?node_id=107070

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

I tried to fix all the things you suggested but i still get errors that dont make sence to me like:
C:\Perl>perl -cw create2.pl [Wed Aug 22 11:10:17 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 1, at end of line [Wed Aug 22 11:10:17 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) [Wed Aug 22 11:10:17 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 2, at end of line [Wed Aug 22 11:10:17 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 11:10:17 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 3, at end of line [Wed Aug 22 11:10:17 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 11:10:17 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 4, at end of line [Wed Aug 22 11:10:17 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) Undefined subroutine &Carp::longmess called at C:\PERL\lib/Carp.pm lin +e 296.
Lines one to four look fine to me? here is the revised code.
#!/usr/bin/perl -w #use CGI::Carp('fatalsToBrowser'); use Carp; use diagnostics; use strict; use warnings; my @pages=(); my @xfilenames=(); my @filenames=(); my @xtitles=(); my @titles=(); my @xheadings=(); my @headings=(); my @xstuff=(); my $xstuff=""; my @stuff=(); my @xtemplate=(); my $xtemplate=""; my @template=(); open(PAGES, "<pages.txt") || die $!; @pages = PAGES; close(PAGES); foreach $_ (@pages) { if (m/^\*{4}filename.ext\*{4}/) { push(@xfilenames, $_) } elsif (m/^\*{4}title\*{4}/) { push(@xtitles, $_) } elsif (m/^\*{4}heading\*{4}/) { push(@xheadings, $_) } else { push(@xstuff, $_) } } foreach $_ (@xfilenames) { s/^\*{4}filename.ext\*{4}//; chomp ($_); push(@filenames, $_); } foreach $_ (@xtitles) { s/^\*{4}title\*{4}//; chomp ($_); push(@titles, $_); } foreach $_ (@xheadings) { s/^\*{4}heading\*{4}//; chomp ($_); push(@headings, $_); } $xstuff = join("", @xstuff); @stuff = $xstuff =~ /\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; open(TEMPLATE, "<template.html") || die $!; @xtemplate = <TEMPLATE>; close (TEMPLATE); $xtemplate = join("", @xtemplate); @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/sg; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/sg); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/sg); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/sg); for ($i=0; $i < (@filenames); $i++) { open(FILE, ">@filenames[$i]") || die $!; print FILE qq($temlate[0] $title[$i] $template[1] $heading[$i] $te +mplate[2] $stuff[$i] $template[3]); close(FILE); } print "Content-type:text/html\n\n"; print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>create</title> </head> <body> EndHTML foreach (@filenames) { print "$a <p>Name:$_</p>"; } print "<hr />"; foreach (@titles) { print "<p>Titles:$_</p>"; } print "<hr />"; foreach (@headings) { print "<p>Headings:$_</p>"; } print "<hr />"; foreach (@stuff) { print "<p>stuff:$_</p>"; } print <<EndHTML; </body> </html> EndHTML

Replies are listed 'Best First'.
Re: errors revisited
by damian1301 (Curate) on Aug 22, 2001 at 22:33 UTC
    damian1301 ends it here

    When run WITHOUT CGI::Carp, it gives clearer errors. This is a revised version of your script.

    #!/usr/bin/perl -w #use CGI::Carp('fatalsToBrowser'); use Carp; use diagnostics; use strict; use warnings; my @pages; my @xfilenames; my @filenames; my @xtitles; my @titles; my @xheadings; my @headings; my @xstuff; my $xstuff=""; my @stuff; my @xtemplate; my $xtemplate=""; my @template; open(PAGES, "<pages.txt") || die $!; @pages = <PAGES>; close(PAGES); foreach $_ (@pages) { if (m/^\*{4}filename.ext\*{4}/) { push(@xfilenames, $_) } elsif (m/^\*{4}title\*{4}/) { push(@xtitles, $_) } elsif (m/^\*{4}heading\*{4}/) { push(@xheadings, $_) } else { push(@xstuff, $_) } } foreach(@xfilenames) { s/^\*{4}filename.ext\*{4}//; chomp $_; push @filenames, $_; } foreach(@xtitles) { s/^\*{4}title\*{4}//; chomp $_; push @titles, $_; } foreach $_ (@xheadings) { s/^\*{4}heading\*{4}//; chomp $_; push @headings, $_; } $xstuff = join "", @xstuff; @stuff = $xstuff =~ /\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; open(TEMPLATE, "<template.html") || die $!; @xtemplate = <TEMPLATE>; close (TEMPLATE); $xtemplate = join"", @xtemplate; @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/sg; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/sg); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/sg); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/sg); for (my $i=0; $i < (@filenames); $i++) { open(FILE, ">$filenames[$i]") || die $!; print FILE qq($template[0] $titles[$i] $template[1] $headings[$i] +$template[2] $stuff[$i] $template[3]); close(FILE); } print "Content-type:text/html\n\n"; print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>create</title> </head> <body> EndHTML foreach (@filenames) { print " <p>Name:$_</p>"; } print "<hr />"; foreach (@titles) { print "<p>Titles:$_</p>"; } print "<hr />"; foreach (@headings) { print "<p>Headings:$_</p>"; } print "<hr />"; foreach (@stuff) { print "<p>stuff:$_</p>"; } print <<EndHTML; </body> </html> EndHTML
    NOTE that ALL errors had to do with not declaring your variables or misspeeling them. Be even more careful next time with your typing and spare us the time :).
    UPDATE: Thanks bikeNomad - I just caught that myself :)

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
      i ran your revised code it gave me these errors????
      C:\Perl>perl -cw create.pl [Wed Aug 22 11:46:32 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 1, at end of line [Wed Aug 22 11:46:32 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) [Wed Aug 22 11:46:32 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 2, at end of line [Wed Aug 22 11:46:32 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 11:46:32 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 3, at end of line [Wed Aug 22 11:46:32 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 11:46:32 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 4, at end of line [Wed Aug 22 11:46:32 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) Undefined subroutine &Carp::longmess called at C:\PERL\lib/Carp.pm lin +e 296.
        His revised code didn't use CGI::Carp, so it couldn't have given those errors. Did you try to run his program from the command line as it stands?

        Note that there was still an error that was caught by the warnings/diagnostics: on line 93, there is a reference to $a, but nothing sets it. You probably want:

        foreach my $a (@filenames) { print "$a <p>Name:$_</p>"; } print "<hr />";
Re: errors revisited
by Sifmole (Chaplain) on Aug 22, 2001 at 22:33 UTC
    I would look at this error first:
    Undefined subroutine &Carp::longmess called at C:\PERL\lib/Carp.pm lin +e 296.
    It seems that your Carp.pm is attempting to call two subroutines longmess and shortmess. There might be a probem with your installation of Carp.

    Update: The above was wrong

    Update:

    open(PAGES, "<pages.txt") || die $!; @pages = PAGES; close(PAGES);
    should be
    open(PAGES, "<pages.txt") || die $!; @pages = <PAGES>; close(PAGES);
    And then look at damian1301's response.
im trying its just pissed at me
by maddfisherman (Sexton) on Aug 22, 2001 at 23:34 UTC
    here is the revised code commenting out carp and cgi:carp
    #!/usr/bin/perl -w #use CGI::Carp('fatalsToBrowser'); #use Carp; use diagnostics; use strict; use warnings; my @pages; my @xfilenames; my @filenames; my @xtitles; my @titles; my @xheadings; my @headings; my @xstuff; my $xstuff=""; my @stuff; my @xtemplate; my $xtemplate=""; my @template; open(PAGES, "<pages.txt") || die $!; @pages = <PAGES>; close(PAGES); foreach $_ (@pages) { if (m/^\*{4}filename.ext\*{4}/) { push(@xfilenames, $_) } elsif (m/^\*{4}title\*{4}/) { push(@xtitles, $_) } elsif (m/^\*{4}heading\*{4}/) { push(@xheadings, $_) } else { push(@xstuff, $_) } } foreach $_ (@xfilenames) { s/^\*{4}filename.ext\*{4}//; chomp ($_); push(@filenames, $_); } foreach $_ (@xtitles) { s/^\*{4}title\*{4}//; chomp ($_); push(@titles, $_); } foreach $_ (@xheadings) { s/^\*{4}heading\*{4}//; chomp ($_); push(@headings, $_); } $xstuff = join("", @xstuff); @stuff = $xstuff =~ /\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; open(TEMPLATE, "<template.html") || die $!; @xtemplate = <TEMPLATE>; close (TEMPLATE); $xtemplate = join("", @xtemplate); @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/sg; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/sg); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/sg); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/sg); for (my $i=0; $i < (@filenames); $i++) { open(FILE, ">@filenames[$i]") || die $!; print FILE qq($temlate[0] $title[$i] $template[1] $heading[$i] $te +mplate[2] $stuff[$i] $template[3]); close(FILE); } print "Content-type:text/html\n\n"; print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>create</title> </head> <body> EndHTML foreach (@filenames) { print "<p>Name:$_</p>"; } print "<hr />"; foreach (@titles) { print "<p>Titles:$_</p>"; } print "<hr />"; foreach (@headings) { print "<p>Headings:$_</p>"; } print "<hr />"; foreach (@stuff) { print "<p>stuff:$_</p>"; } print <<EndHTML; </body> </html> EndHTML
    then i run it from the command line like this
    perl -cw create2.pl
    and it still returns these errors
    C:\Perl>perl -cw create2.pl [Wed Aug 22 12:30:50 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 1, at end of line [Wed Aug 22 12:30:50 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) [Wed Aug 22 12:30:50 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 2, at end of line [Wed Aug 22 12:30:50 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 12:30:50 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 3, at end of line [Wed Aug 22 12:30:50 2001] (eval 1): (Do you need to predeclare Car +p::shortme ss?) [Wed Aug 22 12:30:50 2001] (eval 1): Array found where operator expect +ed at (eva l 1) line 4, at end of line [Wed Aug 22 12:30:50 2001] (eval 1): (Do you need to predeclare Car +p::longmes s?) Undefined subroutine &Carp::longmess called at C:\PERL\lib/Carp.pm lin +e 296.
    what is wrong with lines 1-4 and why is it still giving me an error about carp, im not using it!!!!THANKS for your time and sorry im so slow at figuring this out
      *laughs* I think diagnostics brings in Carp by itself. Get rid of diagnostics and you should be better.

      Also, instead of foreach $_ (@filenames), do foreach (@filenames). The $_ is implicitly done for you.

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!

out of memory
by maddfisherman (Sexton) on Aug 23, 2001 at 00:08 UTC
    new script: now it gives error out of memory help?
    #!/usr/bin/perl -w #use CGI::Carp('fatalsToBrowser'); #use Carp; #use diagnostics; use strict; #use warnings; my @pages; my @xfilenames; my @filenames; my @xtitles; my @titles; my @xheadings; my @headings; my @xstuff; my $xstuff=""; my @stuff; my @xtemplate; my $xtemplate=""; my @template; open(PAGES, "<pages.txt") || die $!; @pages = <PAGES>; close(PAGES); foreach (@pages) { if (m/^\*{4}filename.ext\*{4}/) { push(@xfilenames, $_) } elsif (m/^\*{4}title\*{4}/) { push(@xtitles, $_) } elsif (m/^\*{4}heading\*{4}/) { push(@xheadings, $_) } else { push(@xstuff, $_) } } foreach (@xfilenames) { s/^\*{4}filename.ext\*{4}//; chomp ($_); push(@filenames, $_); } foreach (@xtitles) { s/^\*{4}title\*{4}//; chomp ($_); push(@titles, $_); } foreach (@xheadings) { s/^\*{4}heading\*{4}//; chomp ($_); push(@headings, $_); } $xstuff = join("", @xstuff); @stuff = $xstuff =~ /\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; open(TEMPLATE, "<template.html") || die $!; @xtemplate = <TEMPLATE>; close (TEMPLATE); $xtemplate = join("", @xtemplate); @template = $xtemplate =~ /^(.*?)\*{4}filename.ext\*{4}/sg; push (@template, $xtemplate =~ /\*{4}filename.ext\*{4}(.*?)\*{4}title\ +*{4}/sg); push (@template, $xtemplate =~ /\*{4}title\*{4}(.*?)\*{4}headings\*{4} +/sg); push (@template, $xtemplate =~ /\*{4}headings\*{4}(.*?)/sg); for (my $i=0; $i < (@filenames); $i++) { open(FILE, ">@filenames[$i]") || die $!; print FILE qq($temlate[0] $title[$i] $template[1] $heading[$i] $te +mplate[2] $stuff[$i] $template[3]); close(FILE); } print "Content-type:text/html\n\n"; print <<EndHTML; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transisional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html> <head> <title>create</title> </head> <body> EndHTML foreach (@filenames) { print "<p>Name:$_</p>"; } print "<hr />"; foreach (@titles) { print "<p>Titles:$_</p>"; } print "<hr />"; foreach (@headings) { print "<p>Headings:$_</p>"; } print "<hr />"; foreach (@stuff) { print "<p>stuff:$_</p>"; } print <<EndHTML; </body> </html> EndHTML
      For one thing, you can cut out a number of arrays by combining what you're doing. From a quick glance, you don't seem to be using @xfilenames (and the like) except as a temporary data structure. Instead, do something like:
      my @filenames; my @titles; my @headings; my @stuff; foreach (@pages) { chomp; if (/^\*{4}filename.ext\*{4}/) { s/^\*{4}filename.ext\*{4}//; push @filenames, $_; } elsif (m/^\*{4}title\*{4}/) { s/^\*{4}title\*{4}//; push @titles, $_; } elsif (m/^\*{4}heading\*{4}/) { s/^\*{4}heading\*{4}//; push @headings, $_; } else { s/\*{4}stuff\*{4}(.*?)\*{4}endstuff\*{4}/sg; push @xstuff, $_; } }
      You now have 4 less lists, thus cutting your memory usage for data structures almost in half.

      Note - don't declare all your variables up-front. That's a very C-like thing to do. Declare them when you need them, as you need them. Declaring a list or hash takes up considerable memory, as Perl will pre-declare like 50 (or so) elements that just sit there.

      ------
      /me wants to be the brightest bulb in the chandelier!

      Vote paco for President!