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

UPDATE::: I found that if I include a '\n' character to the print line, the matches are printed to screen. For some strange reason it will not print to screen without it.( or without the extra print line as pointed out in the original post below. ___________ P.S. I only have one of the blocks active in the code at a time, not both.

######## this one fails to print ####### if($templine =~ /(ABC)/) {print " xxx xx";} ######## when this one works ###### if($templine =~ /(ABC)/) {print " xxx \n xx";}
Why? Hello, I,m using perl 5.1 This first one finds the matches of ABC

?????????????????????????????????????????????????????? # This first one finds the matches of ABC ?????????????????????????????????????????????????????? foreach my $line (@_lines) { my $templine= $line; print "$templine"; # or print "$line"; if($templine =~ /(ABC)/){print "$1 = the line";} } #?????????????????????????????????????????????????????? # This next one doesn't find the matches of ABC #?????????????????????????????????????????????????????? foreach my $line (@_lines) { my $templine= $line; ############ print "$templine"; ############ # or print "$line"; if($templine =~ /(ABC)/){print "$1 = the line";} }

If printed to LOG, (e.g. print LOG $templine;), it fails as if I didn't print it at all. Why is the printing of the variable, or the variable it was just set equal to, making a difference. Thank You! Brian

Replies are listed 'Best First'.
Re: Printing the variable makes the next match code work. Why?
by GrandFather (Saint) on Jan 11, 2012 at 04:43 UTC

    Are you sure the first one works and the second doesn't? If I add strictures to the top of your script and supply @_lines the following error is generated:

    Global symbol "$_line" requires explicit package name at noname1.pl li +ne 10. Execution of noname1.pl aborted due to compilation errors.

    Line 10 is the line my $templine = $_line; in the first loop. If I remove strictures but add (1) and (2) as appropriate to the conditional print statements in the two loops I get:

    ABC = the line (2)

    so maybe all your problem is is that you aren't using strictures and you have an old fashioned "typo in a variable name" error? Oh, the test code I used was:

    #use strict; #use warnings; my @_lines = <<LINES; ABC in the first line but not the second LINES foreach my $line (@_lines) { my $templine = $_line; print "$templine"; # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (1)";} } foreach my $_line (@_lines) { my $templine = $_line; ############ print "$templine"; ############ # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (2)";} }

    Uncomment the strictures to get helpful errors and warnings.

    True laziness is hard work
      Thanks My Bad. I am sorry. use strict; is in my code I fixed the typos( all $_line should have said $line) in the code but you had already responded. I don't want to have to print to screen because then I can't easily see my output. Please help me again, even though I did not post well. :) Thanks

        Please note in your node any substantive changes that you make (changing the code is a substantive change).

        Actually once the variable name problem was fixed my earlier sample code wasn't so good. However the following revised code works as expected:

        use strict; use warnings; my @_lines = map {"$_\n"} split /\n/, <<LINES; ABC in the first line but not the second LINES foreach my $line (@_lines) { my $templine = $line; print "$templine"; # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (1)\n";} } foreach my $_line (@_lines) { my $templine = $_line; ############ print "$templine"; ############ # or print "$line"; if ($templine =~ /(ABC)/) {print "$1 = the line (2)\n";} }

        Prints:

        ABC in the first line ABC = the line (1) but not the second ABC = the line (2)

        If this is showing your failure mode then I don't understand what the problem is. If it doesn't show your failure mode then modify my sample code until it does and post the updated code along with the "bad" output.

        True laziness is hard work
Re: Printing the variable makes the next match code work. Why?
by Ratazong (Monsignor) on Jan 11, 2012 at 07:37 UTC

    If the print only works when it contains a \n, you might be suffering from output-buffering. See here in the Perl Cookbook for a detailed discussion of that topic.

    HTH, Rata
Re: Printing the variable makes the next match code work. Why?
by 3dbc (Monk) on Jan 11, 2012 at 04:53 UTC
Re: Printing the variable makes the next match code work. Why?
by Jenda (Abbot) on Jan 11, 2012 at 23:16 UTC

    Using perl 5.1? Surely you do not mean perl 5.001 released 1995-Mar-13.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.