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 |