user2234234
user2234234

Reputation: 349

How to require a file and use its subroutine

how can I use a subroutine in a required file, when the subroutien has to get variables:

file.pl:

...
sub func{
  my ($var) = @_;
  ..

}

main.pl:

..
require "file.pl";
func(1);
..

this is not working for me, I'm getting an error says Undefined subroutine &main::func ..

Upvotes: 0

Views: 90

Answers (1)

mpapec
mpapec

Reputation: 50637

Perl was telling you that no main::func exists (main is default package), so you need to prefix your function with qualified package name,

require "file.pl";
Naming::func(1);

Upvotes: 2

Related Questions