in reply to Re: checking for reserved characters
in thread checking for reserved characters

Hi ikegami and thanks

What user gives me will eventually be used at the command line and I read at http://www.tuxfiles.org/linuxhelp/weirdchars.html that it might create problem since Linux might be confused. Is that true?

I should also worry about spaces, but I don't know how to check for that? Do you?

Thanks again,

Claire

Replies are listed 'Best First'.
Re^3: checking for reserved characters
by Tanktalus (Canon) on Apr 13, 2005 at 23:10 UTC

    Personally, I see no reason to avoid spaces et al. My wife has no clue what characters are special, and has no problems saving files such as "Foo's resume & cover letter.sxd". (No, her name isn't really "Foo" ;-}) And it works just fine.

    What you want to use to check for bad characters is simply

    $folder =~ m<[bad characters here]>;
    In your case, the only bad character is the slash, so we can eliminate the brackets:
    $folder =~ m</>;
    As for using at the command line - this is no different than writing documents in OpenOffice.org. If you want to load it, just put it in quotes. Or, using bash on Linux, just type the first few characters and hit the tab key - all escapes will be put in automatically for filename completion (assuming it is unique to the first few letters you typed in).

    If you're going to programmatically call another program, you should be learning to use the list version of system and/or exec anyway. By eliminating the shell, you don't need to worry about special characters.

    system('/usr/bin/ooffice', $filename);
    Whether $filename has spaces, or any other special character, or not, doesn't matter. It just works.

Re^3: checking for reserved characters
by tlm (Prior) on Apr 13, 2005 at 23:07 UTC

    If all you want to do is rule out certain file names, just test them against a suitable regexp, like this:

    $folder = $entryfolder->get(); #get the value from the entry widget if ( $folder =~ /[\$\\*&.?\s]/ ) { $dialog = $sw-> Dialog( -title=> 'Alert', -text => "Special characters appear in the name + of your folder.", -buttons => qw(OK) ) ->Show(); }
    That checks for whitespace too.

    BTW, I find it hard to understand why you'd want rule out periods in filenames. They are ubiquitous, and cause no problem. The only thing about having a period in a filename is that if it is the first character in the name, some commands, notably ls, will not display them unless one specifically requests it. If you want to rule out filenames that begin with a period, but allow them otherwise, change the regexp above to

    /[\$\\*&?\s]|^\./

    the lowliest monk

Re^3: checking for reserved characters
by ikegami (Patriarch) on Apr 13, 2005 at 23:02 UTC

    Yes, they can cause problems if they are passed to the shell improperly quoted and/or improperly escaped. In fact, I suspect there's more bad character than just those you listed. That's why it's bad to execute commands via the shell if you can avoid it. It's better to use the multiple argument form of system or exec instead if possible, since their argument don't need to be quoted or escaped then.