ExifTool Forum

ExifTool => The Image::ExifTool API => Topic started by: outdoormagic on April 20, 2021, 08:29:32 AM

Title: Efficiency and new Image::ExifTool
Post by: outdoormagic on April 20, 2021, 08:29:32 AM
Hi everyone,

Embarrassing question from a rookie. What is the best / correct way to call ExifTool within a larger perl script?

use Image::ExifTool;
loads the module. So far, so good.  :)

my $info = $exifTool->ImageInfo( $filename );
gets the info from a file and puts it in a hash.

When I do this in a loop over several files, I call
undef $info; # done with the file when I'm done so that I start (presumably) with a fresh hash every time. I then call
$info = $exifTool->ImageInfo( $filename );  # get the information for the next file
and so on.

But what does my $exifTool = new Image::ExifTool; do?

Can I call it once at the beginning of the script, as in
use Image::ExifTool
my $exifTool = new Image::ExifTool;

and leave it alone for multiple files? Should I call undef $exifTool before each new file as I do with $info??

On a related note, I have legacy shell snippets that I am gradually converting to perl. For now, I have calls such as
my @dataOfInterest = `exiftool ...`;
which feels like a terrible idea. Should I use -StayOpen or the @_ option? Or will the fact that I'm calling the shell from Perl mess things up?

Thank you for any insights and recommendations.

Paul
Title: Re: Efficiency and new Image::ExifTool
Post by: Phil Harvey on April 20, 2021, 11:52:32 AM
Hi Paul,

You should only create one Image::ExifTool object, and use it for all the files you want to read.

And you don't need to delete the old $info -- you can let the Perl garbage collection handle that.

If your shell scripts are calling exiftool repeatedly, then you may speed them up by using -stay_open, but it will be a lot of work, and probably easier to convert to using Perl and the API instead.

- Phil
Title: Re: Efficiency and new Image::ExifTool
Post by: outdoormagic on April 20, 2021, 04:37:06 PM
Hi Phil,

Perfect! I'll lean out my Perl scripts and convert shell scripts to Perl.

Thank you so much for an amazing tool and the ongoing support.

Paul