Strict mode

marcel.grunauer at lovely.net marcel.grunauer at lovely.net
Thu Dec 30 07:36:09 CST 1999


* * * vienna-pm-list * * *


?? Ein Programmlauf schreibt mir nun:
?? Global symbol "platform" requires explicit package name at t.pl line ...
?? 
?? Nun die Frage, wie ich den Package Namen wirklich zuweise. Muss ich 
?? hier spezielle Definitionen machen? Ich schreibe ja kein Modul.


perldoc -f require:


=item require EXPR

[...] demands that a library file be included if it hasn't already
been included.  The file is included via the do-FILE mechanism, which is
essentially just a variety of C<eval()>.


perldoc -f do:


=item do EXPR

[...] It also differs in that code evaluated with C<do FILENAME> cannot
see lexicals in the enclosing scope; C<eval STRING> does.


Und das Perl Cookbook (Ram 8:16: Reading Configuration Files) sagt auch:


As with a file read in using require or use, those read in using do count
as a separate and unrelated lexical scope. That means the configuration
file can't access its caller's lexical (my) variables, nor can the caller
find any such variables that might have been set in the file. It also
means that the user's code isn't held accountable to a pragma like use
strict or use integer that may be in effect in the caller.

If you don't want clean partitioning of variable visibility, you can
get the config file's code executed in your own lexical scope. If you
have a cat program or its technical equivalent handy, you could write
yourself a hand-rolled do:

	eval `cat $ENV{HOME}/.myprogrc`;

We've never actually seen anyone (except Larry) use that approach in
production code.


Mit anderen Worten, via require() kannst Du keine lexicals definieren,
die dann im Hauptprogramm ohne weiteres verfuegbar sind. Es ist eigentlich
zum definieren von allgemeinen subroutines gedacht. Oder fuer globale
Variablen, aber die wollen wir ja vermeiden.

Du solltest in Deinem Fall besser ein Modul definieren. Oder Du liest die
Konfigurationsdatei als Textdatei und weist die gelesenen Werte einem Hash
zu, allerdings geht i.A. nur fuer Skalare (ohne groeberen Aufwand). Z.B.:

	my %User_Preferences;

	while (<CONFIG>) {
		chomp;                  # no newline
		s/#.*//;                # no comments
		s/^\s+//;               # no leading white
		s/\s+$//;               # no trailing white
		next unless length;     # anything left?
		my ($var, $value) = split(/\s*=\s*/, $_, 2);
		$User_Preferences{$var} = $value;
	} 



Marcel


###
You are subscribed to vienna-pm-list
http://www.fff.at/fff/vienna.pm/



More information about the Vienna-pm mailing list