ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: lei on May 09, 2025, 01:07:29 AM

Title: Does -if support tag interpolation with static suffixes like -p does?
Post by: lei on May 09, 2025, 01:07:29 AM
Hi Experts,

I'm testing how tag interpolation works inside the -if option, and I've run into a case that doesn't behave as expected.

Consider this command:

exiftool -filename -if "${filename;$_=substr($_,0,2)}export eq 'mmexport'" .

I expected this to behave like -p, where ${filename;...} gets interpolated first, and the resulting string (e.g. "mm") combines with the static "export" to form "mmexport", which would then be compared to "mmexport".

This kind of template works fine with -p:

exiftool -p "${filename;$_=substr($_,0,2)}export" .

It correctly outputs mmexport.

According to the documentation for -if, Note 3 says:

QuoteTags in the string are interpolated in a similar way to -p before the expression is evaluated. In this interpolation, $/ is converted to a newline and $$ represents a single "$" symbol...

So I assumed this means the entire string — including both tag references and static text — would be interpolated before the expression is evaluated.

However, the behavior seems inconsistent with that. Is this limitation expected? Or should the -if option support the same kind of full interpolation as -p?

(Just to clarify: I do know how to achieve the same logic using something like $filename =~ /^mmexport/. I'm only testing interpolation behavior.)

Thanks in advance!
Title: Re: Does -if support tag interpolation with static suffixes like -p does?
Post by: greybeard on May 09, 2025, 02:44:41 AM
exiftool -filename -if "${filename;$_=substr($_,0,2)}.'export' eq 'mmexport'" .
exiftool -filename -if "${filename;$_=substr($_,0,2).'export'} eq 'mmexport'" .
Title: Re: Does -if support tag interpolation with static suffixes like -p does?
Post by: Phil Harvey on May 09, 2025, 09:13:36 AM
Greybeard has the answer.  A bare word (ie. "export") requires quoting in a Perl expression.

- Phil
Title: Re: Does -if support tag interpolation with static suffixes like -p does?
Post by: lei on May 09, 2025, 01:21:35 PM
Thanks, Greybeard and Phil — I appreciate the responses.

Just to clarify: I'm not asking how to concatenate strings using Perl syntax (like with the . operator). I do understand how to do that, and that approach works.

What I'm trying to test is whether template-style tag interpolation, as described in Note 3 of the documentation, works inside -if the same way it does with -p.

For example, this works correctly with -p:

exiftool -p "${filename;$_=substr($_,0,2)}export"

It outputs mmexport, as expected — that is, the tag value is interpolated, then the literal text follows immediately.

So I tried the equivalent with -if:

exiftool -filename -if "${filename;$_=substr($_,0,2)}export eq 'mmexport'" .

But this didn't work. I also tried using 'export' as a quoted literal (without a dot operator), but still no luck.

So my question is:

Is this kind of direct interpolation — where a tag expression is immediately followed by literal text, like in -p — supposed to be supported in -if? Or is it a known limitation that -if doesn't perform full interpolation like -p does?

Thanks again — I'm just trying to understand the intended behavior.
Title: Re: Does -if support tag interpolation with static suffixes like -p does?
Post by: Phil Harvey on May 09, 2025, 01:27:14 PM
The template-style interpolation is similar for -p and -if arguments, but for -if the resulting string is evaluated as a Perl expression, and for -p it is not.

When I say "is similar", I think maybe it is the difference that is confusing you, but this isn't something that most people would think about.  Technically, the tag-name variables themselves are converted to a Perl variable for a -if statement instead of interpolating their values directly into the string.

To be very specific (causual users please stop reading here), in this command:

exiftool -p "$filename" test.jpg

the string generated is "test.jpg".

But in this command:

exiftool -if "$filename" test.jpg

the string that is evaluated is actually "$info{'filename'}", which accesses an internal %info variable when it is evaluated.


- Phil
Title: Re: Does -if support tag interpolation with static suffixes like -p does?
Post by: lei on May 10, 2025, 02:31:05 PM
Thanks, Phil. I haven't fully grasped all the internals you described, but I think I now understand the key difference and why my original expression didn't work. Also thanks to greybeard for the first reply!