Hire a senior Perl / Python programmer today; download my up-to-date resume (PDF)
John Bokma MexIT
freelance Perl programmer

Exporting a scalar variable using Perl

Wednesday, July 5, 2006 | 1 comment

Yesterday I was asked by email how to export a scalar variable from a module in Perl. I assumed that the person asking this has a good reason, because exporting might not always be a good thing.

I first wrote a simple example Perl module, one that just exports one scalar default. The module inherits from the Exporter module, which does most of the work. The scalar we want to export is assigned to the @EXPORT array.

package ExportScalar;

use strict;
use warnings;

use Exporter;
use base 'Exporter';    # or: our @ISA = 'Exporter';

our @EXPORT = qw( $example_scalar );

our $example_scalar = 42;

1;

The following small Perl program shows how $example_scalar is indeed imported by default due to the use ExportScalar; line, and the value of the imported scalar, 42, is printed.

use strict;
use warnings;

use ExportScalar;

print $example_scalar, "\n";

Also today

Please post a comment | read 1 comment by prasant | RSS feed