code_junkie

Hidden features of Perl?

Nov 11th, 2011
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. while(<$fh>)
  2. {
  3. next if 1..1; # skip first record
  4. ...
  5. }
  6.  
  7. $ perl -wle 'my $x = 3; print $ x'
  8. 3
  9.  
  10. $ perl -lwe '*4 = sub { print "yes" }; 4->()'
  11. yes
  12.  
  13. $ perl -wle 'print !!4'
  14. 1
  15. $ perl -wle 'print !!"0 but true"'
  16. 1
  17. $ perl -wle 'print !!0'
  18. (empty line)
  19.  
  20. $ perl -Mstrict -wle 'print q bJet another perl hacker.b'
  21. Jet another perl hacker.
  22.  
  23. m xabcx
  24. # same as m/abc/
  25.  
  26. s{
  27. ^ # make sure to get whole filename
  28. (
  29. [^'] + # at least one non-quote
  30. . # extension dot
  31. (?: # now either suffix
  32. gz
  33. | Z
  34. )
  35. )
  36. z # through the end
  37. }{gzcat '$1' |}xs for @ARGV;
  38.  
  39. while (<>) {
  40. print;
  41. }
  42.  
  43. $x = $a || $b;
  44.  
  45. # $x = $a, if $a is true.
  46. # $x = $b, otherwise
  47.  
  48. $x = $a || $b || $c || 0;
  49.  
  50. my $_ = "a"
  51. print -$_
  52.  
  53. my $_ = "a"
  54. print -$_
  55.  
  56. my $_ = "a"
  57. print -$_
  58.  
  59. print ++$_
  60.  
  61. $_ = 'z'
  62. print ++$_
  63.  
  64. *My::Namespace:: = %Your::Namespace
  65.  
  66. my $method = 'foo';
  67. my $obj = My::Class->new();
  68. $obj->$method( 'baz' ); # calls $obj->foo( 'baz' )
  69.  
  70. *foo = sub { print "Hello world" };
  71.  
  72. use subs 'unlink';
  73. sub unlink { print 'No.' }
  74.  
  75. BEGIN{
  76. *CORE::GLOBAL::unlink = sub {print 'no'}
  77. };
  78.  
  79. unlink($_) for @ARGV
  80.  
  81. my $url = q{http://my.url.com/any/arbitrary/path/in/the/url.html};
  82.  
  83. my $var = q#some string where the pound is the final escape.#;
  84. my $var2 = q{A more pleasant way of escaping.};
  85. my $var3 = q(Others prefer parens as the quote mechanism.);
  86.  
  87. my $var4 = qq{This "$mechanism" is broken. Please inform "$user" at "$email" about it.};
  88.  
  89. my $output = qx{type "$path"}; # get just the output
  90. my $moreout = qx{type "$path" 2>&1}; # get stuff on stderr too
  91.  
  92. sub MyRegexCheck {
  93. my ($string, $regex) = @_;
  94. if ($string)
  95. {
  96. return ($string =~ $regex);
  97. }
  98. return; # returns 'null' or 'empty' in every context
  99. }
  100.  
  101. my $regex = qr{http://[w].com/([w]+/)+};
  102. @results = MyRegexCheck(q{http://myurl.com/subpath1/subpath2/}, $regex);
  103.  
  104. my @allowed = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z { });
  105. my @badwords = qw(WORD1 word2 word3 word4);
  106. my @numbers = qw(one two three four 5 six seven); # works with numbers too
  107. my @list = ('string with space', qw(eight nine), "a $var"); # works in other lists
  108. my $arrayref = [ qw(and it works in arrays too) ];
  109.  
  110. for ($item)
  111. {
  112. s/&‎nbsp;/ /g;
  113. s/<.*?>/ /g;
  114. $_ = join(" ", split(" ", $_));
  115. }
  116.  
  117. my @list = ('abc', 'def', 'ghi', 'jkl');
  118.  
  119. my @list = qw(abc def ghi jkl);
  120.  
  121. print 1, 2, 3, ;
  122.  
  123. print
  124. results_of_foo(),
  125. results_of_xyzzy(),
  126. results_of_quux(),
  127. ;
  128.  
  129. my @lines = <DATA>;
  130. for (@lines) {
  131. print if /bad/;
  132. }
  133.  
  134. __DATA__
  135. some good data
  136. some bad data
  137. more good data
  138. more good data
  139.  
  140. sub do_stuff_with_a_hash (&%) {
  141. my ( $block_of_code, $hash_ref ) = @_;
  142. while ( my ( $k, $v ) = each %$hash_ref ) {
  143. $block_of_code->( $k, $v );
  144. }
  145. }
  146.  
  147. use Data::Dumper;
  148.  
  149.  
  150. do_stuff_with_a_hash {
  151. local $Data::Dumper::Terse = 1;
  152. my ( $k, $v ) = @_;
  153. say qq(Hey, the key is "$k"!);
  154. say sprintf qq(Hey, the value is "%v"!), Dumper( $v );
  155.  
  156.  
  157. } %stuff_for
  158. ;
  159.  
  160. perl -MLib::DB -MLib::TL -e 'run_expensive_database_delete() if $hour_of_day < AM_7';
  161.  
  162. my $old_die_handler = $SIG{__DIE__};
  163. $SIG{__DIE__}
  164. = sub { say q(Hey! I'm DYIN' over here!); goto &$old_die_handler; }
  165. ;
  166.  
  167. overload::constant
  168. integer => sub {
  169. my $lit = shift;
  170. return $lit > 2_000_000_000 ? Math::BigInt->new( $lit ) : $lit
  171. };
  172.  
  173. print '-' x 80; # print row of dashes
  174.  
  175. print for (1, 4, 9) x 3; # print 149149149
  176.  
  177. ls |perl -lne 'print $_; }{ print "$. Files"'
  178.  
  179. LINE: while (defined($_ = <ARGV>)) {
  180. print $_; }{ print "$. Files";
  181. }
  182.  
  183. $a = 5 <=> 7; # $a is set to -1
  184. $a = 7 <=> 5; # $a is set to 1
  185. $a = 6 <=> 6; # $a is set to 0
  186.  
  187. while( <> ){
  188. print "top of loopn";
  189. chomp;
  190.  
  191. next if /next/i;
  192. last if /last/i;
  193.  
  194. print "bottom of loopn";
  195. }continue{
  196. print "continuen";
  197. }
  198.  
  199. while(/G(bw*b)/g) {
  200. print "$1n";
  201. }
  202.  
  203. while (<>) {
  204. ... # code for each line
  205. }
  206.  
  207. BEGIN {
  208. eval {
  209. require 'config.local.pl';
  210. };
  211. if ($@) {
  212. require 'config.default.pl';
  213. }
  214. }
  215.  
  216. rename("$_.part", $_) for "data.txt";
  217.  
  218. print ~~ localtime;
  219.  
  220. print scalar localtime;
  221.  
  222. print localtime;
  223.  
  224. SKIP: {
  225. skip() if $something;
  226.  
  227. print "Never printed";
  228. }
  229.  
  230. sub skip {
  231. no warnings "exiting";
  232. last SKIP;
  233. }
  234.  
  235. local $SIG{__WARN__} = sub { die @_ };
  236. $num = "two";
  237. $sum = 1 + $num;
  238. print "Never reached";
  239.  
  240. $_ = "foo bar";
  241. my $count =()= /[aeiou]/g; #3
  242.  
  243. sub foo {
  244. return @_;
  245. }
  246.  
  247. $count =()= foo(qw/a b c d/); #4
Add Comment
Please, Sign In to add comment