Re: OO question
- From: mritty@xxxxxxxxx (Paul Lalli)
- Date: Tue, 26 Feb 2008 09:54:13 -0800 (PST)
On Feb 26, 11:20 am, paik...@xxxxxxxxxxxxxx (Dermot) wrote:
Hi All,
I am trying to work with the CGI::FormBuilder module in a Catalyst
environment. My question is more about OO programming than either of those 2
modules though.
I am trying to create a method/sub that will create a form to either edit or
add an entry. I am creating the initial object and depending on whether or
not the entry already exists, I want to set-up some aspects of my object
(making sense??). So I want to create the object, assign it to a variable
and make changes to it (or not) later depending on some condition. Here's an
abridged version of what I have done so far:
use strict;
use warnings;
...snip
sub edit : Local Form {
my ($self, $c, $id) = @_;
# Here comes my form
my $fb = $self->formbuilder->field(
name => 'Users',
type => 'select',
options =>
[ map { [ $_ -> id, $_->code ] }
$c->model('Files::users')->all ],
required => 1,
#value => undef
);
...
if ($id) {
$fb->{value} = $some_default_val;
}
}
So I guess the question is what sort of structure have I created initially
with $fb and $self and does $fb->{value} make sense. Can I add an attribute
to an object after I've created it in this way.
Go back to basics. An object is just a hash[1], a reference to which
happens to be blessed into a specific class. But it's still just a
hash. Anything you can do to a normal hash, you can do to an object.
That includes adding new key/value pairs to it.
So yes, it's perfectly allowed. No reason you can't.
Paul Lalli
[1] in this case, of course. You're equally able to make an object
out of a reference to a scalar, an array, a filehandle, or pretty much
anything else...
.
- References:
- OO question
- From: Dermot
- OO question
- Prev by Date: Re: Are comments allowed before package declarations in modules?
- Next by Date: Re: array question
- Previous by thread: OO question
- Next by thread: Are comments allowed before package declarations in modules?
- Index(es):
Relevant Pages
|