Click here to get back home

Stuffing @users into $self->

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Stuffing @users into $self-> Philluminati 05-01-2008
Posted by Ben Morrow on May 3, 2008, 7:19 pm
Please log in for more thread options

> >
> > If $x is a reference to an array, then @{ $x } dereferences it.
> >
> > my @users = @{ $self-> };
>
> Yup. That's one of the two rules to remember. The other is to use -> to
> access individual members.

I'm currently considering writing a module that would allow the syntax

my @users = $self->[];

instead[1]. Given that it will be rather tricky, would people actually
find such a thing useful, or would the 'effort' of loading a module be
enough to put people off using it?

Ben

[1] I originally did it as a patch for core perl, which was considerably
easier; the consensus on p5p was that such minor and not-obviously-
terribly-useful syntax changes should be tested on CPAN first, if at all
possible.

--
I've seen things you people wouldn't believe: attack ships on fire off
the shoulder of Orion; I watched C-beams glitter in the dark near the
Tannhauser Gate. All these moments will be lost, in time, like tears in rain.
Time to die. ben@morrow.me.uk

Posted by xhoster on May 3, 2008, 8:53 pm
Please log in for more thread options
> > >
> > > If $x is a reference to an array, then @{ $x } dereferences it.
> > >
> > > my @users = @{ $self-> };
> >
> > Yup. That's one of the two rules to remember. The other is to use -> to
> > access individual members.
>
> I'm currently considering writing a module that would allow the syntax
>
> my @users = $self->[];
>
> instead[1]. Given that it will be rather tricky, would people actually
> find such a thing useful, or would the 'effort' of loading a module be
> enough to put people off using it?

The module would have to be based on something like Filter::Util::Call,
right? I've heard bad things about the stability of that, which would put
me off using such a module on a regular basis. Other than that, I'd like
it very much.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Peter J. Holzer on May 4, 2008, 4:24 am
Please log in for more thread options
>
>> >
>> > If $x is a reference to an array, then @{ $x } dereferences it.
>> >
>> > my @users = @{ $self-> };
>>
>> Yup. That's one of the two rules to remember. The other is to use -> to
>> access individual members.
>
> I'm currently considering writing a module that would allow the syntax
>
> my @users = $self->[];
>
> instead[1].

Nice. Is there a way to extend this to array slices?

my @users = $self->[1, 3 .. 5]

But I guess that collides with existing valid syntax.

> Given that it will be rather tricky, would people actually
> find such a thing useful, or would the 'effort' of loading a module be
> enough to put people off using it?
>
> Ben
>
> [1] I originally did it as a patch for core perl, which was considerably
> easier; the consensus on p5p was that such minor and not-obviously-
> terribly-useful syntax changes should be tested on CPAN first, if at all
> possible.

I would find that very useful. The effort of loading a module wouldn't
put me off, instabilities introduces by a source-filter might, though.

        hp

Posted by xhoster on May 1, 2008, 11:16 am
Please log in for more thread options
> Hi everyone.
>
> I'm writing a SOAP Server in perl and I need/want to use objects. I
> have an array called @users which is populated in sub new. I want to
> store this array in my $self variable which is passed to all functions
> and then pull it out later. Can somebody help me?
>
> sub new
> {
> my ($class_name) = @_;
> my ($self) = {};
> bless ($self, $class_name);
>
> my $phill = new user;
> $phill->setId(6);
> $phill->setUsername("ptaylor");
> $phill->setPassword("password");
> $phill->setSessionKey("");
>
> #cut some code out to stop the post being enormous.
>
> my @users = ( $phill );

@users will hold exactly one thing, $phill. Why use an array for that?

>
> $self-> = [ @users ];
>
> return $self;
>
> }
> sub login
> {
> my ($self, $username, $password) = @_;
> my @users = @$self->; #<--
> THIS IS LINE 62
>
>
> how I get this message in my VB Soap Client:
>
> Can't use string ("user_management") as an ARRAY ref while "strict
> refs" in use at user_management.pm line 62.

The problem is in some part of the code you didn't show us.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Philluminati on May 1, 2008, 11:26 am
Please log in for more thread options
On May 1, 4:16 pm, xhos...@gmail.com wrote:
> > Hi everyone.
>
> > I'm writing a SOAP Server in perl and I need/want to use objects. I
> > have an array called @users which is populated in sub new. I want to
> > store this array in my $self variable which is passed to all functions
> > and then pull it out later. Can somebody help me?
>
> > sub new
> > {
> > my ($class_name) = @_;
> > my ($self) = {};
> > bless ($self, $class_name);
>
> > my $phill = new user;
> > $phill->setId(6);
> > $phill->setUsername("ptaylor");
> > $phill->setPassword("password");
> > $phill->setSessionKey("");
>
> > #cut some code out to stop the post being enormous.
>
> > my @users = ( $phill );
>
> @users will hold exactly one thing, $phill. Why use an array for that?
>
>
>
>
>
> > $self-> = [ @users ];
>
> > return $self;
>
> > }
> > sub login
> > {
> > my ($self, $username, $password) = @_;
> > my @users = @$self->; #<--
> > THIS IS LINE 62
>
> > how I get this message in my VB Soap Client:
>
> > Can't use string ("user_management") as an ARRAY ref while "strict
> > refs" in use at user_management.pm line 62.
>
> The problem is in some part of the code you didn't show us.
>
> Xho
>
> --
> --------------------http://NewsReader.Com/--------------------
> The costs of publication of this article were defrayed in part by the
> payment of page charges. This article must therefore be hereby marked
> advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
> this fact.

The array only held one value because I didn't want to post an
enourmous block of text. Anyway here is the entire copy of the code.
Each instance gets an array of three people called @users. It's shoved
into $self->. Then in each function I want to pull it out
again but I can't seem to do it. The query functions shows there are
no users what so ever in the array:

#!/usr/bin/perl -w

package user_management;

use user;
#use strict;

sub new
{
my ($class_name) = @_;

my ($self) = {};
bless ($self, $class_name);

        #some objects for the array
        my $phill = new user;
        $phill->setId(6);
        $phill->setUsername("ptaylor");
        $phill->setPassword("password");
        $phill->setSessionKey("");

        my $simon = new user;
        $simon->setId(7);
        $simon->setUsername("sroberts");
        $simon->setPassword("cheese");
        $simon->setSessionKey("");

        my $nick = new user;
        $nick->setId(8);
        $nick->setUsername("nick");
        $nick->setPassword("rock");
        $nick->setSessionKey("");

        #put them in the array
        my @users = ( $phill, $simon, $nick );

        $self-> = \@users; #put the array in an instance variable
(or perl equiv)
        $self-> = 1;

return $self;

}

sub login
{
        my ($self, $username, $password) = @_;

        #print "usr: $username\npwd: $password\n";
        #my $size = @users;
        #;return "ERR: Count is $size";
        #return "ERR: I GOT $username, $password";
        my @users = @{ $self-> };

        foreach my $user (@users)
        {
                if ($user->getUsername() eq $username)
                {
                        if ($user->getPassword() eq $password)
                        {
                                if ($user->getSessionKey ne "")
                                {
                                        return "ERR: User already logged in";
                                }
                                else
                                {
                                        #generate session key
                                        $user->setSessionKey("SESS" . $self->);
                                        $self-> = $self-> + 1;
                                        return $user->getSessionKey();
                                }
                        }
                        else
                        {        return "ERR: Password is wrong"; }
                }
        }

        return "ERR: No users";
}

sub query
{
        my $self = shift;
        my @users = @{ $self-> };
        my $active = 0;
        my $total = @users;
        my $retval = "";

        foreach my $usr (@users)
        {
                if ($usr->getSessionKey() ne "")
                {
                        $retval = $retval . "user " . $usr->getUsername() . " logged in\n";
                        $active++;
                }
                else
                {
                        $retval = $retval . "user " . $usr->getUsername() . " logged out
\n";
                }
        }

        return "$retval$active of $total";
}

sub logout
{
        my ($self, $sessionKey) = @_;
        my @users = @{ $self-> };

        foreach my $usr (@users)
        {
                if ($usr->getSessionKey() eq $sessionKey)
                {
                        $usr->setSessionKey("");
                        return 1;
                }
        }

        return 0;
}

1;


--

Before anyone makes a note of it - I am aware that each instance of
the user_management object will get a duplicate array. This isn't my
primary concern at the moment however. I just want to be able to store
arrays inside object instances.

Similar ThreadsPosted
Any RT users November 23, 2004, 10:11 pm
On redhat, different users = different @INC August 20, 2007, 3:00 pm
Mac Users cannot access perl CGI June 15, 2005, 9:50 pm
NewsPro Users Webring November 18, 2005, 1:15 am
Maybe difficult for English Users - but I ask anyway January 20, 2006, 3:18 am
Best way to keep registered users logged in? October 21, 2006, 4:40 am
removing users from /etc/group September 12, 2007, 11:57 pm
web based bibliography creation by users January 1, 2005, 5:18 pm
file upload failing for some users June 23, 2005, 10:17 am
perl better than python for users with disabilities? December 20, 2006, 11:11 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap