Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl file upload script

         

dcubed

12:47 am on Mar 30, 2003 (gmt 0)

10+ Year Member



I'll start off by saying I'm very much a newcomer to Perl (and this is only my second post here).

I'm working on a script that will allow authorized users to upload files to the web server(it's mostly done & does work) but to avoid possible problems I want to remove any white space from the names of the files being uploaded. i.e. Foo Bar.txt would upload as FooBar.txt. A friend suggesed tr/// but I can't find too much on how I'd use that in this particular instance.

I'd appreciate any suggestions, comments, etc.

Thanks!

andreasfriedrich

1:52 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[url=http://www.perldoc.com/perl5.8.0/pod/func/s.html]s[/url]{\s+}{}g;
will replace all (i.e. one or more, the plus sign) whitespace (\s) characters with the empty string. The replacement will be done globally (i.e. on the whole string, the g pattern modifier).

HTH Andreas

andreasfriedrich

2:09 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The transliteration operators
[url=http://www.perldoc.com/perl5.8.0/pod/func/tr.html]tr[/url]/searchlist/replacementlist/modifiers;
and
[url=http://www.perldoc.com/perl5.8.0/pod/func/y.html]y[/url]/searchlist/replacementlist/modifiers;
will replace all occurrences of the characters found in the search list with the corresponding character in the replacement list. They do not support RE character classes like \s, \w, or \d.

So

[url=http://www.perldoc.com/perl5.8.0/pod/func/s.html]s[/url]{Aaron}{Carter}g;
is fundamentally different from
[url=http://www.perldoc.com/perl5.8.0/pod/func/y.html]y[/url]{Aaron}{Carter};
. The former will replace Aaron with Carter while the latter will replace A with C, a with a, r with r, o with t, and n with e.

To achieve the same thing as with the RE in my above post you would need to list all whitespace you want to delete like so:

tr{\t\r\f\n }{}d;
.

Andreas

Learning Curve

5:03 am on Mar 30, 2003 (gmt 0)

10+ Year Member



Perhaps something easy like this would work.

$File_name =~ s/\s+//g;
or
$File_name =~ s/" "//g;

andreasfriedrich

11:48 am on Mar 30, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




>>$File_name =~ s/\s+//g;

Thatīs exactly the same as the RE posted in message #2.


s{\s+}{}g; will replace...

>>$File_name =~ s/" "//g;

This will delete all occurances of " " from the string contained in $File_name. Given a string of Aaron Carter it would not be altered by this RE since it does not contain " ". To delete all spaces the RE would need to look like this:


s/ //g;
s{ }{}g;

I hope that clears things up a bit.

Andreas

dcubed

7:34 pm on Mar 30, 2003 (gmt 0)

10+ Year Member



Thanks so much for your help - I've got it up & running & doing just what it should do.

Appreciate the help!

d