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

I'll try to keep this simple, I have a web server where user's drop pdf's into a folder to be display'd on the web. when the cgi is launched, it reads the contents of the folder and then created a dynamic web page based on that. so a file may be called "1/25/99-Bruck.pdf" (Note the "/") and the url would be somesite.org/1/25/99-Bruck.pdf Is their any way to pack a file name with web friendly string. Example " " would be "%20" skreuzer@condenast.com

Replies are listed 'Best First'.
Re: Packing HTML Paths
by Beatnik (Parson) on May 22, 2001 at 20:58 UTC
    check uri::escape...
    update:
    #!/usr/bin/perl -w #-w courtesy of that nitpicker ! :) use strict; use URI::Escape; my $string = "valid / not valid \@ \$ ' "; my $safe = uri_escape($string); print $safe;
    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
(Ovid) Re: Packing HTML Paths
by Ovid (Cardinal) on May 22, 2001 at 21:41 UTC

    The aforementioned example doesn't actually convert the forward slash. You could try URI escaping everything your file system disallows in a filename. On a Windows system, that would be \/:*?"<>|. The following shows how you can get valid, but funky looking filenames:

    use strict; use URI::Escape; my @tests = ( "1/15/99-Bob.pdf", "test.txt", '\\/:*?"<>|' ); for ( @tests ) { print uri_escape( $_, ':\\\\\/*?"<>|' ), "\n"; }

    That outputs the following:

    1%2F15%2F99-Bob.pdf test.txt %5C%2F%3A%2A%3F%22%3C%3E%7C

    This may not be your best options, but have you thought of creating directories like the following?

    wwwroot -- documents --- 1 -- 25 -- 99 | -- 2 | -- 3

    You actual pdf could then be named -Bruck.pdf and the path would work fine. Of course, I'd probably put the year first, but it does make for a handy organizational structure if you'll be working with many files.

    One concern with your approach may be portability. Is that an issue? Do you want this to work with 8.3 filenames? Do all file systems allow for percent signs in filenames (though I don't know of any which don't).

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      If we're to use this as an organisational structure, I recommend using either european/continental date style (DD/MM/YY) or even better, the unnamed but alphabetically sorting style YYYY/MM/DD. This way, old stuff can easily be found and put on tape.

      Update: Of course, Ovid already said that. People who can read (and actually do) have an advantage I guess.

        Just general rambles about your reply (which is merely a cheap way to mention something I did in VBScript :)

        I prefer YY/MM/DD or YYYY/MM/DD since sorting is much easier. It may not seem like a big deal to Perl users, but imagine if others are forced to manipulate data in other languages, such as VBScript, which doesn't have native sorting.*

        In any event, even with Perl's robust sorting abilities, having a straight-forward sort on the dates is much easier to read than a lot of weird maps or greps or other things. Of course, if I have complex sorting, I usually stick it in a function with a descriptive function name.

        Cheers,
        Ovid

        * I kind of feel sorry for some of the VBScript programmers I left behind. I am so used to shift, pop, sort, and so many other handy tools to use with lists and arrays that I wrote a "Perl functions" library in VBScript. Much of my VBScript code read like "baby Perl" (though only in parts where I thought the overhead wouldn't kill a language that was not designed for such functions).

        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        It isn't unamed, it is ISO 8601. It is the internationally standardized date format (except the standard specifies that you use "-" instead of "/", which is a good idea as it is easier to read that way ["/" looks too much like a "1" to too many cases] and also doesn't require escaping).

        See international standard date format.

                - tye (but my friends call me "Tye")
Re: Packing HTML Paths
by traveler (Parson) on May 22, 2001 at 21:29 UTC
    If you are using HTML::Template you can use ESCAPE='URL' as an "argument" to TEMPL_VAR.