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

Hi, Could someone help me with a script which will disallow to commit if there are spaces in the file names or directory names........ Scrpt should check for the spaces in the names of the files or directories which we are commiting.
  • Comment on perl script to disallow spaces in file or dir name

Replies are listed 'Best First'.
Re: perl script to disallow spaces in file or dir name
by marto (Cardinal) on May 17, 2012 at 09:55 UTC

    I'm not sure what you mean by "commit", are you doing some database work? The following is a very basic example for (one way of) checking for spaces within strings:

    #!/usr/bin/perl use strict; use warnings; my $path; $path = "/usr/bin"; #$path = "/usr/bin foo"; # uncomment for testing if ( $path =~ /\s/){ print "String $path contains a space character.\n"; }else{ print "String $path does not contain a space character.\n"; }

    See perlrequick.

Re: perl script to disallow spaces in file or dir name
by diddy_perl (Novice) on May 17, 2012 at 09:54 UTC

    Maybe something along those lines:

    use strict; my $filename = 'fielname.doc'; $filename = &trim($filename); unless( $filename =~ /\s/ ) { ## commit print 'Commit', "\n"; } ## Removes leading and trailing spaces sub trim() { my $nm = shift; $nm =~ s/^\s+//; $nm =~ s/\s+$//; return $nm; }
Re: perl script to disallow spaces in file or dir name
by sauoq (Abbot) on May 17, 2012 at 11:42 UTC
    Could someone help me with a script

    It's very likely that someone can help. What do you have so far?

    -sauoq
    "My two cents aren't worth a dime.";