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.


In reply to Re: backticks execution "inside" variable (string) by haukex
in thread backticks execution "inside" variable (string) by richard.sharpe

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.