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

Hello!
I'm trying to automatically create a subdirectory with today's date and output the xxx files (this is done by a different program, I just pass on the parameters).
This is what I have (with help from "haukex")

#!/usr/bin/env perl use warnings; use strict; use IPC::System::Simple; my $savedir = foldername; system('GPIBShot.EXE','/HIDE','/12','/COLOR',"/NAME=$savedir/TEST1",'/ +SCREEN');


A few questions that I wasn't able to find online (or I'm just miserable at googling)

1. Is there a way to fetch today's date and assign it to my "savedir" variable?

2. Assuming I did (or didn't) get the variable set up above, how do I pass it down using "system" in one of the parameters? The paramter "/NAME=" is where I set the directory, and the program manual doesn't specify whether sending a dir will work or not, which is what I'm trying to test eventually. But I'm getting an error from Perl as the following. Bareword "foldername" not allowed while "strict subs" in use at test2.pl

Replies are listed 'Best First'.
Re: How to pass directory variable using IPC::System::Simple
by ablanke (Monsignor) on Jul 25, 2016 at 07:57 UTC
    1. Yes:
    my @t = ( localtime(time) )[ reverse( 3 .. 5 ) ]; #get year month and +day from current localtime $t[0] += 1900; # see localtime doc for details $t[1]++; # see localtime doc for details my $savedir = '/folder/name/' . sprintf( '%04d%02d%02d', @t ); say $savedir;
    /folder/name/20160725
    2. In this case the scalar $savedir should be an string, so you have to quote it. (e.g. my $savedir = 'foldername';

      gracias!!

Re: How to pass directory variable using IPC::System::Simple
by Corion (Patriarch) on Jul 25, 2016 at 07:36 UTC

    Why do you import IPC::System::Simple? Maybe that module can export a subroutine foldername?

    I recommend that you construct and print the filename before trying to launch the external program. For example:

    my $date = '2016-07-25'; my $filename = "$savedir/TEST-$date.jpg"; system(...);

    To get at the current date, the easiest approach is to use the strftime function from the POSIX module:

    my $date = strftime '%Y-%m-%d', localtime; ...

      I honestly just used the Simple module just cuz someone told me to do so... literally.
      That's all I'm trying to do and I have no idea how to call the said command line without the module (*screams help*)
      Thanks for the POSIX module! I'll look that up for sure

        Hi bill5262,

        I made a mistake in my node, which I've now fixed, apologies!

        The line "use IPC::System::Simple;" should be "use IPC::System::Simple 'system';", because otherwise it has no effect, meaning that the default system function is called instead of IPC::System::Simple's version of system which has better error handling.

        Regards,
        -- Hauke D