Guest User

Untitled

a guest
Jun 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. sub outer
  2. {
  3. my $a = 123;
  4.  
  5. sub inner
  6. {
  7. print $a, "n";
  8. }
  9.  
  10. # At this point, $a is 123, so this call should always print 123, right?
  11. inner();
  12.  
  13. $a = 456;
  14. }
  15.  
  16. outer(); # prints 123
  17. outer(); # prints 456! Surprise!
  18.  
  19. sub outer
  20. {
  21. my $a = 123;
  22.  
  23. my $inner = sub
  24. {
  25. print $a, "n";
  26. };
  27.  
  28. # At this point, $a is 123, and since the anonymous subrotine
  29. # whose reference is stored in $inner closes over $a in the
  30. # "expected" way...
  31. $inner->();
  32.  
  33. $a = 456;
  34. }
  35.  
  36. # ...we see the "expected" results
  37. outer(); # prints 123
  38. outer(); # prints 123
  39.  
  40. sub find_files
  41. {
  42. my @files;
  43.  
  44. my $wanted = sub
  45. {
  46. if($something)
  47. {
  48. push @files, $File::Find::name;
  49. }
  50. };
  51.  
  52. # The find() function called here is imported from File::Find
  53. find({ wanted => $wanted }, $directory);
  54.  
  55. return @files;
  56. }
  57.  
  58. #!/usr/bin/perl
  59.  
  60. use strict;
  61. use warnings;
  62.  
  63. sub generate_multiplier {
  64. my ($coef) = @_;
  65.  
  66. return sub {
  67. my ($val) = @_;
  68. $coef * $val;
  69. }
  70. }
  71.  
  72. my $doubler = generate_multiplier(2);
  73. my $tripler = generate_multiplier(3);
  74.  
  75. for my $i ( 1 .. 10 ) {
  76. printf "%4d%4d%4dn", $i, $doubler->($i), $tripler->($i);
  77. }
  78.  
  79. __END__
  80.  
  81. C:Temp> v
  82. 1 2 3
  83. 2 4 6
  84. 3 6 9
  85. 4 8 12
  86. 5 10 15
  87. 6 12 18
  88. 7 14 21
  89. 8 16 24
  90. 9 18 27
  91. 10 20 30
  92.  
  93. sub Foo { stuff() }
  94.  
  95. BEGIN { *Foo = sub { stuff() } } # essentially equivalent
  96.  
  97. use 5.010;
  98. use strict;
  99. use warnings;
  100.  
  101. sub make_iterator {
  102. my @list = @_;
  103. return sub { shift @list }; # new sub that 'remembers' @list
  104. }
  105.  
  106. my $iter1 = make_iterator( 0 .. 10 );
  107. my $iter2 = make_iterator( 'a' .. 'z' );
  108.  
  109. say $iter1->(); # '0'
  110. say $iter1->(); # '1'
  111. say $iter2->(); # 'a'
  112.  
  113. my @sorted_array = sort { $a <=> $b } @array;
  114.  
  115. use strict;
  116. use warnings;
  117.  
  118. sub xx {
  119. my $zz=1;
  120.  
  121. sub yy {
  122. print $zz;
  123. }
  124. }
  125.  
  126.  
  127. perl tmp.pl
  128. Variable "$zz" will not stay shared at tmp.pl line 8.
  129.  
  130. # jump table to subroutines / variables
  131. my %jump = (
  132. id => 'id',
  133. xid => 'xid',
  134. hex_id => 'xid',
  135.  
  136. h => &h,
  137. mac => &mac,
  138. sed => &sed,
  139. make => &make,
  140. nsis => &nsis,
  141.  
  142. perl => &perl,
  143. dump => &perl,
  144. yaml => &yaml,
  145. yml => &yaml,
  146. json => &json,
  147. js => &json,
  148.  
  149. help => &help,
  150. usage => sub{
  151. require Pod::Usage;
  152.  
  153. Pod::Usage::pod2usage(
  154. "run perldoc $0 or pod2text $0 for more information"
  155. );
  156. }
  157. );
Add Comment
Please, Sign In to add comment