1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#! /usr/bin/env perl use strict; use warnings; use 5.020; sub function { # arguments are aliased to @_ # Overwrite the first argument $_[0] = "Hallo Welt"; } my $foo = "Hello World"; say "Vorher: $foo"; function($foo); say "Nachher: $foo"; __END__;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#! /usr/bin/env perl use strict; use warnings; use 5.020; # Prototype defines, we expect ONE argument and we pass it by reference # (the user doesn't need to reference it herself) sub function (\$) { my ( $ref ) = @_; $$ref = "Hallo Welt"; } my $foo = "Hello World"; say "VORHER: $foo"; # see, no reference by user here (\$foo)! function($foo); say "NACHHER: $foo"; __END__;
QuoteThe array @_ is a local array, but its
elements are aliases for the actual scalar parameters.
1 2 3 4 5 6 7 8 9 10
use 5.030; use feature 'signatures'; sub incr($x) { ++$x; } my $value = 0; say incr($value); # 1 say $value; # 0 (immer noch)