• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Perl5i
 

Perl5i

on

  • 694 views

 

Statistics

Views

Total Views
694
Views on SlideShare
692
Embed Views
2

Actions

Likes
1
Downloads
0
Comments
0

2 Embeds 2

http://www.linkedin.com 1
https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Perl5i Perl5i Presentation Transcript

    • Perl5i Marcos Rebelo (oleber@gmail.com)
    • Tribal knowledge
    • 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 );
    • 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 { ...
    • ... } 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;; } }
    • Modern::Perl use v5.10; use strict; use warnings; To use perl5i::2;
    • 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
    • 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
    • time to Object my $log_time_prefix = epoch_to_iso8601(time()) ; To my $log_time_prefix = time()->iso8601 ;
    • stat to Object my $original_utime = ( stat($file_name) )[9] ; To my $original_utime = stat($file_name)->mtime ;
    • stat to Object return if $original_utime < ( stat($last_path) )[9] ; To return if $original_utime < stat($last_path)->mtime ;
    • function/method signature sub epoch_to_iso8601 { my ( $time ) = @_; To func epoch_to_iso8601( $time ) {
    • autobox my $last_path = ( sort @existing_files )[-1] ; To my $last_path = @existing_files->maxstr ;
    • Extend Scalars func epoch_to_iso8601($time) { ... epoch_to_iso8601($original_utime); To func SCALAR::epoch_to_iso8601($time) { ... $original_utime->epoch_to_iso8601;
    • 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 );
    • 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 );
    • 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 { ...
    • 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 { ...
    • ... } 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;; } }
    • ... } 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;; } }
    • Other features
    • 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;
    • Packages
      • use Child;
      • use English qw( -no_match_vars );
      • use File::chdir; # gives you $CWD # representing the current working # directory
      • use Time::y2038; # gmtime() and # localtime() will now safely work with # dates beyond the year 2038 and before # 1901.
      • use IO::Handle;
      • use Try::Tiny;
    • Q / A