James and Rob,

OK version x.2
- I want to create a user object with value initialized.
- Initialize/Change it anytime

test.pl
-------
use UserInfo;

my $ui = new UserInfo();
$ui->(fname) = "bob";
$ui->(lname) = "Bingham";

#change name
$ui->(fname) = "robert";

print "ui: [" . $ui->full_name() . "]\n";

exit

UserInfo.pm
-----------
#!/usr/bin/perl
package UserInfo;

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:01  PM, [EMAIL PROTECTED] wrote:
>
>> Can someone make this work like I want? I'm trying to create a package
>> USER and reference/change it. The only thing I'm able to do is to call
>> the
>> sub prtAll. I just want a structure that I can pass around in perl.
>>
>> test.pl
>> -------
>
> Good code starts with the following, I promise.
>
> use strict;
> use warnings;
>
>> use USER;
>
> Let's drop the all caps here.  Perl tradition is class names in
> titlecase.  The name could also be more descriptive.  What kind of user?
>
> use User;
>
>> #this does NOT work
>> #how do i reference these vars
>> USER::fname="bob";
>> USER::lname="Bingham";
>
> It doesn't work because you forgot the special variable symbols.
>
> $User::fname = 'bob';
> $User::lname = 'Bingham';
>
> However, objects would probably be better here, so let's try:
>
> my $user = User->new(f_name => 'bob', l_name => 'Bingham');
>
>> print USER::prt . "\n";
>
> print $user->full_name(), "\n";
>
>> USER.pm
>
> User.pm
>
>> -------
>> package USER;
>
> package User;
>
> use strict;
> use warnings;
>
>> $fname;
>> $lname;
>
> These would need to use our(), under strict:
>
> our($fname, $lname);
>
> But we don't need them.
>
> sub new {
>       my $class = shift;
>       my $self = { f_name => 'John', l_name => 'Doe', @_ };
>       return bless $self, $class;
> }
>
> sub full_name {
>       my $self = shift;
>       return $self->{f_name} . ' ' . $self->{l_name};
> }
>
> That's very basic object oriented programming, tell me if you need
> parts of it explained.
>
> James
>
>> sub prtAll { ... }
>>
>>
>>
>> --
>> 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]

Reply via email to