What happens on these lines of using Perl?

I do not understand what is going on here:

use PAR { file => 'foo.par', fallback => 1 };

I think an anonymous hash. How does a module use it from a string use? Can you shed some light?

EDIT: I'm not interested in the PAR module. I'm just interested in how it works behind a curtain. How do I configure such modules?

+3
source share
5 answers

Basically, this is what the hashref funny syntax does (using perldoc -f use as a reference):

Let's say we have this base module (put it in Foo.pmin your current directory):

package Foo;

use Data::Dumper;
sub import
{
    print "import was passed these arguments: ". Dumper(\@_);
}

1;

perl -I. -wle'use Foo { a => 1, b => 2}', Foo->import({a=>1, b=>2}). , , :

import was passed these arguments: $VAR1 = [
          'Foo',
          {
            'a' => 1,
            'b' => 2
          }
        ];

, Exporter, , , import() ( , !)

+7

use , . (, , , ), (-).

:

use Module LIST

:

BEGIN {
    require Module;
    Module->import( LIST );
}

BEGIN , . require , . module import() , ( LIST) use.

- LIST, import(). ; , import() Exporter. . perldoc -f.

import(), , , , export_to_level(), Exporter. , , . 1 , , . , import().

sub import {
    my ($class, @args) = @_;

    # Do whatever you need to do with the LIST of arguments
    # supplied by the client code using your module.


    # Let Exporter do its normal work of exporting symbols
    # into the client code using your module.
    $class->export_to_level(1, @_);
}
+7

, . import.

+2

PAR - CPAN Perl Archive Toolkit. Hashref - , PAR.

:

use PAR { file => 'foo.par, fallback => 1 };
use Foo::Bar;

Foo::Bar, , "foo.par" ( Foo::Bar).

0

: PAR this ( 340):

# called on "use PAR"
sub import {
    my $class = shift;
    [...]
    my @args = @_;
    [...]
    # process args to use PAR 'foo.par', { opts }, ...;
    foreach my $par (@args) {
        if (ref($par) eq 'HASH') { # <---- This is what handle your case!
            # we have been passed a hash reference
            _import_hash_ref($par);
        }
        elsif ($par =~ /[?*{}\[\]]/) {
           # implement globbing for PAR archives
           [...]
        }
        else {
            # ordinary string argument => file
            [...]
        }
    }

, , , , , CPAN , . , : "" , perl "import" .

: . {key = > "value", key2 = > "value2",...} . perlref, perlreftut.

0
source

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


All Articles