TMTOWTDI.
package User;
use Scalar::Util qw( weaken );
sub new {
my $class = shift;
my $self = { @_ };
bless $self, $class;
my $session = $self->{session};
if ($session) {
$session->{user} = $self;
weaken $session->{user};
}
}
sub username {
my ($self) = @_;
return scalar getpwuid( $self->{id} );
}
package Admin;
use base qw( User );
# code
use Admin;
my $user = Admin->new( session => \%session, id => 100 );
# later code
my $user = $session{user};
# whatever code
print $user->{id}, " is ", $user->username;
Although at this point I would have used DBIx::Class, which rocks.
Or Class::Std, which is also useful.
Mark
On Tue, 13 Jun 2006, John Li wrote:
> From: John Li <[EMAIL PROTECTED]>
> To: 'fixed' <[EMAIL PROTECTED]>, [email protected]
> Date: Tue, 13 Jun 2006 10:15:36 -0700
> Subject: Re: [Mason] Mason Design Advice
>
> I would do it this way:
>
> $session{User} = {
> id => 100,
> username => 'tim',
> };
>
> display_id_and_username($session{User});
>
> John
>
>
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of fixed
> Sent: Tuesday, June 13, 2006 6:51 AM
> To: [email protected]
> Subject: Re: [Mason] Mason Design Advice
>
> Thank you for the reply.
>
> I come from a Java background and what you wrote makes sense. However, am
> I right in saying that you are creating a User object and storing this in
> $session? Therefore, Mason will always have access to:
>
> $session{User}->get_id or other methods for this object?
>
> In many Perl examples I have seen, I have never seen them put objects in
> the session, but things like:
>
> $session{id} = 100;
> $session{username} = tim;
> display_id_and_username($session{id}, $session{username});
>
> But I would prefer:
> display_id_and_username($session{User}->{id},$session{User}->get_name);
> or even better:
> display_id_and_username(\$session);
>
> Is it good practise in Perl to put the object in the session and go from
> there?
>
> Thank you
>
> Ogden
>
>
> On Mon, 12 Jun 2006, Mark Hedges wrote:
>
> >
> >
> > On Mon, 12 Jun 2006, Ogden wrote:
> > >
> > > MyApp::do_something($session{User}.get_id());
> >
> > Are you a python or java programmer? ( . = concat, -> = deref )
>
> > package MyApp;
> > use Perl6::Export::Attrs;
> > sub do_something :Export( :DEFAULT ) {
> > do_stuff();
> > }
> >
> > # in mason...
> >
> > use MyApp;
> > do_something( $session{User}->get_id() );
> >
> > There are 10,000 ways to do it in Perl. 80% of them are wrong.
> > Your way doesn't sound half bad.
> >
> > You could, for example, write your own PerlAuthen/AuthzHandler
> > that processes before Mason, and either redirects to the login
> > if not authenticated or sets the user object into $r->pnotes...
> > The the autohandler puts it into your session. Or a pre-process
> > handler creates the session.
> >
> > Whatever works.
> >
> > Mark
> >
>
>
> _______________________________________________
> Mason-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mason-users
>
>
>
>
> _______________________________________________
> Mason-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mason-users
>
_______________________________________________
Mason-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mason-users