ExifTool Forum

ExifTool => Newbies => Topic started by: issam9973 on February 10, 2021, 10:58:00 AM

Title: Defining sub routine / global variables within .Exiftool_Config
Post by: issam9973 on February 10, 2021, 10:58:00 AM
Hi, I'm newbie to perl / exiftool !

Is it possible to define a sub routine / global variables within .Exiftool_Config then use them in the user-defined composite tags section?

Here is a toy example. Please help me correct the structure in case the above is possible. (Note: i now the toy example below can be done without subroutine but it's not the point :)!)


#---------------------------------
# File:   exiftool.config
#---------------------------------

# global variable
my $smallestDate = '1960:01:01 00:00:00';
my $largestDate =  '2960:01:01 00:00:00';

# Sub routine / function
sub mySubRoutine {
my ($res) = @_ ;
# Do some calculations
# ...
  return $res;
}

%Image::ExifTool::UserDefined = (
   'Image::ExifTool::Composite' => {

MyDateTimeMax => {
  Desire => {
  0 => 'DateTimeOriginal',
  1 => 'FileModifyDate',2 => 'FileAccessDate',3 => 'FileCreateDate',
  4 => 'FileName',
  # other date tags ...
  }, 
  ValueConv => q{
    my $r = $smallestDate;
    foreach (@val) {
    next if (not defined $_);
  $tmp = mySubRoutine $_; 
    $r = ($r lt $tmp) ? $tmp : $r;
    }; 
    return $r;
  },
  },
},
);
1; # end



Title: Re: Defining sub routine / global variables within .Exiftool_Config
Post by: StarGeek on February 10, 2021, 11:17:45 AM
For a subroutine, yes.  You can even use the subroutine as an helper function on the command line, similar to DateFmt helper function (https://exiftool.org//exiftool_pod.html#Helper-functions).

See my user defined tag in this thread (https://exiftool.org/forum/index.php?topic=8705.msg44673#msg44673) for an example.  The MyNormalize subroutine in that config file can be used on the command line to convert accented characters such as à , ê , ó into the base characters a, e, o.  Yeah, I'm a silly American who doesn't like to deal with those characters.

A global variable is a bit more difficult.  You can't access it directly if you're using ValueConv => q{ but can if you use ValueConv => sub {.  I may be wrong the first part.  I asked this once and can't remember the response.  It's in the forum someplace and I'll link if I can find it.

Using ValueConv => sub { does require a bit more work.  Take a look at the picasa_faces.config (https://github.com/exiftool/exiftool/blob/master/config_files/picasa_faces.config) for some examples.

The config files (https://github.com/exiftool/exiftool/tree/master/config_files) can provide more examples and there are plenty more in the forums here.  I usually use this google search (https://www.google.com/search?q=site:exiftool.org%20user+defined+tag) to find them.
Title: Re: Defining sub routine / global variables within .Exiftool_Config
Post by: StarGeek on February 10, 2021, 11:20:07 AM
Quote from: StarGeek on February 10, 2021, 11:17:45 AM
A global variable is a bit more difficult.  You can't access it directly if you're using ValueConv => q{ but can if you use ValueConv => sub {.  I may be wrong the first part.  I asked this once and can't remember the response.  It's in the forum someplace and I'll link if I can find it.

Here's (https://exiftool.org/forum/index.php?topic=9596.0) where I previously asked about this.  It is possible.