How to call one module sub inside another module sub in Perl -


i have created perl modules under /my_project/abc/. abc folder contains 3 subroutine modules: build.pm, config.pm, , operation.pm. there common subroutine in operation.pm need access config.pm, when tried throws

"configuretherepository" not exported abc::config module`.

here call stack.

operation.pm

    package abc::operation;      use strict;     use warnings;      use term::ansicolor qw(:constants);     use file::basename qw(dirname);     use exporter qw(import);     use term::ansicolor qw( colored );     use data::dumper qw(dumper);     use json::pp;     use file::basename qw(dirname);     use cwd  qw(abs_path);     use abc::config qw(configuretherepository);      our @export = qw(commonfunc)     sub commonfunc{       #handle logic     } 1; 

config.pm

    package abc::config;      use strict;     use warnings;      use term::ansicolor qw(:constants);     use file::basename qw(dirname);     use exporter qw(import);     use cwd  qw(abs_path);     use cwd  qw(abs_path);     use abc::operation qw(commonfunc); #compilation failed when insert line.if removed script execute , in runtime throws undefined commonfunc.     our @export = qw(configuretherepository);     sub configuretherepository{       #handle logic     }  1; 

please let me know made mistake.

there circular dependency. use statements done @ compile time. means before actual code run. when start program, , first thing go abc::operation, following happen:

  • scan abc::operation use statements
  • load term::ansicolor
    • parsing switches term::ansicolor
    • scan term::ansicolor use statements ...
  • parsing switches abc::operation
  • load file::basename
    • parsing switches file::basename
    • scan file::basename
  • parsing switches abc::operation
  • ... couple more of those
  • load abc::config
    • parsing switches abc::config
    • scan abc::config use statements ...
  • parsing witches abc::operation
    • import stuff term::ansicolor, file::basename, exporter , cwd; these not loaded again because perl loaded them before. imports symbols current abc::config namespace
    • import function commonfunc abc::config; again, has been loaded, doesn't load again
    • throw error because @ point, abc::config has not finished parsing, , not yet export commonfunc symbol

this bit confusing, it's sign architecture broken. if things common, can sure in common package. common package cannot use uses it. if that's case, whatever using becomes common definition.

the solution rethink functions go where. find smallest possible parts, , put them in 1 package. that's things every other package shares. use it's needed. next things use shouldn't bring each other in. last level should combine of them. dependency tree called tree reason. having circle in there can't work.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -