Not sure to understand what you want to do, but just a couple of commen ts on you code:

Code

my $ref = @_;

This is probably not doing what you want: this stores into $ref the number of elements of the @_ array.

Code

my $required = { file => 1, type => 1, head => 1 };

Again, although valid code, this is unlikely to be what you want. This stored into the required scalar variable a reference to an anonymous hash. Given the relatively beginner level of your questions, I doubt that you want to use hash refs. Perhaps you want:

Code

my %required = ( file => 1, type => 1, head => 1 );

which creates the %required hash.

as for your question, as I said, I don't understand it, but just syntax example:

c:\test>perl-1.pl $VAR1 = { 'head' => 'some_head', 'file' => 'file.txt', 'data' => 'some_data' }; The following parameter was passed in the call to sample_class::sample_method but was not listed in the validation options: foo at C:\test\perl-1.pl line 29. sample_class::sample_method(undef, 'file', 'file.txt', 'head', 'some_head', 'foo', 'fum') called at C:\test\perl-1.pl line 11

I have several comments. Remember, after correctness, the most important property of a program is that people (including yourself) can understand it. Use of the module is a clear winner. You have not even tried to make a case for do-it-yourself.

Your test cases do pass. This suggests that your code is correct, and that you understand it (at least today!). There a few issues which you have not tested.

Use of the default values in your method calls makes it impossible to tell if the program got the correct value from the call or from the default.

You did not test any invalid parameters. Should you ignore them or croak? (Croak is the default for the module, but it can be told to ignore them.)

Good practice requires you to test combinations of these conditions. I understand that this post is not the proper place for that.

The real purpose is the side-effect "$tmp = .....". (The final result is stored in @_ and never used. Perhaps a good thing, because many of us do not know the result of assigning a scalar to an existing array.) Note: You must study the rest of the program before you can tell the purpose of the statement.

Do you really believe that you will remember all the precedence and associativity rules needed to understand what "? $tmp = ..... " means? I did not.

Why did you not use the 'defined or' (//) operator? Are you using a version of perl older than v5.10.0? Good Luck, Bill

BillKSmith, FishMonger.. I have looked into hash validation and code correction and this is how I corrected and completely understand my code.

I have prepared many cases for validation pass. First I store my required/allowed/default hash values into $opts. Next I check @_, testing whether the passed argument is a hash ref or even-valued list or croak if neither one are passed. If not @_ then croak on no passed arguments.

Next I check if 'file' key is passed because its required, if not then croak on it.

I then use grep with an @array to check for invalid arguments passed.

Finally I loop through $opts keys and check if $args exist first then checking whether the value is allowed or not, or set the default values and return $args.