[26999] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 8935 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 9 00:05:48 2006

Date: Wed, 8 Feb 2006 21:05:06 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 8 Feb 2006     Volume: 10 Number: 8935

Today's topics:
    Re: circular references, chdir() and Term::ReadLine::Gn (Jamie)
    Re: circular references, chdir() and Term::ReadLine::Gn (Jamie)
    Re: circular references, chdir() and Term::ReadLine::Gn <uri@stemsystems.com>
        Filehandle STDOuT reopened as $fh only for input xhoster@gmail.com
    Re: Help: How do I get the lists of groups a unix user  <abaugher@esc.pike.il.us>
    Re: Help: How do I get the lists of groups a unix user  <noreply@gunnar.cc>
    Re: Help: How do I get the lists of groups a unix user  <rwxr-xr-x@gmx.de>
    Re: Help: How do I get the lists of groups a unix user  <noreply@gunnar.cc>
    Re: Help: How do I get the lists of groups a unix user  <someone@example.com>
    Re: Help: How do I get the lists of groups a unix user  <noreply@gunnar.cc>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Wed, 08 Feb 2006 23:37:34 GMT
From: nospam@geniegate.com (Jamie)
Subject: Re: circular references, chdir() and Term::ReadLine::Gnu?
Message-Id: <Lucy1139441401109560xbd8138@localhost>

In: <dscn5b$nra$1@mamenchi.zrz.TU-Berlin.DE>, anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
>> There has GOT to be.. a good way, something that doesn't involve more 
>> CPAN modules... to deal with this in a more sane fashion.
>
>As has been mentioned in the other thread, one of the standard methods
>to defuse circular refs is to make one of them weak.  It is applicable
>when your program is aware when it closes a cycle, (less so when cycles
>crop up at random) and the weak ref must be treated with attention: it
>can become undefined by some distant action.

I'd like to make them "weak" but as it's perl 5.6.0 my CPAN options for
XS modules are rather limited. (and the nature of the program is such that
many external modules are a bit of a no-no unless they're common)

The circular thing can't be avoided w/out a dependence on global variables, 
which, if I'd had it to do over again.. I probably would have used more of.

This is one of those cases where a few carefully chosen globals and use of
local() might have actually made the code easier to deal with. Certainly
would have been easier then a dozen or so cleanup() methods.

I allowed the "anti global" movement to cloud my judgement on this one.

Jamie
-- 
http://www.geniegate.com                    Custom web programming
guhzo_42@lnubb.pbz (rot13)                User Management Solutions


------------------------------

Date: Thu, 09 Feb 2006 00:36:25 GMT
From: nospam@geniegate.com (Jamie)
Subject: Re: circular references, chdir() and Term::ReadLine::Gnu?
Message-Id: <Lucy1139443951109560xc2e684@localhost>

In: <20060208100055.242$Cw@newsreader.com>, xhoster@gmail.com wrote:
>I'm not sure, but I suspect you should spend more effort on refactoring
>your code to avoid as much as the mess as possible, rather than refactoring
>perl to deal with the mess after the fact.

I can't help the circular thing at this point, but, a few careful globals
rather then the current model would have been much better. (normally, that
is what I would do, but this time I wanted to avoid globals as much as
possible, to see how it goes I guess.)

Needless to say, there is indeed a time and place for globals, at least,
localized globals. 

>> I remember some time ago reading about a trick using TIESCALAR to
>> accomplish this, but I can't seem to recall the method.
>
>At this point, I no longer know what the "this" above refers to.
>What are you trying to accomplish with tie, here?

Trying to implement transparent "weak" references. (this is perl 5.6.0, it's 6
years old... circa 2000... the List::Util is not available, so, the packages
that are available to weaken a scalar aren't available either)

>$a->{b} holds that thing you get back when you call FETCH on the tied
>variable $tb.
>
>$b->{a} holds that thing you get back when you call FETCH on the tied
>variable $ta.
>
>The things you get from calling FETCH are not themselves tied.

I tried this too:

package Circle;
# ----- snip --------
our(%STORE);
sub TIESCALAR {
	# ... SNIP ...
    my $key = "" . $var;  # Stringification of address as key
    $STORE{$key} = $var;  # into %STORE ($var is an object)
	# ... SNIP ....
}
sub FETCH {
    my($kref) = shift;      # $kref ($self) is a reference to a scalar. 
    return($STORE{$$kref}); # that scalar is a key in a %STORE hash
}

sub DESTROY {
	# ----- SNIP            # When Circle goes out of scope, delete the
	delete($STORE{$key});   # key in the global hash.
}
1;

{
	my($a) = new A('Object A');
    my($b) = new B('Object B');
    tie $a->{C}, 'Circle', $b;   # This time, try tie directly on the key.
    tie $b->{C}, 'Circle', $a;
}


Same results, the reference is not weakended.

I would have thought.. $a, $b would each hold a unique instance of the 
Circle class (Circle implementing the TIESCALAR interface) 

Since Circle doesn't hold $a or $b, just the stringification of the variable
used as a key in a %STORE hash, it should go out of scope, when it does, it 
deletes it's copy. (to be addressed.. since the stringification is the same
for each, etc.. this WOULDN'T work.. just trying to grok the whole
circular thing first.)

A and B should ALSO go out of scope since they no longer hold references
to each other, instead they hold references to Circle ?

Same exact result, except this time Circle, A, B all get destroyed
at global destruct time rather then when they leave the block.


Jamie
-- 
http://www.geniegate.com                    Custom web programming
guhzo_42@lnubb.pbz (rot13)                User Management Solutions


------------------------------

Date: Wed, 08 Feb 2006 23:19:34 -0500
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: circular references, chdir() and Term::ReadLine::Gnu?
Message-Id: <x7mzh1duzt.fsf@mail.sysarch.com>

>>>>> "J" == Jamie  <nospam@geniegate.com> writes:

  J> In: <dscn5b$nra$1@mamenchi.zrz.TU-Berlin.DE>, anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
  >>> There has GOT to be.. a good way, something that doesn't involve more 
  >>> CPAN modules... to deal with this in a more sane fashion.
  >> 
  >> As has been mentioned in the other thread, one of the standard methods
  >> to defuse circular refs is to make one of them weak.  It is applicable
  >> when your program is aware when it closes a cycle, (less so when cycles
  >> crop up at random) and the weak ref must be treated with attention: it
  >> can become undefined by some distant action.

  J> I'd like to make them "weak" but as it's perl 5.6.0 my CPAN options
  J> for XS modules are rather limited. (and the nature of the program
  J> is such that many external modules are a bit of a no-no unless
  J> they're common)

  J> The circular thing can't be avoided w/out a dependence on global
  J> variables, which, if I'd had it to do over again.. I probably would
  J> have used more of.

  J> This is one of those cases where a few carefully chosen globals and use of
  J> local() might have actually made the code easier to deal with. Certainly
  J> would have been easier then a dozen or so cleanup() methods.

there are ways to deal with circular refs and get them destroyed even
with refcounting. the main idea is to make a simple object (say even a
scalar) that has the ref to the real data structure. when the scalar
object gets refcounted to 0 it has its DESTROY called and that code
breaks the circular refs and so that data will get freed as well.

or be explicit and call your own destructor when you know you will need
to. this isn't hard in certain designs where the code that creates the
object keeps track of it. when you know the object is dead you call its
destructor which breaks the circular refs. if you code so you just pass
the object around and just wait for it to fall out of scope, you can't
use this method.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


------------------------------

Date: 09 Feb 2006 01:55:12 GMT
From: xhoster@gmail.com
Subject: Filehandle STDOuT reopened as $fh only for input
Message-Id: <20060208205759.772$Kx@newsreader.com>

I got a warning of

Filehandle STDOUT reopened as $fh only for input...

On which perldiag expands thus:

       Filehandle %s reopened as %s only for input
           (W io) You opened for reading a filehandle that got
           the same filehandle id as STDOUT or STDERR. This
           occured because you closed STDOUT or STDERR previ-
           ously.

Right, I did close STDOUT previously.  But so what?  This sounds more
like an amusing anecdote than a warning.  Is something bad likely to happen
because of this confluence of file descriptors?  If so, under what
circumstances?  If not, then why is this a warning?

Thanks,

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


------------------------------

Date: Wed, 08 Feb 2006 17:37:25 -0600
From: Aaron Baugher <abaugher@esc.pike.il.us>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <863bita0cq.fsf@cail.baugher.pike.il.us>

Brandon Hoppe <bhoppe@ti.com> writes:

> I need the list of groups that a user is in, similar to calling
> "groups" at a command prompt.

The easy answer would be:

  my @groups = split / /, `groups $username`;

If you want to do it all with perl functions, take a look at
getgrent().  It'll iterate through the group file, so you can do
something like this (untested):

  my $user = 'foobar'; # username we want the groups for
  my @groups;          # list of groups to which $user belongs
  while(my($group, $passwd, $gid, $users) = getgrent ){
    if( $users =~ /\b$user\b/ ){
      push @groups, $group;
    }
  }



-- 
Aaron -- aaron_baugher@yahoo.com
         http://360.yahoo.com/aaron_baugher


------------------------------

Date: Thu, 09 Feb 2006 01:51:41 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <44vht4F491a6U1@individual.net>

Brandon Hoppe wrote:
> Eric Schwartz wrote:
>> Brandon Hoppe <bhoppe@ti.com> writes:
>>> I need the list of groups that a user is in, similar to calling 
>>> "groups" at a command prompt.
>>
>> What's wrong with
>>
>> my @groups = split ' ', `groups`;
> 
> Shoot...didn't even think of that.

<snip>

> Yeah, I've tried several combinations of the functions getpwuid(), 
> getgrnam(), getgrgid() etc. But using those I was only able to get the 
> first group listed when you call groups. I wasn't able to figure out 
> what other group the user was associated with. For example:
> 
> my $group = getgrgid((getpwuid($<))[3]);
> 
> Only returned the 1st group name listed when you ran groups. Even if the 
> user changed to a group using 'newgrp' command, it wouldn't give the 
> current group that the user was using.

This comes close, without shelling out:

     my @groups = map { (getgrgid $_)[0] } split ' ', $);

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Thu, 9 Feb 2006 03:00:01 +0100
From: "Lukas Mai" <rwxr-xr-x@gmx.de>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <dse7n1$jfa$01$2@news.t-online.com>

Brandon Hoppe <bhoppe@ti.com> schrob:
> 
> I need the list of groups that a user is in, similar to calling
> "groups" at a command prompt.
> 
> I've searched everywhere but haven't been able to find anything to do
> this in perl.

my @groups = split ' ', $(;

Or am I missing something?

HTH, Lukas


------------------------------

Date: Thu, 09 Feb 2006 03:17:25 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <44vmtsF48s6rU1@individual.net>

Lukas Mai wrote:
> Brandon Hoppe <bhoppe@ti.com> schrob:
>>I need the list of groups that a user is in, similar to calling
>>"groups" at a command prompt.
>>
>>I've searched everywhere but haven't been able to find anything to do
>>this in perl.
> 
> my @groups = split ' ', $(;
> 
> Or am I missing something?

Yes, the rest of this thread.
http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998

And the fact that $( consists of group IDs as opposed to group names.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: Thu, 09 Feb 2006 02:36:30 GMT
From: "John W. Krahn" <someone@example.com>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <OUxGf.190808$AP5.74644@edtnps84>

Gunnar Hjalmarsson wrote:
> Lukas Mai wrote:
>> Brandon Hoppe <bhoppe@ti.com> schrob:
>>> I need the list of groups that a user is in, similar to calling
>>> "groups" at a command prompt.
>>>
>>> I've searched everywhere but haven't been able to find anything to do
>>> this in perl.
>>
>> my @groups = split ' ', $(;
>>
>> Or am I missing something?
> 
> Yes, the rest of this thread.
> http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998
> 
> 
> And the fact that $( consists of group IDs as opposed to group names.

my @groups = map scalar getgrgid $_, split ' ', $('


John
-- 
use Perl;
program
fulfillment


------------------------------

Date: Thu, 09 Feb 2006 03:54:35 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Help: How do I get the lists of groups a unix user is in?
Message-Id: <44vp3iF41p2lU1@individual.net>

John W. Krahn wrote:
> Gunnar Hjalmarsson wrote:
>>Lukas Mai wrote:
>>>
>>>my @groups = split ' ', $(;
>>>
>>>Or am I missing something?
>>
>>Yes, the rest of this thread.
>>http://groups.google.com/group/comp.lang.perl.misc/browse_frm/thread/b9b4eeb7aabaf998
>>
>>And the fact that $( consists of group IDs as opposed to group names.
> 
> my @groups = map scalar getgrgid $_, split ' ', $('

[ Sigh ]

http://groups.google.com/group/comp.lang.perl.misc/msg/eb9dfdb5453a0dba

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V10 Issue 8935
***************************************


home help back first fref pref prev next nref lref last post