Hi,

I have to make a script with parameter that limits the character. This is the code snippet
So script.pl -teacher [string]
where teacher should only be allowed of [A-Za-z0-9_-]# , if not, print error message which is "error".
Other thing is that, the parameter -teacher have to follow the script.pl, if not, it will show an error message of "please use parameter 'teacher'. Please guide

#!/usr/bin/perl
use Getopt::Long;

my $teacher =~ s/[^A-Za-z0-9\-_].//#//g;
Getopt::Long::GetOptions("teacher=s" => \$teacher); #how can i show that a parameter that have to be inserted, if not show error "please use parameter 'teacher'

unless
{
$teacher
}
print "error";

This code doesn't run. Where did you get this code? Have you been able to run it successfully? It doesn't make any sense to me.

This code doesn't run. Where did you get this code? Have you been able to run it successfully? It doesn't make any sense to me.

Hi, this was a total messed up code, so it was not running.
Also I am sorry, there is a wrong sentence that I wrote in my previous post.
But basically, I need to create script.pl -teacher [string] where the string should only be allowed of [A-Za-z0-9_-]#

so example I allowed do
script.pl -teacher sandra

but I am not allowed to do
script.pl -teacher sandra§ (and will show errors as I have described before)

I hope this is more clear, sorry again for the mistake

Near the beginning of the documentation for Getopt::Long it says "To distinguish between a bundle of single-character options and a long one, two dashes are used to precede the option name." You would type the following at your command line to run your script.pl perl script.pl --teacher In every perl script that you write you should use strict; and use warnings; ... they sometimes tell you why your program may not do what you expect.

The following: #my $teacher =~ s/[^A-Za-z0-9\-_].//#//g; violates the syntax rules because the semicolon does not terminate the command. Putting a semicolon in a comment doesn't do anything.

Maybe you meant this instead? my $teacher =~ s/[^A-Za-z0-9\-_]//g;#Remove characters not in the good set But how can you remove characters from $teacher, which comes from Getopt::Long::GetOptions("teacher=s" => \$teacher); BEFORE the statement that gets the teacher option? The statements occur in the wrong sequence in your script.

Plus you put brace brackets around your condition instead of parentheses, and your print statement should be enclosed in brace brackets because you want it to be in a block following your unless condition.

Near the beginning of the documentation for Getopt::Long it says "To distinguish between a bundle of single-character options and a long one, two dashes are used to precede the option name." You would type the following at your command line to run your script.pl perl script.pl --teacher In every perl script that you write you should use strict; and use warnings; ... they sometimes tell you why your program may not do what you expect.

The following: #my $teacher =~ s/[^A-Za-z0-9\-_].//#//g; violates the syntax rules because the semicolon does not terminate the command. Putting a semicolon in a comment doesn't do anything.

Maybe you meant this instead? my $teacher =~ s/[^A-Za-z0-9\-_]//g;#Remove characters not in the good set But how can you remove characters from $teacher, which comes from Getopt::Long::GetOptions("teacher=s" => \$teacher); BEFORE the statement that gets the teacher option? The statements occur in the wrong sequence in your script.

Plus you put brace brackets around your condition instead of parentheses, and your print statement should be enclosed in brace brackets because you want it to be in a block following your unless condition.

Hi,

Thank you so much for your explanation, but I meant the wrong thing (and I did not do the correct script).

I am sorry, but what I meant is that like this
I am allowed do
script.pl -teacher sandra

but I am not allowed to do
script.pl -teacher sandra§

(which means that can replace "sandra" with anything from character of [A-Za-z0-9_-]# ) Sorry for this confussion, but appreciate so much for your explanation

I don't understand the use of the # in your message. It indicates a comment in perl. If I were you I'd RTFM, of which David posted an important portion. If you do the regex as David suggested, you'll remove all non-letter/number/dash/underscore characters and you'll never have to run the error code. I think you either need to test for their existence and print the error or remove them, but not both.

I don't understand the use of the # in your message. It indicates a comment in perl. If I were you I'd RTFM, of which David posted an important portion. If you do the regex as David suggested, you'll remove all non-letter/number/dash/underscore characters and you'll never have to run the error code. I think you either need to test for their existence and print the error or remove them, but not both.

Good point. my $teacher =~ s/[^A-Za-z0-9\-_]//g; removes all characters that are not letters, numbers, hyphen or underscore, but this is NOT what you say you want to do. You want to know if your regex matches any invalid characters; you don't want to substitute anything. The s prefix means 'substitute'. You want to match instead of substitute. There are plenty of examples of matching in http://perldoc.perl.org/perlrequick.html#Simple-word-matching and http://perldoc.perl.org/perlrequick.html#Using-character-classes

aha, now I understand. Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.