Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Perl5i

797 views

Published on

Published in: Technology
  • Be the first to comment

Perl5i

  1. 1. Perl5i Marcos Rebelo (oleber@gmail.com)
  2. 2. Tribal knowledge
  3. 3. use v5.10; use strict; use warnings; use File::Find; use File::Glob 'bsd_glob'; use DateTime; my ( $from, $to ) = @ARGV; die "Directory $from not found" if not -d $from; die "Directory $to not found" if not -d $to; sub epoch_to_iso8601 { my ( $time ) = @_; return DateTime->from_epoch(epoch => $time)->iso8601; } File::Find::find( { wanted => &process_file, no_chdir => 1 }, $from );
  4. 4. sub process_file { my $log_prefix = epoch_to_iso8601(time); my $file_name = $File::Find::name; my $postfix = File::Spec->abs2rel( $file_name, $from ); my $destiny = File::Spec->join( $to, $postfix ); die "$file_name isn't readable" if not -r $file_name; if ( -d $file_name ) { return if -d $destiny; mkdir($destiny) or die "Can't mkdir $destiny: $!"; say "$log_prefix: Directory $destiny created"; } else { ...
  5. 5. ... } else { my $original_utime = ( stat $file_name )[9]; if ( my @files = bsd_glob(&quot;$destiny.BACKUP.*&quot;) ) { my $last_path = ( sort @files )[-1]; return if $original_utime < ( stat $last_path )[9]; } $destiny = &quot;$destiny.BACKUP.&quot; . epoch_to_iso8601($original_utime); system( 'cp', $file_name, $destiny ) and die &quot;Can't cp $file_name $destiny&quot;; say &quot;$log_prefix: cp $file_name $destiny&quot;; } }
  6. 6. Modern::Perl use v5.10; use strict; use warnings; To use perl5i::2;
  7. 7. autodie mkdir($destiny) or die &quot;Can't mkdir $destiny: $!&quot; ; To mkdir($destiny); Error Example: Can't mkdir('backup/pippo'): Permission denied at perl5i/example.pl line 26
  8. 8. autodie system('cp', $file_name, $destiny) and die &quot;Can't cp $file_name $destiny&quot; ; To system('cp', $file_name, $destiny); Error example: &quot;cp&quot; unexpectedly returned exit value 1 at (eval 67) line 13 at perl5i/example.pl line 37
  9. 9. time to Object my $log_time_prefix = epoch_to_iso8601(time()) ; To my $log_time_prefix = time()->iso8601 ;
  10. 10. stat to Object my $original_utime = ( stat($file_name) )[9] ; To my $original_utime = stat($file_name)->mtime ;
  11. 11. stat to Object return if $original_utime < ( stat($last_path) )[9] ; To return if $original_utime < stat($last_path)->mtime ;
  12. 12. function/method signature sub epoch_to_iso8601 { my ( $time ) = @_; To func epoch_to_iso8601( $time ) {
  13. 13. autobox my $last_path = ( sort @existing_files )[-1] ; To my $last_path = @existing_files->maxstr ;
  14. 14. Extend Scalars func epoch_to_iso8601($time) { ... epoch_to_iso8601($original_utime); To func SCALAR::epoch_to_iso8601($time) { ... $original_utime->epoch_to_iso8601;
  15. 15. use v5.10; use strict; use warnings; use File::Find; use File::Glob 'bsd_glob'; use DateTime; my ( $from, $to ) = @ARGV; die &quot;Directory $from not found&quot; if not -d $from; die &quot;Directory $to not found&quot; if not -d $to; sub epoch_to_iso8601 { my ( $time ) = @_; return DateTime->from_epoch(epoch => $time)->iso8601; } File::Find::find( { wanted => &process_file, no_chdir => 1 }, $from );
  16. 16. use perl5i::2; use File::Find; use File::Glob 'bsd_glob'; use DateTime; my ( $from, $to ) = @ARGV; die &quot;Directory $from not found&quot; if !-d $from; die &quot;Directory $to not found&quot; if !-d $to; func SCALAR::epoch_to_iso8601( $time ) { DateTime->from_epoch(epoch => $time)->iso8601 } File::Find::find( { wanted => &process_file, no_chdir => 1 }, $from );
  17. 17. sub process_file { my $log_prefix = epoch_to_iso8601(time()); my $file_name = $File::Find::name; my $postfix = File::Spec->abs2rel( $file_name, $from ); my $destiny = File::Spec->join( $to, $postfix ); die &quot;$file_name isn't readable&quot; if not -r $file_name; if ( -d $file_name ) { return if -d $destiny; mkdir($destiny) or die &quot;Can't mkdir $destiny: $!&quot;; say &quot;$log_prefix: Directory $destiny created&quot;; } else { ...
  18. 18. func process_file() { my $log_prefix = time->iso8601 ; my $file_name = $File::Find::name; my $postfix = File::Spec->abs2rel( $file_name, $from ); my $destiny = File::Spec->join( $to, $postfix ); die &quot;$file_name isn't readable&quot; if not -r $file_name; if ( -d $file_name ) { return if -d $destiny; mkdir($destiny); say &quot;$log_prefix: Directory $destiny created&quot;; } else { ...
  19. 19. ... } else { my $original_utime = ( stat $file_name )[9] ; if ( my @files = bsd_glob(&quot;$destiny.BACKUP.*&quot;) ) { my $last_path = ( sort @files )[-1]; return if $original_utime < ( stat $last_path )[9] ; } $destiny = &quot;$destiny.BACKUP.&quot; . epoch_to_iso8601($original_utime) ; system( 'cp', $file_name, $destiny ) and die &quot;Can't cp $file_name $destiny&quot;; say &quot;$log_prefix: cp $file_name $destiny&quot;; } }
  20. 20. ... } else { my $original_utime = stat($file_name)->mtime ; if ( my @files = bsd_glob(&quot;$destiny.BACKUP.*&quot;) ) { return if $original_utime < stat( @files->maxstr )->mtime ; } $destiny = &quot;$destiny.BACKUP.&quot; . $original_utime->epoch_to_iso8601 ; system( 'cp', $file_name, $destiny ); say &quot;$log_prefix: cp $file_name $destiny&quot;; } }
  21. 21. Other features
  22. 22. Meta Object # the object's class my $class = $obj->mo->class; # its parent classes my @isa = $obj->mo->isa; # the complete inheritance hierarchy my @complete_isa = $obj->mo->linear_isa; # the reference type of the object my $reftype = $obj->mo->reftype;
  23. 23. Packages <ul><li>use Child;
  24. 24. use English qw( -no_match_vars );
  25. 25. use File::chdir; # gives you $CWD # representing the current working # directory
  26. 26. use Time::y2038; # gmtime() and # localtime() will now safely work with # dates beyond the year 2038 and before # 1901.
  27. 27. use IO::Handle;
  28. 28. use Try::Tiny; </li></ul>
  29. 29. Q / A

×