Matching/Substituting backslashes inside double qouted strings

TwoHumpsUp used Ask the Experts™
Hi Guys,

I'm in a dead end situation on a project i'm currently doing. The project is simple enough, users fill-up a form on a webpage and has an option of uploading a picture along with form. I've already done most of the data handling (save information to file and mail it) and it worked well, however, when i tried to get the filename from the win32-style file path of the uploaded picture, 'coz I found out that the backslashes are ignored, but it worked well for unix paths.

This problem really puzzled me. I've done quite a lot of experimenting but to no avail. Here's a sample code.

#!/usr/bin/perl -w

use strict;
use CGI qw(:all);
use MIME::Lite;

... initializations

if (my $file = upload('uploaded_file')) {
  my $info = uploadInfo($file) or die "$0: $!\n";
  .... some more code here
  ( $filename = $info->{"filename"} ) =~ s/^.+[\\|\/]//g;  # doesnt work, returns: C:My DocumentsMy Picsme_in_black.gif
  ... attach file to email with $filename as a filename

... rest of code


Here's a sample value taken using uploadInfo($file)

$info->{"filename"} = "C:\My Documents\My Pics\me_in_black.gif"


The string is DOUBLE-QUOTED by default, this is because the information is auto-generated by the upload handler of so the only way (I think) to get around it is to 'QUOTE' the darn string so the BACKSLASH will NOT be intrepreted as a start of an ESCAPE SEQUENCE but a literal backslash.

Now if anyone has ANOTHER WAY OF DOING IT, and I'm sure there is, it will be much appreciated.

Thanks in advance.

Hump UP!
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can simply replace the backslashed with slashes.
Paths with slashes instead of backslashes is AFAIK valid on all Windows versions. Below is code for doing that.

use strict;

$_ = 'c:\temp\myfile.txt';
print "Before : $_\n";
print "After : $_\n";

I always use the following code for my uploaded files:

$filename =~ s/^.*(\\|\/)//;
$filename =~ s/ +/\_/g;

You could also build in an error message for the uploading user:
if ($info->{"filename"} =~ /([^\/\\]+)$/)) {
 print "Filename should not contain forward slashes";

Nothing has happened on this question in over 11 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points [grade B] between rj2 and dutchfoxer (responses don't quite match the problem set out by asker).

Please post any comments here within the next seven days.


EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial