1

I cannot figure out why the output complains that $count is not initialized, and what should I do? Here is my code:

#!/usr/bin/perl -w
# Author: Thomas Thiel
# Program name: primer3_in.pl
# Description: creates a PRIMER3 input file based on SSR search results

open (IN,"<$ARGV[0]") || die ("\nError: Couldn't open misa.pl results file (*.misa) !\n\n");

my $filename = $ARGV[0];
$filename =~ s/\.misa//;
open (SRC,"<$filename") || die ("\nError: Couldn't open source file containing original FASTA sequences !\n\n");
open (OUT,">$filename.p3in");

undef $/;
$in = <IN>;
study $in;

$/= ">";

my $count;
while (<SRC>)
  {
  next unless (my ($id,$seq) = /(.*?)\n(.*)/s);
  $seq =~ s/[\d\s>]//g;#remove digits, spaces, line breaks,...
  while ($in =~ /$id\t(\d+)\t\S+\t\S+\t(\d+)\t(\d+)/g)
    {
    my ($ssr_nr,$size,$start) = ($1,$2,$3);
    $count++;
    print OUT "SEQUENCE_ID=$id"."_$ssr_nr\nSEQUENCE_TEMPLATE=$seq\n";
    print OUT "PRIMER_PRODUCT_SIZE_RANGE=100-280\n";
    print OUT "TARGET=",$start-3,",",$size+6,"\n";
    print OUT "PRIMER_MAX_END_STABILITY=250\n=\n"
    };
  };
print "\n$count records created.\n";

How can I initialize $count?

5
  • adding "use diagnostics" would suggest it's a warning which occurs always when performing stringification of undef value.
    – mpapec
    Commented Nov 26, 2023 at 12:24
  • Is this an actual program -- without use strict;? I very strongly recommend to always have that, along with use warnings; (better than -w).
    – zdim
    Commented Nov 26, 2023 at 20:37
  • It is better to use "lexical" filehandles with open than the globs, so: open my $fh_in,... instead of open IN,.... See open
    – zdim
    Commented Nov 26, 2023 at 20:46
  • 1
    Rolled back to a version of the question that does not disqualify the top voted answer.
    – TLP
    Commented Nov 28, 2023 at 15:33
  • Some advices: consider use open with 3 arguments instead 2 (perldoc -f open), consider add use strict; use warnings; in your code and check about IO::File module — will help a lot programs like this Commented Dec 5, 2023 at 21:42

2 Answers 2

5

Your code does not initialize the $count variable; it is undefined.

To initialize $count to a numeric value (like 0), change:

my $count;

to:

my $count = 0;
1
  • @annxxin Why became this? You changed code or data.
    – clamp
    Commented Nov 27, 2023 at 12:47
4

There are multiple paths through the code that do not result in $count++ being called. One or more of these may never be true:

  • while (<SRC>)
  • unless (my ($id,$seq) = /(.*?)\n(.*)/s)
  • while ($in =~ /$id\t(\d+)\t\S+\t\S+\t(\d+)\t(\d+)/g)

Before the first use, unconditionally initialise $count. The obvious place would be in the my $count statement.


perldoc perlsyn explains:

The only things you need to declare in Perl are report formats and subroutines (and sometimes not even subroutines). A scalar variable holds the undefined value ("undef") until it has been assigned a defined value, which is anything other than "undef". When used as a number, "undef" is treated as 0; when used as a string, it is treated as the empty string, ""; and when used as a reference that isn't being assigned to, it is treated as an error. If you enable warnings, you'll be notified of an uninitialized value whenever you treat "undef" as a string or a number. Well, usually.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.