Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Portably transforming a string to a valid filename

by chipmunk (Parson)
on Feb 20, 2001 at 10:10 UTC ( [id://59592]=note: print w/replies, xml ) Need Help??


in reply to Portably transforming a string to a valid filename

Here's a very straightforward solution. Instead of %, it uses _ as the escape character, since I think _ should be safe just about anywhere. (If not, maybe -?)

Anyway, my thinking is, escape almost everything (including the escape character, of course). :)

sub encode_title { my($title) = @_; $title =~ s/([^A-Za-z0-9])/sprintf '_%02X', ord $1/ge; return $title; } sub decode_title { my($title) = @_; if ($title =~ /[^A-Za-z0-9_]/ or $title =~ /_(?![A-F0-9]{2})/) { return undef; } $title =~ s/_(..)/chr hex $1/ge; return $title; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://59592]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-25 20:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found