Question: I'm reading a file and then running a for loop for each value. My script is stopping after executing the first value. The file\_detail.txt has multiple lines. But it is not proceeding to the next value. My script looks something like this --

```
    open (my $fh, "<", "$mycrdir/file_detail.txt" );
foreach(my $value = <$fh>) {
    chomp($value);
    if ($value eq "abc") {
        qx/"mkdir $mydir"/;
        func1($mycrnum,$mydir,$value,%envparams);
        func2($mycrnum,$mydir,$value,%envparams);
    }
    elsif($value eq "123") 
    {
        qx/"mkdir $mydir"/;
        func1($mycrnum,$mydir,$value,%envparams);
        func2($mycrnum,$mydir,$value,%envparams);
    }
}

```

Answer: 
Your code needs:

1. use warnings; use strict;
2. error handling on `open`
3. error handling on `qx`, i.e. replace it with `system` since you do not care about the output, or better yet, with `File::Path::mkpath()`
4. change the `foreach` to a `while`
5. add an `else` clause to your loop

Try this:

**Input**

```
123
456
abc
def

```

**Program**

```
#!/usr/bin/env perl

use strict;
use warnings;

use File::Path qw( make_path );

my ($mycrnum, $mydir, %envparams);

$mydir = 'bar';
my $file = "foo.txt";
open (my $fh, "<", "$file")
    or die "Unable to open '$file' : $!";

while(my $value = <$fh>) {
        chomp($value);
        print "Value is '$value'\n";
        if ($value eq "abc") {
            unless (-d $mydir) {
                make_path($mydir)
                    or die "Unable to mkdir '$mydir'";
            }
            func1($mycrnum,$mydir,$value,%envparams);
            func2($mycrnum,$mydir,$value,%envparams);
        }
        elsif($value eq "123")
        {
            unless (-d $mydir) {
                make_path($mydir)
                    or die "Unable to mkdir '$mydir'";
            }
            func1($mycrnum,$mydir,$value,%envparams);
            func2($mycrnum,$mydir,$value,%envparams);
        }
        else {
            print "Don't know what to do with line '$value'\n";
        }
}

sub func1 {
    print "You called func1\n";
}

sub func2 {
    print "You called func2\n";
}

```

**Output**

```
Value is '123'
You called func1
You called func2
Value is '456'
Don't know what to do with line '456'
Value is 'abc'
You called func1
You called func2
Value is 'def'
Don't know what to do with line 'def'

```
Either use while loop as Dave mentioned or slurp the file in an array then loop over array using foreach. 

```
use IO::All;

my @lines = io($filename)->slurp;

foreach (@lines){
    #work on each line
}

```