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: Edit Log



perlFun
Novice

Jun 15, 2013, 12:18 AM


Views: 589
Params::Validate question

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)


Edit Log:
Post edited by perlFun (Novice) on Jun 15, 2013, 12:20 AM
Post edited by perlFun (Novice) on Jun 15, 2013, 12:20 AM


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

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