fork download
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. ########################################
  6. # global variables – all start at 0
  7. our ($a, $b, $c, $d) = (0, 0, 0, 0);
  8. ########################################
  9.  
  10. sub g1 { # void g1(int b, int c)
  11. local ($b, $c) = @_; # dynamically-scoped parameters
  12. print "$a $b $c $d\n"; # prints global a & d, local b & c
  13. }
  14.  
  15. sub g2 { # void g2(int a, int c)
  16. local ($a, $c) = @_; # re-bind a and c dynamically
  17. g1($a, $c); # call g1(a,c)
  18. }
  19.  
  20. sub g3 { # int g3(int c, int a)
  21. local ($c, $a) = @_; # parameter order: (c , a)
  22. local $b = 3; # int b = 3;
  23. g1($a, $b); # g1(a,b);
  24.  
  25. { # begin nested block
  26. local $d = 4; # int d = 4;
  27. local $c = 8; # int c = 8;
  28. g2($a, $b); # g2(a,b);
  29. } # d and c revert here
  30.  
  31. g1($a, $b); # g1(a,b);
  32. return $b; # return b;
  33. }
  34.  
  35. sub main {
  36. local $a; # int a;
  37. local $b; # int b;
  38. $a = 4;
  39. $b = 5;
  40. $a = g3($b, $c); # a = g3(b,c); (c is still 0)
  41. g3($b, $a); # g3(b,a); // return value ignored
  42. }
  43.  
  44. main();
  45.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
0 0 3 0
0 0 3 4
0 0 3 0
3 3 3 0
3 3 3 4
3 3 3 0