Perl file changes package between functions / inside

I look at some of our Perl codebases and am puzzled by the use packageof some files.

We have a file containing useful functions functions.pl, which looks something like this:

package functions;
use strict;

sub function_a {
  # code here
}

sub function_b {
  # code here
}

package main;

sub function_c {
  my ($arguments, $for, $this, $function) = @_;

  package functions; 

  # Actual function code here.
}

(The names of functions and packages are changed, obviously.)

The functions in this file are used in other scripts with require 'functions.pl', and then called &function_c()- because the scripts where it is called function_cdo not declare a package, they are supposedly in the namespace main, so don When you call it, function_cyou need to attach something before adding it.

function_a function_b , , , function_c , functions:: .

- , - script, , , ?

, Perl , package , , ?

+4
2

, . package "" , .

, , , ; , script () export , .


package - OO, , . :

package MyClass;

# ... MyClass methods here ...

package MyClass::Helper {
    # ... helper class methods here ...
}

# ... more MyClass methods here ...

, Perl (< 5.14):

package MyClass;

# ... MyClass methods here ...

{
    package MyClass::Helper;
    # ... helper class methods here ...
}

# ... more MyClass methods here ...
+3

, script (, function.pl; function1.pl; function2.pl) .

, , $function, $function function.pl function1.pl

, ;)

+2

Source: https://habr.com/ru/post/1529192/


All Articles