[Vienna-pm] Gültigkeitsbereich von Variablen

Peter J. Holzer hjp-vienna-pm-list at hjp.at
Mon Feb 23 04:01:27 CST 2004


On 2004-02-23 06:24:12 +0100, Klaus Zahradnik wrote:
> Das folgende Script soll den Gültigkeitsbereich von Variablen
> "erklären":
> 
> #!/usr/bin/perl
> use warnings;
> use strict;
>                                                                                 
> my $x = 10;
> $_ = "alpha";
[...]
> $x ist immer 10. Daraus folgern wir, das der Wert von $x aus dem Bereich
> genommen wird, in dem die Funktion definiert wird, richtig?

Ja.

> Aber warum ist das so?

Weil Larry Wall es so festgelegt hat :-)

> Nach meinem Verständnis, müsste $x aus dem
> Bereich genommen werden in welchem die subroutine aufgerufen wird!?

Nein, das macht local.

Aus perlsub:

       A "local" modifies its listed variables to be "local" to the enclosing
       block, "eval", or "do FILE"--and to any subroutine called from within
       that block.  A "local" just gives temporary values to global (meaning
       package) variables.  It does not create a local variable.  This is
       known as dynamic scoping.  Lexical scoping is done with "my", which
       works more like Cs auto declarations.

	hp


-- 
   _  | Peter J. Holzer    | I think we need two definitions:
|_|_) | Sysadmin WSR       | 1) The problem the *users* want us to solve
| |   | hjp at hjp.at         | 2) The problem our solution addresses.
__/   | http://www.hjp.at/ |    -- Phillip Hallam-Baker on spam
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/vienna-pm/attachments/20040223/1d8c6b10/attachment.bin


More information about the Vienna-pm mailing list