Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

variable inside a string inside back ticks

by phamalda (Novice)
on Jul 30, 2017 at 16:01 UTC ( #1196310=perlquestion: print w/replies, xml ) Need Help??

phamalda has asked for the wisdom of the Perl Monks concerning the following question:

I have a filename that is formatted as electric_inventory_WE_20170730_195758.dat. There is a similar file created each day. The time created may vary. I need to capture the filename with today's date and not worry about the time and end with a .dat extension.

I have created a variable for today's date. I need to use this variable inside a string inside back ticks. Something like this:

my $today = `date "+%Y%m%d"`;

my $filename = `ls electric_inventory_WE_$today*.dat`;

I did review #828922 but it didn't quite get me what I needed. I cannot find a way to get this variable to work inside a string inside the back ticks. Any help would be greatly appreciated.

  • Comment on variable inside a string inside back ticks

Replies are listed 'Best First'.
Re: variable inside a string inside back ticks
by haukex (Bishop) on Jul 30, 2017 at 16:12 UTC

    I will answer your question below, but first, I strongly recommend not to use backticks to run shell commands for things you can do in Perl! For date/time handling, I prefer DateTime, but in this case you can probably just use the core module Time::Piece, plus the Perl builtin glob (Update: Make sure to read File::Glob, though, as it's got some caveats in regards to whitespace, which is why I use File::Glob ':bsd_glob' in the code):

    use warnings; use strict; use Time::Piece; use File::Glob ':bsd_glob'; my $today = localtime->strftime('%Y%m%d'); my $filename = glob('electric_inventory_WE_'.$today.'*.dat');

    I wrote about backticks and the potential security issues with them here.

    The problem you are having with the code you showed can be solved by looking at item 3 on the Basic debugging checklist (unexpected whitespace), and using chomp on those variables.

Re: variable inside a string inside back ticks
by Discipulus (Abbot) on Jul 30, 2017 at 16:16 UTC
    Hello phamalda,

    there are better ways to grab dates and filenames (PS infact haukex was faster than me and shown you better ways), whithout shelling out..

    Anyway i cannot confirm your issue: it works as expected; you must have some other errors

    echo blur > 20170730_195758.dat # warning windows double quotes! perl -e "my $today = '20170730'; print `ls 20170730*.dat`" 20170730_195758.dat

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      i cannot confirm your issue

      same mistake as OP

      perl -e 'print ">>".`ls 20170730*.dat`."<<"' >>20170730_195758.dat <<
Re: variable inside a string inside back ticks
by Anonymous Monk on Jul 30, 2017 at 16:22 UTC
    my $today = `date "+%Y%m%d"`;
    chomp $today;
    my $filename = `ls electric_inventory_WE_$today*.dat`;

    But really, try and follow hakuex's advice
Re: variable inside a string inside back ticks
by Anonymous Monk on Jul 31, 2017 at 13:33 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1196310]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2022-01-24 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (64 votes). Check out past polls.

    Notices?