Code Comments
Programming Forum and web based access to our favorite programming groups.This works:
foreach $line (@db){
if ($meth eq 'name') {
print $line;
}
}
But this does not
foreach $line (@db){
$meth = $line;
if ($meth eq 'name') {
print $line;
}
}
Why ?
Post Follow-up to this message"Martin Johansen" <martinfj@is.online.no> wrote:
> This works:
>
> foreach $line (@db){
> if ($meth eq 'name') {
> print $line;
> }
> }
What is @db and $meth?
> But this does not
>
> foreach $line (@db){
> $meth = $line;
> if ($meth eq 'name') {
> print $line;
> }
> }
What do you mean by "works" and "does not work"? What are you
getting? What did you expect to get? It's impossible to answer
your question without knowing what @db and $meth are.
Peter
--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1
/x;
Post Follow-up to this messageok sorry. I am programming a script for my site at www.portland.co.uk, my server has cgi support and perl support. But it has zero tolerance on the perl scripts. i.e. the slighest thing and it reports an internal server error and nothing more than that. Here is code that runs: <code> open(DB, "imaforumdb.txt"); @db = <DB>; close(DB); foreach $line (@db){ print $line; } </code> But this next code returns an internal server error and nothing more. <code> open(DB, "imaforumdb.txt"); @db = <DB>; close(DB); foreach $line (@db){ if($line eq 'name'){ print $line; } } </code>
Post Follow-up to this messageMartin Johansen wrote: > I am programming a script for my site at www.portland.co.uk, my > server has cgi support and perl support. > > But it has zero tolerance on the perl scripts. i.e. the slighest > thing and it reports an internal server error and nothing more than > that. > > Here is code that runs: > > open(DB, "imaforumdb.txt"); You should always, yes always, check the success of an open() and take proper action if it fails. > @db = <DB>; > close(DB); > > foreach $line (@db){ > print $line; > } > > But this next code returns an internal server error and nothing more. This is _NOT_ a Perl (or perl) error message. We can't help you with that one here. If you can tell us the real error message then we may be able to identify the reason for your problem. There are many ways: check your server logs (please don't ask here how to do that, ask in a NG that actually deals with your server), use the Carp module, etc. > open(DB, "imaforumdb.txt"); > @db = <DB>; > close(DB); > > foreach $line (@db){ > if($line eq 'name'){ > print $line; > } > } Now, this code, run from the command line, runs just fine. No error message at all. So you don't have a Perl error. However, it doesn't produce any output at all. My crystal ball tells me that maybe you intended to print the content of imaforumdb.txt (you didn't say if your problem was that the script didn't print the lines). In that case you forgot to chomp() the lines before comparing them using eq. If this is really the problem I don't know, because you didn't tell us enough details about what the script is supposed to do versus what it is actually doing. Please note, I said the script. We don't care about the web server and how it munges the script output! That you have to discuss in a newsgroup that actually deals with CGI programming and/or your web server. jue jue
Post Follow-up to this message"Martin Johansen" <martinfj@is.online.no> wrote:
> Here is code that runs:
>
> <code>
>
> open(DB, "imaforumdb.txt");
> @db = <DB>;
> close(DB);
>
> foreach $line (@db){
> print $line;
> }
>
> </code>
You should start off by using
use strict;
use warnings;
and always check the return value from open(), print(), and
close():
open(DB, "imaforumdb.txt")
or die "$0: imaforumdb.txt: can't open file for reading: $!";
my @db = <DB>;
close(DB)
or die "$0: imaforumdb.txt: can't close file: $!";
foreach my $line (@db){
print $line or die "$0: print failed: $!";
}
</code>
> But this next code returns an internal server error and nothing more.
>
> <code>
> open(DB, "imaforumdb.txt");
> @db = <DB>;
> close(DB);
>
> foreach $line (@db){
> if($line eq 'name'){
> print $line;
> }
> }
>
> </code>
Hm. I can't see anything wrong with the code. Run the code with
all warnings and error checks and see what you get.
Peter
--
#!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
# matlab comment stripper (strips comments from Matlab m-files)
s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1
/x;
Post Follow-up to this message"Peter J. Acklam" <pjacklam@online.no> wrote in message news:smg2l181.fsf@online.no... > > You should start off by using > > use strict; > use warnings; > And in this case: use CGI::Carp qw/fatalsToBrowser/; Then he'll be able to see what the server is complaining about. Matt
Post Follow-up to this messageMartin Johansen wrote:
> This works:
>
> foreach $line (@db){
> if ($meth eq 'name') {
> print $line;
> }
> }
If $meth is set to something other than 'name', that loop won't
print anything. (Some web servers don't do anything if a SSI
outputs nothing.)
> But this does not
>
> foreach $line (@db){
> $meth = $line;
> if ($meth eq 'name') {
> print $line;
> }
> }
>
> Why ?
Since you forgot to chomp($line) (or chomp($meth)), you can
expect that $meth="name\n" which is not eq to "name", therefore
nothing will be printed unless "name" is the last line of the
file and the file does not have "\n" on that last line.
-Joe
Post Follow-up to this messageOn 21 Mar 2004 15:32:46 +0100, Peter J. Acklam <pjacklam@online.no> wrote: > > open(DB, "imaforumdb.txt") > or die "$0: imaforumdb.txt: can't open file for reading: $!"; What's $0? I can't find it in the index of my Llama and perldoc, maybe I'm not up to speed with it but can't get it to tell me either. Thanks for your answer. -- Justin C, by the sea.
Post Follow-up to this messageJustin C wrote: > On 21 Mar 2004 15:32:46 +0100, Peter J. Acklam > <pjacklam@online.no> wrote: > > What's $0? I can't find it in the index of my Llama and perldoc, maybe You must have missed it in "perldoc perlvar" > I'm not up to speed with it but can't get it to tell me either. $PROGRAM_NAME $0 Contains the name of the program being executed. On some operating systems assigning to "$0" modifies the argument area that the ps program sees. This is more useful as a way of indicating the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.) Note for BSD users: setting "$0" does not completely remove "perl" from the ps(1) output. For example, setting "$0" to ""foobar"" will result in ""perl: foobar (perl)"". This is an operating system feature. jue
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.