Can't believe you guys didn't catch it. I had parenthesis instead of
braces ;)
$ui->(fname) = "bob"; #incorrect ()
$ui->{fname} = "bob"; #correct {}
Thank for you help. Now asome enlighten questions.
- Are the fname and lname implicitly declared?
- I guess you can't have vars outside of the methods and constructor?
#!/usr/bin/perl
package UserInfo;
use strict;
#our $fname,$lname;
#our $age, $sex; #if yes, how do you ref this from calling prog
sub new
{ my $class = shift;
my $self = { fname=>'', lname=>'' };
return bless $self, $class;
}
sub full_name
{ my $self = shift;
return $self->{fname} . ' ' . $self->{lname};
}
1;
----------------------------------
> OK version x.3
> Please just change these lines to make it work. The calling program
> test.pl fails on the assignment line:
>
> Bareword "fname" not allowed while "strict subs" in use at utest line 8.
> Bareword "lname" not allowed while "strict subs" in use at utest line 9.
> Bareword "fname" not allowed while "strict subs" in use at utest line 12.
> Execution of test.pl aborted due to compilation errors.
>
> test.pl
> -------
> #!/usr/bin/perl
>
> use strict;
>
> use UserInfo;
>
> my $ui = new UserInfo();
> $ui->(fname) = "bob"; #dying here on assignment
> $ui->(lname) = "Bingham";
>
> #change name
> $ui->(fname) = "robert";
>
> print "ui: [" . $ui->full_name() . "]\n";
>
> exit
>
> UserInfo.pm
> -----------
> #!/usr/bin/perl
> package UserInfo;
>
> use strict;
>
> sub new
> { my $class = shift;
> my $self = { fname=>'', lname=>'' };
> return bless $self, $class;
> }
>
> sub full_name
> { my $self = shift;
> return $self->{fname} . ' ' . $self->{lname};
> }
>
> 1;
>
> ----------------------
>> On Tuesday, September 30, 2003, at 06:51 PM, [EMAIL PROTECTED] wrote:
>>
>>> James and Bob,
>>>
>>> OK version x.2
>>> - I want to create a user object with value initialized.
>>
>> I showed you how to do this in my last message. Go back and take a
>> look.
>>
>>> - Initialize/Change it anytime
>>
>> It's best to do this with accessor methods. You should have one for
>> each name. Can you figure those out from my full_name() example?
>>
>>> test.pl
>>> -------
>>
>> What's still missing here? ;)
>>
>>> use UserInfo;
>>
>> Okay, UserInfo is better, but doesn't tell us much. I was thinking
>> something more like ComputerUser, DatabaseUser, GymUser, etc.
>>
>>> my $ui = new UserInfo();
>>> $ui->(fname) = "bob";
>>> $ui->(lname) = "Bingham";
>>>
>>> #change name
>>> $ui->(fname) = "robert";
>>
>> This works, but again, accessor methods would be better.
>>
>>> print "ui: [" . $ui->full_name() . "]\n";
>>>
>>> exit
>>
>> This isn't a great habit. We don't need it, so leave it out.
>>
>>> UserInfo.pm
>>> -----------
>>> #!/usr/bin/perl
>>> package UserInfo;
>>
>> Hmm, something is still missing here too... :D
>>
>>> sub new
>>> { my $class = shift;
>>> my $self = { fname, lname };
>>
>> That's not what I said, is it? Your making an anonymous hash here and
>> storing a reference to it. Hashes have key value pairs. Take a look
>> at my code again.
>>
>> You also broke my initialization trick by changing the above line...
>>
>>> return bless $self, $class;
>>> }
>>>
>>> sub full_name
>>> { my $self = shift;
>>> return $self->{fname} . ' ' . $self->{lname};
>>> }
>>>
>>> 1;
>>
>> Just shout if you need more help, but make sure you're looking at what
>> I'm sending you. The answers are in my last message, mostly.
>>
>> James
>>
>>
>> --
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
>>
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]