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

I have recently started running perl scripts under the ISAPI dll file and one of my scripts has stoped working. It works fine when I run it under the perl.exe but when I run it under the .dll it does not work. I am not given any error messages but instead of prtingin the content of a few txt files nothing gets printed. To see it working you can look at http://www.ffinfo.com/cgi-bin/headlines.pl and to see what the problem output is go to http://www.ffinfo.com/cgi-bin/headlines.plx Can anyone take a look at the code and tell me what might be wrong?
#!/user/bin/perl -w use strict; use CGI q~:standard~; my ($BasePath, $i, @NewsBoard, @NewsFile, $NewsPost, @News, $ID, @Mont +h, $Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDST, $Time +); @Month = ('January', 'February', 'March', 'April', 'May', 'June', 'Jul +y', 'August', 'September', 'October', 'November', 'December'); $BasePath = 'D:/finalfantasyinfo/community'; open (FILE, "$BasePath/boards/news.txt") or die 'Can not open new boar +d file'; @NewsBoard = <FILE>; close FILE; @NewsBoard = sort { $b <=> $a } @NewsBoard; $i = 0; while ($i < 4) { @NewsFile = split (/\|/, "$NewsBoard[$i]"); open (FILE, "$BasePath/messages/$NewsFile[0].txt") or die 'Can not + open News File'; $NewsPost = <FILE>; close FILE; @News = split (/\|/, "$NewsPost"); &ProcessHTML(); $i++; } sub ProcessHTML { $ID = $News[3]; ($Sec, $Min, $Hour, $MDay, $Mon, $Year, $WDay, $YDay, $IsDST) = lo +caltime($News[3]); if ($Min < 10) { $Min = qq~0$Min~; } if ($Hour > 12) { $Hour = $Hour -12; $Hour = qq~$Hour:$Min P.M.~; } else { $Hour = qq~$Hour:$Min A.M.~; } $Year = 1900 + $Year; $Time = qq~$Hour on $Month[$Mon] $MDay, $Year~; $News[8] =~ s/\[(\/)?(b|u|i)\]/<$1$2>/gi; $News[8] =~ s~\[center](.*?)\[/center]~<center>$1</center>~gi; $News[8] =~ s~\[url=(\S+?)\](.*?)\[/url\]~<a href="$1">$2</a>~gi; $News[8] =~ s~\[img](.*?)\[/img\]~<img src="$1" alt="" />~gi; $News[8] =~ s~\s*\[\*\]~</li><li>~isg; $News[8] =~ s~\[olist\]~<ol>~isg; $News[8] =~ s~\s*\[/olist\]~</li></ol>~isg; $News[8] =~ s~</li><ol>~<ol>~isg; $News[8] =~ s~<ol></li>~<ol>~isg; $News[8] =~ s~\[list\]~<ul>~isg; $News[8] =~ s~\s*\[/list\]~</li></ul>~isg; $News[8] =~ s~</li><ul>~<ul>~isg; $News[8] =~ s~<ul></li>~<ul>~isg; $News[8] =~ s~\[ch9679\]~&#9679\;~isg; print qq~<p class="center"><span class="header">$News[0]</span> <br />Posted by: $News[1] at $Time</p> <p>$News[8]</p> <p style="text-align: right"><a href="http://www.ffinfo.com/community/ +YaBB.pl?num=$ID">Discuss this posting.</a></p>~; }

Replies are listed 'Best First'.
Re: Scripts work under perl .exe but not ISAPI
by Anonymous Monk on Oct 23, 2009 at 19:26 UTC
    I don't know much about ISAPI, but based on the output, looks like you have nothing in your @News array. Apparently your script is not dying from failure to open the news file, but something strange is happening there. Can you add warn statements for debugging to see what is in $NewsPost after you attempt to read the file? I'm assuming Perl ISAPI write out error logs somewhere.
      Taking your idea to print out $NewsPost to see what was in it I have found that everything is going right UNTILL I call the sub &ProcessHTML(); It seams the array I use to hold the news is not auto-passed to the sub like in perl for CGI (.exe)

        This sounds very much like the "will not stay shared" warning that you can get when you take a CGI and use it in mod_perl. Does ISAPI.dll wrap the *.pl file into a subroutine like Apache::Registry does?

        The descriptions of Perl for ISAPI make it sound much like mod_perl / Apache::Registry but I could not find any cases of anybody actually documenting what it does or even mentioning "wrapping the script in a sub" much less noting the need to deal with file-scoped lexicals being used by subs in order to avoid "will not stay shared" problems (but I didn't spend a lot of time searching and scanning).

        The linked searches should find you a lot of information about this problem in case you are interested in understanding it better.

        If this is indeed the same problem, then the least-change way to fix your script would be to go from its current pattern:

        declare and initialize variables with static data code to be run subroutines that use above variables

        to a pattern like:

        BEGIN { declare and initialize variables with static data Main(); sub Main { code to be run } subroutines that use above variables }

        So you can probably fix your script by adding 5 lines of code ("BEGIN {", "Main();", "sub Main {", "}", and "}").

        But such a change will leave your script easy to break under mod_perl / PerlIS because it will be easy to stick something that isn't "declare and initialize variables with static data" into that first chunk of code. Which is why you might want to learn more about this particular problem and why you have to do these things to avoid it.

        This is just one of the reason why I still code in a style very much like the one I outlined long ago at (tye)Re: Stupid question (now updated with some justifications).

        - tye        

        It seams the array I use to hold the news is not auto-passed to the sub like in perl for CGI (.exe)

        It is never auto-passed, it is merely in scope. You should read Coping with Scoping

        Here is how you might rewrite that code
        #!/usr/bin/perl -- use strict; use warnings; { use CGI qw~ -nph :standard ~; my $BasePath = 'D:/finalfantasyinfo/community'; print header, start_html; MyNewsBoardApp::PrintSomeNews("$BasePath/boards/news.txt"); print end_html; exit; } package MyNewsBoardApp; use strict; use warnings; use POSIX; sub PrintSomeNews { my $NewsBoardFile = shift; my @NewsBoard = GetNewsBoard($NewsBoardFile); my $i = 0; while ( $i < 4 ) { print PostToHtml( GetNewsPost( $NewsBoard[$i][0] ) ); $i++; } } sub GetNewsBoard { use autodie qw' open close '; open my $NewsFh, shift; my @News = map { [ split /\|/, $_ ] } sort { $b <=> $a } <$NewsFh>; close $NewsFh; return @News; } sub GetNewsPost { use autodie qw' open close '; open my $NewsFh, shift; my $News = join '', $NewsFh; close $NewsFh; return split /\|/, $News; } sub PostToHtml { my (@News) = @_; my $ID = $News[3]; my $Time = POSIX::strftime( q!%H:%m %p on %B, %Y!, localtime( $New +s[3] ); $News[8] =~ s/\[(\/)?(b|u|i)\]/<$1$2>/gi; $News[8] =~ s~\[center](.*?)\[/center]~<center>$1</center>~gi; $News[8] =~ s~\[url=(\S+?)\](.*?)\[/url\]~<a href="$1">$2</a>~gi; $News[8] =~ s~\[img](.*?)\[/img\]~<img src="$1" alt="" />~gi; $News[8] =~ s~\s*\[\*\]~</li><li>~isg; $News[8] =~ s~\[olist\]~<ol>~isg; $News[8] =~ s~\s*\[/olist\]~</li></ol>~isg; $News[8] =~ s~</li><ol>~<ol>~isg; $News[8] =~ s~<ol></li>~<ol>~isg; $News[8] =~ s~\[list\]~<ul>~isg; $News[8] =~ s~\s*\[/list\]~</li></ul>~isg; $News[8] =~ s~</li><ul>~<ul>~isg; $News[8] =~ s~<ul></li>~<ul>~isg; $News[8] =~ s~\[ch9679\]~&#9679\;~isg; return qq~<p class="center"><span class="header">$News[0]</span> <br />Posted by: $News[1] at $Time</p> <p>$News[8]</p> <p style="text-align: right"><a href="http://www.ffinfo.com/community/YaBB.pl?num=$ID">Discuss this po +sting. </a></p>~; } __END__
        For debugging you should use print escapeHTML( Dumper( $var, \@var, \%var ) );

        See autodie, POSIX