in reply to backticks execution "inside" variable (string)

It depends:

Is it only ever going to be the date/time that you want to interpolate? Then I'd strongly recommend having the user provide a string in strftime format - either the core module Time::Piece, or perhaps DateTime (or even from POSIX).

Do you care about the potential security issues of your Perl script executing an arbitrary user-supplied command? If not, here's how I might have done it (I'm explicitly calling /bin/bash because of The problem of "the" default shell):

use IPC::System::Simple qw/capturex/; my $str = q{"filename_`date +%d%m%Y`.log"}; my $out = capturex('/bin/bash','-c',"echo -n $str");

Another potentially very risky (!) way to do it, if you want to execute arbitrary Perl code instead of the shell's syntax, is eval.

Otherwise, there might be CPAN modules to do interpolation and execution of commands in the same way the shell does, but I don't know of any off the top of my head. The AM post mentions templates, which might also be a potential generalized solution, depending on what kind of stuff you want to interpolate (typically, templates use some other characters instead of backticks for interpolation, but that's usually configurable).

Side notes:

it was just the first successful try how to force Perl not cutting filename after date

That's the shell doing that, not Perl. Also, in regards to your sample piece of code, see the caveats in To glob or not to glob, and see "open" Best Practices.

Replies are listed 'Best First'.
Re^2: backticks execution "inside" variable (string)
by richard.sharpe (Sexton) on Dec 20, 2019 at 13:26 UTC

    Hello Hauke,

    I am aware that using backticks may be risky (how much, depends on use case context), thank you for your cautions anyway, better measuring multiple times, than doing something ill-judged and then having problems.

    In my case, I am doing automated conversions of log monitoring policies from legacy monitoring system to correlation engine with prescribed correlation templates. Input policies and also correlation templates are using backticks to handle multi-file logs, and it is out of scope of my task, changing these prescribed input and output norms (it would be much more work and this is currently out of project scope), so adapted to these norms, and used backticks also internaly in my conversions and also when generating automated tests. Although currently only dates are in backticks, in general, in general there can be any meainingful command, so I am doing general solution, repeatedly usable also in future, not just covering dates. There is some level of trust, that backticks contents was verified in the past, and also during the analysis before doing automated conversion, and conversions and correlations are running under unprivileged user, so I see the risk level as acceptable in this case, and I am carefully thinking about potential "what ifs" and testing. My showcase was very shortened, removed from its context, so I was interested in very specific thing, not finding alternative solutions what to use instead of backticks, because I know you can't know all the requirements and limitations.

    It is not interesting fo me, finding "who did it" (shell or Perl), I saw, where was the problem (shell was in the service of Perl in this case, so I am in charge to solve this problem by Perl, so I am writing this in Perl forum instead of some shell user group).

    Thank you for all your tips anyway - currently having little time capacity to read thoroughly all articles you provided, I flied them with my eyes and bookmarked.

    May the Perl be with you.

    Richard