Thread Bad name after... (52 answers)
Opened by bianca at 2015-02-19 12:08

bianca
 2015-02-21 15:00
#179816 #179816
User since
2009-09-13
6977 Artikel
BenutzerIn

user image
Klasse!! Das geht prinzipiell tatsächlich! Danke dir!

Nun baue ich das ein und brauche jetzt weitere Funktionalität. Und dafür brauche ich jetzt bitte noch ein bisschen Anleitung, wie man Code in eval schreibt.

Das hier ist das Hauptscript test.pl:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl
use strict;
use warnings;

my @dispatchtab = (
    {
        moduldatei      => 'test_extern.pl',
        aufruf_neu      => 'testsub',
        unit            => 'unit1',
        referenz        => defined,
    },
    {
        moduldatei      => 'test_extern2.pl',
        aufruf_neu      => 'testsub',
        unit            => 'unit2',
        returnval       => defined,
    },
    # hier sind natürlich noch ganz viele
);
for my $ref ( @dispatchtab ) {
    #
    # besonderer Code folgt wegen des Problems der doppelten sub Namen und des darauf folgenden "...redefined at..."
    # dies ist nur eine Übergangslösung, bis alle sub's in sub's aufgelöst sind
    # Quelle: Linuxer: https://www.perl-community.de/bat/poard/thread/19514#ms_179778
    #
    my ($back,%back);
    eval <<"EVAL_CODE";
package $ref->{unit};
require "$ref->{moduldatei}";
if ($ref->{returnval}) {
    $back = $ref->{aufruf_neu}->("$ref->{unit}");
    print "Back: '$back'\n";
}
else {
    $ref->{aufruf_neu}->("$ref->{unit}",\%back);
    print "Back: '$back{text}'\n";
}
EVAL_CODE
;
    warn "$@\n" if $@;
}


test_extern.pl:
Code (perl): (dl )
1
2
3
4
5
6
7
8
#!/usr/bin/perl
use strict;
use warnings;

sub testsub {
    return "Gewonnen! Sub $_[0]\n";
}
return '1';


test_extern2.pl:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/perl
use strict;
use warnings;

sub testsub {
    my ($val,$back) = @_;
    $back{text} = "Gewonnen! Sub $val\n";
    return;
}
return '1';

Ich muss zwei verschiedene Arten des Aufrufs unterscheiden können. Einmal kommt das Ergebnis in die Variable, einmal wird es in das Hash gesteckt, dessen Referenz in die sub übergeben wird.
Wie schreibe ich das richtig?

Im Moment wirft das:
Quote
Use of uninitialized value in concatenation (.) or string at test.pl line 27.
Use of uninitialized value $back in concatenation (.) or string at test.pl line 27.
Use of uninitialized value $back in concatenation (.) or string at test.pl line 27.
Use of uninitialized value in concatenation (.) or string at test.pl line 27.
syntax error at (eval 1) line 3, near "() "
syntax error at (eval 1) line 7, near ";
}"
(Might be a runaway multi-line "" string starting on line 5)
syntax error at (eval 1) line 12, near ";
}"
(Might be a runaway multi-line "" string starting on line 10)

Use of uninitialized value $back in concatenation (.) or string at test.pl line 27.
Use of uninitialized value $back in concatenation (.) or string at test.pl line 27.
Use of uninitialized value in concatenation (.) or string at test.pl line 27.
syntax error at (eval 2) line 3, near "() "
syntax error at (eval 2) line 7, near ";
}"
(Might be a runaway multi-line "" string starting on line 5)
syntax error at (eval 2) line 12, near ";
}"
(Might be a runaway multi-line "" string starting on line 10)
10 print "Hallo"
20 goto 10

View full thread Bad name after...