W Perlu, jak umieścić wiele pakietów w single.pm akta?

Jestem prawie pewien, że gdzieś czytałem, że to możliwe, ale jest kilka gotchas, o których musisz być świadomy. Niestety, nie mogę znaleźć tutoriala lub strony opisującej, co musisz zrobić. Przejrzałem tutoriale Perla i nie znalazłem tego, który pamiętam. Czy ktoś mógłby wskazać mi Stronę lub dokument opisujący jak umieścić kilka pakietów w jednym pliku. pm?

Author: Thomas Owens, 2009-11-17

4 answers

Po prostu zaczynasz nowy pakiet z inną instrukcją pakietu:

package PackageOne;

# ...... code

package PackageTwo;

# .... more code

Problemy z takim podejściem (zarchiwizowane na 2009)

 16
Author: ennuikiller,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-06-12 10:44:18

Tak to zwykle robię:

use strict;
use warnings;
use 5.010;

{
    package A;
    sub new   { my $class = shift; bless \$class => $class }
    sub hello { say 'hello from A' }
}

{
    package B;
    use Data::Dumper;
    sub new   { my $class = shift; bless { @_ } => $class }
    sub hello { say 'Hello from B + ' . shift->dump       }
    sub dump  { Dumper $_[0] }
}

$_->hello for A->new, B->new( foo => 'bar' );
 31
Author: draegtun,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-21 15:40:03

Jak to zrobić: wystarczy wydać wiele package instrukcji.

Gotchas przychodzi mi na myśl: my - zmienne nie są zlokalizowane w pakietach, więc i tak są współdzielone. Przed wydaniem, domyślnie znajdujesz się w pakiecie main.

 4
Author: JB.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-11-17 13:36:54

To mi się udało:

#!/usr/bin/perl

use strict;
use warnings;

{
   package A;
   use Exporter;
   our @ISA = qw(Exporter);
   our @EXPORT_OK = qw(a_sub);
   our @EXPORT = qw(a_sub);

   sub a_sub {
       # your code ...
   }
}
{
   package B;
   use Exporter;
   our @ISA = qw(Exporter);
   our @EXPORT_OK = qw(b_sub);
   our @EXPORT = qw(b_sub);

   sub b_sub {
       # your code ...
   }
}

# Main code starts here ##############

use boolean;
use Data::Dumper;

import A qw(a_sub);
import B qw(b_sub);

a_sub();
b_sub();

Ważne jest to, że zamiast używać "use", zmieniasz go na "import" (w ten sposób nie pójdzie i nie spróbuje szukać pliku).

 2
Author: lepe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-29 11:51:50