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

I have a problem with my code that when I comment out the else statement it works, but when I try to use the else statement the code does not work properly. Sorry if I ask to many questions.. Do I have to create an array out of the input file to make this work properly?
#!/usr/bin/perl -- use strict; use warnings; my $input = <STDIN>; chomp $input; while (<DATA>) { if ((/#bg\s$input/ .. /#be\s$input/) =~ /^\d+(?<!^1)$/) { print $_; } elsif (/bb\s$input/) { print $_; last; } # else # { # print "nope".$_; # last; # } } __DATA__ #bg at,super spaz razz tazz kazz #be at #bg as,clock brock spock mock sock #be as #bf am,lost fluff muff tuff cuff #be am bb lionsclub

Replies are listed 'Best First'.
Re: troubleshooting filehandle
by JavaFan (Canon) on Oct 17, 2008 at 21:02 UTC
    "Does not work properly", does that mean
    • It sits on the couch eating chocolates.
    • You get a syntax error.
    • It called in sick.
    • It unexpectedly dies.
    • It never terminates.
    • It pretends to be a squirrel, stealing your nuts.
    • It terminates sooner than you expect.
    • It prints something you didn't expect.
    • It didn't print something you expect.
    • It's more interested in the cute little blond program in /usr/local.
    • Something else?
      Well what happens is that it totally ignores the if and elsif statements and goes straight to the else statement as you can see if you run it without the else section commented out, which is not the desired result.

        And are you sure the very first line in DATA matches one of the condition? 'Cause if it doesn't Perl tries the first one, fails, tries the second one, fails, prints 'nope' and the content of the line and EXITS THE LOOP.