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

My question is there a simpler way to take the output from the printf statement so that it can be used with the mkdir command? My previous attempts resulted with the return status of 1 being used with the mkdir command. The below code does work.
sub dirname () { @date = (); @date = localtime(); $time = localtime(); unlink<$now>; open(NOW, "> $now") or die "LOG->Can't open $now: $!\n"; NOW->autoflush(1); printf NOW ("%02d-%02d-%04d", $date[4]+1, $date[3], $date[5]+1900) +; close(NOW); open(TMP, "< $now") or die "LOG->Can't open $now: $!\n"; while($_ = <TMP>) { if ($_ =~ s/^(\d{2}-\d{2}-\d{4})$/$1/) { $dirname = ""; $dirname = $1; if ( -d "$path/$dirname") { last; } else { mkdir("$path/$dirname", 0777) or die "LOG->Error is: $!\n" +; } } else { print LOG "$time Program Error 8 - $_\n"; email(); exit 1; } } close(TMP); }

Replies are listed 'Best First'.
Re: mkdir & date command
by radiantmatrix (Parson) on Nov 08, 2004 at 18:55 UTC

    sprintf takes the same arguments as printf (except filehandle) but returns the formatted string instead of printing it.

    In fact,

    printf FH "%s ==> %i", $string, $int;
    Is equivalent to:
    print FH sprintf("%s ==> %i", $string, $int);
    So you could easily use
    mkdir(sprintf($format, @stuff));

    Which would be much more elegant than writing to a file, then reading it back.

    radiantmatrix
    require General::Disclaimer;
    "Users are evil. All users are evil. Do not trust them. Perl specifically offers the -T switch because it knows users are evil." - japhy
      Thank you... it works like a champ!
Re: mkdir & date command
by Fletch (Bishop) on Nov 08, 2004 at 18:51 UTC