From b947df75ee544db212d6ffb8994d2fb07754fa26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Golja?= Date: Fri, 28 Oct 2016 08:30:52 +0200 Subject: [PATCH] Trying to figure out how this File::Spec and Cwd thing works. --- symlink-images-by-aspect-ratio.pl | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/symlink-images-by-aspect-ratio.pl b/symlink-images-by-aspect-ratio.pl index 4f09c3c..1322fb5 100755 --- a/symlink-images-by-aspect-ratio.pl +++ b/symlink-images-by-aspect-ratio.pl @@ -5,13 +5,16 @@ use strict; use File::Find::Wanted; use File::Basename; +use Cwd qw/ realpath abs_path /; +use File::Spec; +use File::Spec::Functions; use Image::EXIF; use Data::Dumper::Simple; use Math::Round; my $conf = { - src_dir => "./pics", - dst_dir => "./links", + src_dir => "pics", + dst_dir => "links", }; # verify the source dir @@ -95,7 +98,8 @@ print "Aspect ratio information for all images (aspect ratio => number of occure print Dumper $ars; print "\n"; -# Create destination directories +# Create destination directories. TODO: use something smarter than just string +# concatenation. print "Creating destination directories: "; foreach my $d (keys %$ars) { my $dirname = $conf->{dst_dir} . "/" . $d; @@ -109,14 +113,19 @@ print "Symlinking source files into destination directories.\n"; foreach my $f (keys %$files) { next unless exists $files->{$f}->{ar}; - # construct the symlink name + # Construct the symlink path. TODO: use something smarter than just string + # concatenation. my $name = basename($f); my $ar = $files->{$f}->{ar}; - my $dest = $conf->{dst_dir} . "/" . $ar . "/" . $name; + my $symlink = $conf->{dst_dir} . "/" . $ar . "/" . $name; + + # Costruct the relative path that the symlink points to + my $path = File::Spec->abs2rel(realpath($f), realpath($symlink)); + + print "f='$f' symlink='$symlink' path='$path' abs_path(f)='" . abs_path($f) . "'\n"; # symlink - print "$f -> $dest\n"; - symlink($f, $dest); + print "symlink($path, $symlink);\n"; } # vim: set ts=4 sw=4 et cc=80: