hanish
hanish

Reputation: 67

perl : how to share variables and subroutines across various perl files

I am trying something of this sort:

main.pl

use YAML::XS
our $yaml_input = YAML::XS::LoadFile("$input_file");
parse_yaml($yaml_input); 
#this variable has to be passed to the function parse_yaml which is in other file.

parser.pl

sub parse_yaml($yaml_input)
{
#some processing
}

I have read few answers about using a package but how do we use it in this case.

Upvotes: 0

Views: 271

Answers (1)

Borodin
Borodin

Reputation: 126742

Basically you need to import the parse_yaml subroutine into you current program, rather than trying to export the value of the parameter, but I'm uncertain why you have written your own parse_yaml utility when YAML::XS::LoadFile has already done it for you

This is all very clearly described in the documentation for the Exporter module

Here's a brief example

main.pl

use strict;
use warnings 'all';

use YAML::XS 'LoadFile';
use MyUtils 'parse_yaml';

my $input_file = 'data.yaml';

my $yaml_input = LoadFile($input_file);

parse_yaml($input_file);
# this variable has to be passed to the function parse_yaml which is in other file.

MyUtils.pm

package MyUtils;

use strict;
use warnings;

use Exporter 'import';

our @EXPORT_OK = 'parse_yaml';

sub parse_yaml {
    my ($yaml_file) = @_;

    # some processing
}

1;

Upvotes: 1

Related Questions