1
votes

I have some code I decided to pull into a module in perl. I admit that I am doing a bit of monkey-see monkey-do stuff following documentation I've found online.

There is only one pubicly visible subroutine, which sets 2 variables * would like to use in other subroutines in the module, without passing them explicitly as parameters -- $fname, and @lines. Looking online I came up with the "our" keyword, but when I try to declare them at global level (see code snippet below), I get the following error:

mig_tools.pm did not return a true value at

I have worked around the issue by declaring "our $fname" and "our @lines" in every subroutine they are used, but I would prefer to declare them once at global scope. Is that possible?

Here's what I take to be the relevant part of the code.

package mig_tools;
require Exporter;

use strict;
use warnings;

our @ISA = qw(Exporter);
our @EXPORT = qw( ensure_included);

our $fname;
our @lines;

// definitions of already_has_include find_include_insert_point and ensure_included.
1

1 Answers

2
votes

All-lowercase variables are reserved for local identifiers and pragma names. Your module should be in MigTools.pm and you should use it with use MigTools

The did not return a true value error is just because your module file didn't return a true value when it was executed. It is usual to add a line containing just 1; to the end of all modules

Your MigTools.pm should look like this. It is best to use the import directly from Exporter instead of subclassing it, and our doesn't help you to create a global variable, but I am a little worried about why you are structuring your module this way

package MigTools;

use strict;
use warnings;

use Exporter qw/ import /;

our @EXPORT = qw/ ensure_included /;

my ($fname, @lines)

sub ensure_included {

}

sub already_has_include {

}

sub find_include_insert_point {

}

sub ensure_included {

}

1;