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

$file2 = param('file'); # A windows path is supposed to be filled here + (c:/foo/bar.txt) <br>print "$file2\n"; # c:foobar.txt is printed <br>$file2 =~ s[\\][/]g; # It's too late to substitute.

As you can see, this is quite a pickle. How do I fix this?

Replies are listed 'Best First'.
Re: / to \ substitution
by zodiac (Beadle) on May 31, 2000 at 16:52 UTC
    why don't you substitute before printing ?????
    $file2 = param('file'); # A windows path is supposed to be filled h +ere (c:/foo/bar.txt) $file2 =~ s%\\%/%g; # It's not too late to substitute here. $file2 =~ s%.*:%%; # also cut off c: print "$file2\n"; # c:foobar.txt is printed
    if you need to keep the original value you can do
    ($file3 = $file2) =~ s%what%ever%g;
      First of all, it is still too late to substitute.

      Second, why would I want to cut c: off?

      This solves nothing, I'm still left with an invalid path.

      -NM

RE: / to \ substitution
by mdillon (Priest) on May 31, 2000 at 06:03 UTC
    i don't know about embedding <FONT> tags in nodes. i'm against it. i happen to have installed Verdana, but i don't think this will degrade well for others. please use <CODE></CODE> tags.
Re: / to \ substitution
by comatose (Monk) on May 31, 2000 at 07:10 UTC

    Can you post more of your code? I don't see anything here that would cause the the problem you're having.

    Is this a CGI script? Are you using CGI.pm? Have you tried running it from the command line?

      That's about it. I am using CGI.pm. You can do something as simple as this and see the problem
      use CGI qw(:all); $file2 = param('file') unless $debug; print "$file2\n"; $file2 =~ s[\\][/]g;
      Then you can do "perl foobar.pl file=c:\foo\bar.txt". Observe what happens. -NM
        Actually, try this as well:
        use CGI qw(:all); $debug = 1; $file2 = param('file') unless $debug; $file2 = 'c:\foo\bar.txt' if $debug; print "$file2\n"; $file2 =~ s[\\][/]g;
        This debug thing really was a neat thing, btw.

        -NM

Re: / to \ substitution
by NeverMore (Acolyte) on May 31, 2000 at 06:20 UTC
    Sorry, I'll try to remember not to use them next time.
Re: / to \ substitution
by NeverMore (Acolyte) on May 31, 2000 at 06:24 UTC
    Sorry, I'll try to remember not to use them next time.
      no problem. see the Site How To for tips about stuff like linking, <CODE> tags, and getting square brackets ( [ ] ) to show up correctly in non-<CODE>.