FAQ 5.15 How can I translate tildes (~) in a filename?

Discussion in 'Perl Misc' started by PerlFAQ Server, Jan 28, 2011.

  1. This is an excerpt from the latest version perlfaq5.pod, which
    comes with the standard Perl distribution. These postings aim to
    reduce the number of repeated questions as well as allow the community
    to review and update the answers. The latest version of the complete
    perlfaq is at http://faq.perl.org .

    --------------------------------------------------------------------

    5.15: How can I translate tildes (~) in a filename?

    Use the <> ("glob()") operator, documented in 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
    "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;



    --------------------------------------------------------------------

    The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
    are not necessarily experts in every domain where Perl might show up,
    so please include as much information as possible and relevant in any
    corrections. The perlfaq-workers also don't have access to every
    operating system or platform, so please include relevant details for
    corrections to examples that do not work on particular platforms.
    Working code is greatly appreciated.

    If you'd like to help maintain the perlfaq, see the details in
    perlfaq.pod.
    PerlFAQ Server, Jan 28, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,005
    Travis Murray
    Aug 24, 2004
  2. =?Utf-8?B?anRob3JuYnk=?=

    Translate a filename to a URL

    =?Utf-8?B?anRob3JuYnk=?=, Apr 11, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,950
    Brock Allen
    Apr 11, 2005
  3. Replies:
    1
    Views:
    1,437
    Roland de Ruiter
    Jun 15, 2006
  4. ernie367

    Tildes and accents

    ernie367, Feb 21, 2006, in forum: C++
    Replies:
    2
    Views:
    418
    Victor Bazarov
    Feb 21, 2006
  5. PerlFAQ Server
    Replies:
    0
    Views:
    140
    PerlFAQ Server
    Apr 29, 2011
Loading...

Share This Page