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

Hello Monks; I am in need of your wisdom. The ultimate goal is ti create a method of validating user input. I have a script that prompts the user to enter values on the command line, (using getopts). I have a configuration file (text file) where I have a line configured to store the 'allowed values'
Example:
Tags mail, page, conf, test

I am using use Config::Simple to read this file and create an array of "allowed" values:
open(FH, "config.txt"); $cfg->guess_syntax(\*FH)); my @tags = $cfg->param("Tags"); unlink $_ for @tags;
I then grab the user entry via getopts and store in an array
my @user_tags = $opt_c ; And, I use use Array::Utils qw(:all) like so...
my @valid_tag = intersect(@user_tags, @hash_tags); if (@valid_tag == 0){ die " \n\n NOTICE: TAG MISSING OR INVALID \n \ +n\n"; } else{ chomp (@valid_tag); }
This works okay as long as only a single value is passed on the command line. Example:
$ script.p, -c "test"
But, if I pass multiple values, the verification step fails Example:
$ script.pl -c "test page"
NOTICE: TAG MISSING OR INVALID
Any advice would be much appreciated! Thank you! Bee

Replies are listed 'Best First'.
Re: Comparing Arrays
by davido (Cardinal) on Jul 16, 2013 at 03:49 UTC

    One quick question: Is this actual code, or a typo?

    my @user_tags = $opt_c ;

    If that's actual code, it's only ever going to be capable of placing a single element in @user_tags, because that's the most that a scalar can contain.


    Dave

Re: Comparing Arrays
by kcott (Archbishop) on Jul 16, 2013 at 05:33 UTC
Re: Comparing Arrays
by 5mi11er (Deacon) on Jul 16, 2013 at 03:47 UTC
    Well, you're going to need to split the flag input on white space and put each item in the array, as you have it now the string "test page" is in the array.

    But why are you making this difficult for yourself? If the tags are roughly static, you could make a command line flag for each "tag" instead...

    -Scott

Re: Comparing Arrays
by mtmcc (Hermit) on Jul 16, 2013 at 07:24 UTC
    You could customize the behaviour by writing your own code, maybe something like this:

    #!/usr/bin/perl use strict; use warnings; my $configFile = "tags.txt"; # <-- file containing the 'tags' (Tags ma +il, page, conf, test) my $line; my @array; my %tags; my $options; my @optionLine; my $totalTags = 0; my $totalNonTags = 0; open (CONFIG, "<", $configFile) or die "$configFile not found\n\n"; while (<CONFIG>) { if ($_ =~ m/Tags/) { chomp; $line = $_; $line =~ s/Tags //; while ($line =~ s/\s//){}; @array = split (',', $line); for (@array) { $tags{"$_"} = 1; } } } print STDERR "Enter tag(s): "; $options = <STDIN>; chomp $options; @optionLine = split (" ", $options); for (@optionLine) { print "$_ tag found\n" if defined $tags{$_}; print "$_ is not a tag!\n" unless defined $tags{$_}; $totalTags = $totalTags + $tags{$_} if defined $tags{$_}; $totalNonTags += 1 unless defined $tags{$_}; } print STDERR "$totalTags tags found in total\n"; print STDERR "$totalNonTags non-tags entered\n";

    -Michael