WalaWiki content from p1k3.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.8 KiB

  1. WareLogging. Some bits of PerlLanguage. Also look at PerlCompatibleRegularExpressions.
  2. = one-liners and the like =
  3. $/ = "\n"; # record separator
  4. $re = qr/whatever/; # store a RegEx
  5. # ridiculous (shouldn't cut or basename do this?), but...
  6. # in your shell - count uniquely named jpgs:
  7. find . -iname '*.jpg' | perl -n -e 's{.*/(.*$)}{$1}; print'| sort | uniq | wc -l
  8. # 'course, find can do the filename trimming by itself:
  9. find . -iname '*.jpg' -printf '%f\n'|sort|uniq|wc -l
  10. = return a date-based directory name =
  11. (Edit this page to see brackets in the proper spot, until I fix the crappy markup around here.)
  12. #!/usr/bin/perl
  13. use POSIX qw(strftime);
  14. my @t = localtime;
  15. my $dir = strftime( "%Y/%m/%e", @t );
  16. $dir =~ s/\/[0 ]/\//g;
  17. print $dir;
  18. = dispatch table to package subs =
  19. package Whatever::Thing;
  20. my %tags = (
  21. p => \&tag,
  22. em => \&tag,
  23. small => \&tag,
  24. strong => \&tag,
  25. table => \&tag,
  26. );
  27. # Install appropriate subs in symbol table:
  28. for my $key (keys %tags) {
  29. *$key = sub { $tags{$key}->($key, @_) };
  30. }
  31. sub tag {
  32. my ($tag, $text, $class) = @_;
  33. if (defined $class) {
  34. return qq{<$tag class="$class">$text</$tag>};
  35. } else {
  36. return qq{<$tag>$text</$tag>};
  37. }
  38. }
  39. # should give you '<p class="item"><strong>Some stuff.</strong></p>'
  40. print p(strong('Some stuff'), 'item');
  41. = print every so many lines =
  42. #!/usr/bin/perl
  43. @lines = <STDIN>;
  44. $start = 0; # array index not line - first line is 0
  45. $skip = 7;
  46. $count = $skip;
  47. for ( @lines[$start .. $#lines] ) {
  48. print if ( $count % $skip == 0 );
  49. $count++;
  50. }
  51. = recursive file rename =
  52. This still running in the background on about a gazillion images. Let's hope it doesn't break anything. Yes, next time I will stop being an idiot and use find(1).
  53. #!/usr/bin/perl
  54. use strict;
  55. use warnings;
  56. change(".");
  57. sub change {
  58. my ($dir) = @_;
  59. print "Trying $dir\n";
  60. my $dh;
  61. opendir $dh, $dir;
  62. my @files = readdir $dh;
  63. closedir $dh;
  64. for my $file (@files) {
  65. next if ($file =~ m/^\.\.$/);
  66. next if ($file =~ m/^\.$/);
  67. if (-d "$dir/$file") {
  68. # recursion.
  69. change("$dir/$file");
  70. } elsif ($file =~ m/\.jpe?g/i) {
  71. next if ($file =~ m/\.jpg$/);
  72. my $new_file = $file;
  73. $new_file =~ s/\.jpe?g/\.jpg/i;
  74. rename ("$dir/$file", "$dir/$new_file");
  75. print "$dir/$file to: $dir/$new_file\n";
  76. }
  77. }
  78. }
  79. = grep =
  80. $ perl
  81. %ignore = (blah => 1);
  82. @wog = qw(blah wog tog dog);
  83. @wog = grep { ! $ignore{$_} } @wog;
  84. print @wog;
  85. wogtogdog
  86. $ perl
  87. $n = 100;
  88. if ( grep { $n % $_ != 0 } (1..10) ) {
  89. print $n;
  90. }
  91. 100