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

In the script below, the variable $fline prints expected data before the if statement $x=-/go/ but not after (it seems that the 'if then' statement erases the $fline variable).

I can simply delete the if statement that contains $x=-/go/ then the $fline variable is then ok and unchanged and prints the expected data in both print statements. In fact, several different 'tests' (such as index) instead of the if statement cause the same problem.

What the heck is the problem with this screwy perl language???? Perl is driving me crazy!!! Why does putting in some type of 'if then' statement erase a variable???? Am I just an idiot???? Am I not seeing something obvious???? How do I stop it????

#!/usr/local/bin/perl -w print "Content-type:text/html\n\n"; #Content Header open(TOP, "first.htm"); # Open db for reading and display @farray=<TOP>; close (TOP); open(BOTTOM, "second.htm"); # Open db for reading and display @sarray=<BOTTOM>; close (BOTTOM); foreach $sline(@sarray){ chomp($sline); print "second $sline \n"; if (index ($sline, "pattern") > "-1") { foreach $fline(@farray){ chomp($fline); print "first $fline \n"; if ($fline =- /go/) {print "hello";} print "first $fline \n"; } } }

Edited by footpad, ~ Mon Aug 26 02:11:50 2002 (UTC) : Added <CODE> and other HTML formatting tags.

Replies are listed 'Best First'.
Re: Variable Data Lost
by elusion (Curate) on Aug 26, 2002 at 02:15 UTC
    You were right that your problem was with $fline =- /go/. I think you want $fline =~ /go/. Notice the ~ instead of -. You were saying set fline equal to negative /go/.

    elusion : http://matt.diephouse.com

Re: Variable Data Lost
by BrowserUk (Patriarch) on Aug 26, 2002 at 02:24 UTC

    As elusion just pointed out to me, you do have the -w in your code, I'll shut up (though if you had run this from the command line you would have seen the error, and there is no reason why you can't run a CGI script from the command line for testing).

    You could also take a look at using use CGI::Carp qw(fatalsToBrowser); which might help next time.

    In addition to what elusion has already said, if you had had use warnings; at the top of your code or -w on your shebang (#!) line, you would have got the error message:

    Reversed -= operator at C:\test\192744.pl line 24. telling you exactly the problem.


    What's this about a "crooked mitre"? I'm good at woodwork!
Re: Variable Data Lost
by Cody Pendant (Prior) on Aug 26, 2002 at 02:48 UTC
    Also, what on earth is this doing:
    if (index ($sline, "pattern") > "-1")
    the minus one shouldn't be in quotes, and you don't need that clunky JavaScript sytax, just use a regex like you do a couple of lines later.
    --
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
      As far as the minus one being in quotes...

      It doesn't really matter, because in perl, strings and numbers and numbers are strings. So while no quotes would be preferred, with quotes certainly works.

      I would have to agree with your suggestion to use a regex, though.

      elusion : http://matt.diephouse.com

        I think I was getting confused with JavaScript syntax myself there. Quotes will force a number to be a string in JS. But the day it's a variable, not just a constant, in those quotes, it will cause huge problems, won't it?
        --
        ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;