CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Params::Validate question

 



perlFun
Novice

Jun 15, 2013, 12:18 AM

Post #1 of 4 (604 views)
Params::Validate question Can't Post

I am SO confused about a Perl module, Params, a subroutine it has called Validate. Note, I have looked at the CPAN doc., and I'm just still having trouble which is why I'm turning here. Let me give an example from the CPAN documentation.

# takes named params (hash or hashref)
sub foo {
validate(
@_, {
foo => 1, # mandatory
bar => 0, # optional
}
);
}


so if I'm understanding, does this mean it's going to call the sub validate, and it's going to try and validate that the arguments passed in were of type foo and bar, (regardless of order), where foo is mandatory but bar is optional? Ok so does that mean one of the things being passed to the sub called "foo" is a blessed foo reference?


The reason I ask these is because I am using the Validate sub as follows and my program continually crashes. Let me give an example:

sub my_sub{
my (%opts) = validate(@_, {
host => { type=>OBJECT, isa=>'SomeClass' },
mode =>{type=>SCALAR,default=>"normal",optional=>1 },
conn => {type=>SCALAR,default=>"#ssh",optional=>1},
}
);
.
.
. (other stuff is done in the sub and it eventually returns a hash) }


Now when I make the following call in another file:

%result=my_sub($obj), where $obj is a blessed SomeClass object, I get an error saying: "SCRIPT ERROR: The following parameter was passed in the call to Lib::my_sub but was not listed in the validation options: system_id" right when the editor gets to that Validate params part of the sub.


Honestly, I am not even certain what "system_id" is. I think it is a param associated with objects of type SomeClass. But I have no idea why it's taking that, rather than my SomeClass object. I have even made print statements going print ref($obj) to verify I'm not crazy, and sure enough I'm passing the correct type of object.


Even odder, if I pass to the method $obj, along with "normal" and "#ssh", I get the following error:

"SCRIPT ERROR: Odd number of parameters in call to Lib::my_sub when named parameters were expected"


(This post was edited by perlFun on Jun 15, 2013, 12:20 AM)


BillKSmith
Veteran

Jun 15, 2013, 4:13 AM

Post #2 of 4 (598 views)
Re: [perlFun] Params::Validate question [In reply to] Can't Post

Your call to validate is testing for named parameters. You are calling the subroutine with one positional prameter. Change call to:

Code
%result=my_sub(host=>$obj);

Good Luck,
Bill


perlFun
Novice

Jun 15, 2013, 9:45 AM

Post #3 of 4 (584 views)
Re: [BillKSmith] Params::Validate question [In reply to] Can't Post

thank you so much. so let me make sure I understand. if i do a named parameter call, for example the my_sub(host=>$obj) as you mentioned, the significance of the string "host" is that it needs to match what's given there in the call to the Validate subroutine. "host" is NOT the name of a class, it'snot the name of a subroutine, in that call to Validate if the code were switched from host to anyThingRandom I would have needed to make my call my_sub(anyThingRandom=>$obj)


BillKSmith
Veteran

Jun 15, 2013, 11:58 AM

Post #4 of 4 (577 views)
Re: [perlFun] Params::Validate question [In reply to] Can't Post

You are right about all the things that the string 'host' is not. Still, I may have misled you. I assumed that you understood named parameters and intended to use them. The parmeter names (e.g. 'host') are used as hash keys with the value stored as the corresponding hash value. Validate builds and returns this hash for you. Your subroutine must look for the values in that hash. If you made the change you suggest in the call to validate, validation would fail until until you made the same change in your call. Your program still would not work until you changed the subroutine to use the new hash key.

If you cannot get it working, show us the part of your subroutine where you get the value of "host".
Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives