Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Want to be a Perl contributor? It's easy. Just fix this FAQ answer. Update: An by update, I mean rewrite it, not tell me how to rewrite it. :)

perlfaq5 has an answer for "How can I translate tildes (~) in a filename?". Looking at the code in the answer, I know it must break in a lot of places since it relies either on getpwnam or environment variables. Who can come up with a good answer that is more robust? I'm especially interested in any module-fu that can be added. You don't have to necessarily keep any of the existing answer. If you replace the answer, you get your name listed right next to it. :)

You can post your answer here or to the mailing list in perlfaq.

Here's the source (although you can get at the svn repository too by looking at the instructions in perlfaq:

=head2 How can I translate tildes (~) in a filename? X<tilde> X<tilde expansion> Use the E<lt>E<gt> (C<glob()>) operator, documented in L<perlfunc>. Versions of Perl older than 5.6 require that you have a shell installed that groks tildes. Later versions of Perl have this feature built in. The C<File::KGlob> module (available from CPAN) gives more portable glob functionality. Within Perl, you may use this directly: $filename =~ s{ ^ ~ # find a leading tilde ( # save this in $1 [^/] # a non-slash character * # repeated 0 or more times (0 means me) ) }{ $1 ? (getpwnam($1))[7] : ( $ENV{HOME} || $ENV{LOGDIR} ) }ex;
--
brian d foy <brian@stonehenge.com>
Subscribe to The Perl Review

In reply to Improve perlfaq5: How can I translate tildes (~) in a filename? by brian_d_foy

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2014-12-29 14:58 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (191 votes), past polls