Dinesh
Dinesh

Reputation: 1866

Perl not able to access sub from module file

I have create two files both in same folder to learn modules.

Modules.pm

package Modules;

use strict;
use warnings;

require Exporter;

BEGIN { our @EXPORT = qw(Print); }

sub Print { print shift(@_); }

END { }

1;

Main.pl

use strict;
use warnings;

use Modules;

Print("Hello World!");

on running the command perl Main.pl I'm getting the following error. What I am doing wrong?

Undefined subroutine &main::Print called at Main.pl line 7

Upvotes: 0

Views: 44

Answers (1)

mpapec
mpapec

Reputation: 50637

You've forgot to tell your package that it inherits from Exporter

package Modules;

use strict;
use warnings;

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw(Print);

sub Print { print shift(@_); }


1;

or less error prone,

package Modules;

use strict;
use warnings;

use parent qw( Exporter );
our @EXPORT = qw(Print);

sub Print { print shift(@_); }


1;

More in perldoc

Upvotes: 4

Related Questions