0

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.

hanish
  • 67
  • 6
  • 4
    There's plenty of documentation on how to create modules. [perlmod](http://perldoc.perl.org/perlmod.html) for example. However, without a clearer explanation, we can't really answer your question. What are you actually trying to accomplish? – Sobrique May 11 '16 at 08:36
  • 1
    You could bless an object of your module and then use it access variables, subroutines in your main.pl. For using a variable you will be using `$module::variablename`. Let me know if this clarifies things. – AbhiNickz May 11 '16 at 08:59
  • @AbhiNickz : i did that to access variable, how to do the same for function? – hanish May 11 '16 at 13:11
  • 1
    @AbhiNickz: i got it , we have to use &modulename::function() – hanish May 11 '16 at 13:21

1 Answers1

1

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;
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • parse_yaml() subroutine is written to take variable values from my yaml file. Loadfile just gives the pointer (reference0 to the first object in Yaml. – hanish May 11 '16 at 13:22