Jairo Jairo - 1 month ago 11
Perl Question

Issues executing with ActivePerl 5.20

I have a program which uses a module, located at the same folder that the executable but when I execute it it only closes.

use strict;
use warnings;
use genLetras;




for my $k (1 .. 30 ) {
for my $j (1 .. 30 ) {
genLetras::generarLetra();
$matriz[$k][$j] = genLetras::generarLetra();
}
}

for my $i (1 .. 30 ) {
for my $j (1 .. 30 ) {
print "[$matriz[$k][$j]] ";
}
print "\n";
}

<>;


That is the code of the executable.

An this one is the modules's one

use strict;
use warnings;
use Math::Complex;

my $seed = time();
my $a = $seed / 5;
my $c = $seed - 7;
my $x = $seed;
my $m = sqrt($seed % 574) + $seed;
my $numAleatorio;


sub generadorMultiplicativo{
$numAleatorio = ((($a*$x) + $c) % $m);
$x = $numAleatorio;
}
my $letra;
my $residuo;
sub generarLetra{
for my $i(1..30){
generadorMultiplicativo();
$residuo = $x/$m;

if($residuo < 0.0384615384615385 ){
$letra = 'A';
}
if($residuo > 0.0384615384615385 && $residuo < 0.076923076923077){
$letra = 'B';
}
if($residuo > 0.076923076923077 && $residuo < 0.1153846153846154){
$letra = 'C';
}
if($residuo > 0.1153846153846154 && $residuo < 0.1538461538461538){
$letra = 'D';
}
if($residuo > 0.1538461538461538 && $residuo < 0.1923076923076923){
$letra = 'E';
}
if($residuo > 0.1923076923076923 && $residuo < 0.2307692307692308){
$letra = 'F';
}
if($residuo > 0.2307692307692308 && $residuo < 0.2692307692307692){
$letra = 'G';
}
if($residuo > 0.2692307692307692 && $residuo < 0.3076923076923077 ){
$letra = 'H';
}
if($residuo > 0.3076923076923077 && $residuo < 0.3461538461538462){
$letra = 'I';
}
if($residuo > 0.3461538461538462 && $residuo < 0.3846153846153846){
$letra = 'J';
}
if($residuo > 0.3846153846153846 && $residuo < 0.4230769230769231){
$letra = 'K';
}
if($residuo > 0.4230769230769231 && $residuo < 0.4615384615384615){
$letra = 'L';
}
if($residuo > 0.4615384615384615 && $residuo < 0.5){
$letra = 'M';
}
if($residuo > 0.4615384615384615 && $residuo < 0.5384615384615385){
$letra = 'N';
}
if($residuo > 0.5384615384615385 && $residuo < 0.5769230769230769){
$letra = 'O';
}
if($residuo > 0.5769230769230769 && $residuo < 0.6153846153846154){
$letra = 'P';
}
if($residuo > 0.6153846153846154 && $residuo < 0.6538461538461538){
$letra = 'Q';
}
if($residuo > 0.6538461538461538 && $residuo < 0.6923076923076923){
$letra = 'R';
}
if($residuo > 0.6923076923076923 && $residuo < 0.7307692307692308){
$letra = 'S';
}
if($residuo > 0.7307692307692308 && $residuo < 0.7692307692307692){
$letra = 'T';
}
if($residuo > 0.7692307692307692 && $residuo < 0.8076923076923077){
$letra = 'U';
}
if($residuo > 0.8076923076923077 && $residuo < 0.8461538461538462){
$letra = 'V';
}
if($residuo > 0.8461538461538462 && $residuo < 0.8846153846153846){
$letra = 'W';
}
if($residuo > 0.8846153846153846 && $residuo < 0.9230769230769231){
$letra = 'X';
}
if($residuo > 0.9230769230769231 && $residuo < 0.9615384615384615){
$letra = 'Y';
}
if($residuo > 0.9615384615384615 && $residuo < 1){
$letra = 'Z';
}
return;
}
}


I've already compiled both with perl-c , perl -V and all said it was correct.

I'm using ActivePerl 5.20 on Windows 10

Answer

Four things to consider here:

  1. The module's filename must be genLetras.pm.
  2. The main script must find it. Do as @ikegami suggested in his answer and add

    use FindBin qw( $RealBin );
    use lib $RealBin;
    

    before the use genLetras; line.

  3. The module must end with some true value else the loader will complain. Add the line

    1;
    

    as the very last line to your module.

  4. The module needs a line package genLetras; as the first line. Add that.

Further notes:

  • Module names – by convention – usually start with an uppercase letter, like GenLetras, because lowercase letters are reserved for pragmas (like e.g. warnings or strict).

  • I use FindBin usually like this:

    use FindBin;
    use lib $FindBin::Bin;
    

    but that's more a matter of taste.

  • The <>; at the end of your main script normally is useless. I think you used it to prevent your Perl window from closing immediately but wait for you to press ENTER instead. That's ok then.

  • I wonder why perl -c script.pl didn't show any errors. At my PC it did.