title: Where to open file in a Perl module
body: <p></p>
  
  <p>Below is a toy version of a module I'm writing. This example faithfully demonstrates the problem I having. </p>
  
  <p>I have a file <code>bed_in.txt</code></p>
  
  <pre class="lang-none prettyprint-override"><code>2L      0       4953
  2L      16204   16284
  2L      16612   16805
  2L      17086   18561
  2L      18757   18758
  2L      19040   19120
  2L      19445   19635
  2L      19894   21366
  2L      21582   21583
  2L      47501   52365
  2L      4698700 4709369
  </code></pre>
  
  <p>that I want to use to filter values in an array.</p>
  
  <p>I want to check whether the second column of <code>bed_in.txt</code> matches any value in</p>
  
  <pre class="lang-none prettyprint-override"><code>my @lines = qw/ 16204 40 200 149 19445 178 /;
  </code></pre>
  
  <p>push <em>this</em> to an array if so.</p>
  
  <p>My final output for this data should be the two elements in <code>@lines</code> that are also in the second column of <code>bed_in.txt</code></p>
  
  <pre class="lang-none prettyprint-override"><code>2L      0       4953
  2L      16204   16284 # 16204 == 16204
  2L      16612   16805
  2L      17086   18561
  2L      18757   18758
  2L      19040   19120
  2L      19445   19635 # 19445 == 19445
  </code></pre>
  
  <p>But where should I open my file?</p>
  
  <p>Should this happen in the script that calls my module <code>run_fileOpen.pl</code>, or in the module itself? </p>
  
  <p>In either case, I am not getting the correct output, and my module appears to be called for only the first element of <code>@lines</code></p>
  
  <h3>parseFile.pm</h3>
  
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl
  
  package parseFile;
  use strict;
  use warnings;
  
  use FindBin qw/ $Bin /;
  use lib File::Spec-&gt;catdir($FindBin::Bin, '..', 'bin/');
  
  use Data::Printer;
  
  open my $bed, '&lt;', 'bed_in.txt';
  
  sub check_lines {
    my ($line, $filter_ref) = @_;
    my @filter_reasons = @{ $filter_ref };
  
    my $lines_to_filter_ref = filter_lines($line, $bed, \@filter_reasons);
  
    return($lines_to_filter_ref);
  }
  
  
  sub filter_lines {
    my ($line, $bed_file, $lines_to_filter) = @_;
  
    my @filter = @{ $lines_to_filter };
  
    while(&lt;$bed_file&gt;){
      chomp;
      my $start = (split)[1];
      # print "line: $line  start: $start\n";
      if ($line == $start ) {
  
        push @filter, "Filter as equal: $start, $line [$_]";
      }
    }
    # p(@filter);
    return(\@filter);
  }
  
  
  1;
  </code></pre>
  
  <h3>run_parseFile.pl</h3>
  
  <pre class="lang-perl prettyprint-override"><code>#!/usr/bin/perl
  
  use strict;
  use warnings;
  
  use parseFile;
  
  
  my @lines = qw/ 16204 40 200 149 19445 178 /;
  
  foreach(@lines){
    my @filter_reasons = 'reason 1';
    my $lines2filter = parseFile::check_lines($_, \@filter_reasons);
    @filter_reasons = @{ $lines2filter };
    print "$_\n" foreach @filter_reasons;
  }
  </code></pre>
  
  <h3>Output</h3>
  
  <pre class="lang-none prettyprint-override"><code>reason 1
  Filter as equal: 16204, 16204 [2L   16204   16284]
  reason 1
  reason 1
  reason 1
  reason 1
  reason 1
  </code></pre>
  
  <p>When I add a <code>print</code> statement to the <code>while</code> loop in <code>filter_lines</code> I can see that only the first element is being run: </p>
  
  <pre class="lang-none prettyprint-override"><code>line: 16204  start: 0
  line: 16204  start: 16204
  line: 16204  start: 16612
  line: 16204  start: 17086
  line: 16204  start: 18757
  line: 16204  start: 19040
  line: 16204  start: 19445
  line: 16204  start: 19894
  line: 16204  start: 21582
  line: 16204  start: 47501
  line: 16204  start: 4698700
  </code></pre>
  
  <p>Is this an appropriate way to deal with opening my <code>bed_in.txt</code> file, or should I open it in the <code>run_parseFile.pl</code> script? Or is there something else entirely wrong with my code? </p>