http://qs321.pair.com?node_id=123543


in reply to Re: convert windows path
in thread convert windows path

$var =~ tr!\\!/! didn't work - this just split out the \ slashes. the problem seems to be that the forward slashes in the variable are not interpreted as such so c:\dir would not work whereas we know c:\\dir would be interpreted as c:\dir and would work. i think i need to find a way of returning the path from my html form with \\ instead of \ each time. i wonder if cgi.pm has an ability to convert paths?

Replies are listed 'Best First'.
Re: Re: Re: convert windows path
by George_Sherston (Vicar) on Nov 06, 2001 at 15:02 UTC
    If I'm right in my guess about what you're trying to do, then the answer is "yes, CGI.pm does do it all for you". That's to say, if you're dealing with form input from a form that has a tag like <INPUT TYPE="FILE" NAME="FILENAME"> then if you do
    my $fh = param('FILENAME'); while (<$fh>) { do_stuff_with($_); # manipulate the file line by line }
    CGI.pm will deal with the path in the appropriate way - at least, that's been my experience when receiving input from browsers on Windows systems and processing it on a Linux system.

    (BTW you might want to use the upload() method, but as this comes only with later versions of CGI.pm I mention the old-fashioned way, in case you are stuck with an annoying ISP.)

    CGI.pm does the thinking so you can spend more time drinking beer :)

    George Sherston
      i've think i have confused the situation, the problem is some browsers submit the full path whereas some submit just the filename. when the full path is submitted i can't work out how to get the filename out. eg filename from c:\somedir\file.ext. perl doesn't let me search & replace those \ slashes when they are on their own.
        If you're just trying to extract a file from a path then something like
        $var =~ /([^\\\/]*)$/ and $file = $1
        should work. (I'm sure that there's a FAQ on this). Or you might want to try one of the File::Path modules, but that's probably overkill.
        So... what? you want to get a scalar var containing the filename? So you can print it out or something?

        If that's what you want, There's A Lot More Than One Way To Do It. Don't let anyone around here hear you say "Perl doesn't let me search and replace..." ! If anyone can search and replace it's Perl.

        Assuming you have
        my $filepath = 'c:\somedir\file.ext'
        and you want to get
        $filename eq 'file.ext'
        then
        $filepath =~ /([\w|\.]*)$/; my $filename = $1;
        Oughta do it - i.e. capture the longest possible string at the end of the filepath that is either a word character (alpha-num or _) or a dot. Or if you think there might be non-word characters in the file name, $filepath =~ /([^\\|^\/]*)$/; - capture the longest string at the end that doesn't have either a "\" or a "/".

        I'm no regex guru, so there may be obscure pitfalls in either of these, or a neater way to do it... but that's what I'd do.

        Of course, I may have completely mis-understood your question again :)

        George Sherston
Re: Re: Re: convert windows path
by Tyke (Pilgrim) on Nov 06, 2001 at 14:55 UTC
    I'm not sure I understand the problem.
    What does your variable really contain? What is the length of c:\dir? 5 or 6? Can you post a snippet?

    If you need a way to escape special characters you can use the quotemeta built-in.

    I don't use CGI so I'm afraid that I can't help you there.