In this, the second in a series of challenges (see the first such challenge), the goal is to explore the language in ways to create a filename. Inspired by a brief conversation on the CB where thezip suggested the mundane "sprintf is your friend," I'm looking for any and all ways to create a filename.

As in the previous challenge, the wording is deliberately vague. You can take this to mean anything you want, as long as the end result is the name of a file. The file may exist, or it may not exist, or its existence may be indeterminate. The choice is up to you.

Entries need not be serious - they can be exploratory (as davido pointed out last time, exporing the corners of the language is sometimes valuable), but pros and cons should be listed. Sometimes an off-beat approach is just what is needed, and knowing the pros and cons of an approach is vital in such evaluation. And, in the context of this challenge, it proves you know what you're doing rather than just copying and pasting it :-)

As to some examples:

# thezip's method: $fn = sprintf "%s.%04d-%02d-%02d-%02d%02d%02d.ext", $base_name, (localtime)[5,4,3,2,1,0];
Straight-forward, though there's a bit more work to get natural numbers for year and month. strftime would probably be better here.
# my method (from CB): $fn = ''; { open my $fh, '>', \$fn; print $fh $base_name, '.'; print $fh strftime("%F-%T"); }
pro: we're preparing this for open, so what could be more natural than using open for that? :-)
con: um, don't ever do this?

If you have some standard tools, even if wacky, esoteric, domain-specific, please share them. Or if you just are going for bonus points for obtuseness, please do. I managed to learn a lot from last time (including IO::All, the "cat $file |" trick for too-large-for-perl files, using Win32::OLE on Windows, though not necessarily in this order of usefulness ;-)), and I noticed others did as well, so don't be afraid to add your trick - you won't know who will find it helpful, even as an eye-opener, unless you do.


In reply to TIMTOWTDI Challenge: Create a filename by Tanktalus

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.