[30585] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1828 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Aug 30 21:09:45 2008

Date: Sat, 30 Aug 2008 18:09:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sat, 30 Aug 2008     Volume: 11 Number: 1828

Today's topics:
        question about data structures - what does $# mean? <edwardjameshartnett@gmail.com>
    Re: question about data structures - what does $# mean? <jurgenex@hotmail.com>
    Re: question about data structures - what does $# mean? <joost@zeekat.nl>
    Re: question about data structures - what does $# mean? <sanjeeb25@gmail.com>
    Re: question about data structures - what does $# mean? <rvtol+news@isolution.nl>
    Re: question about data structures - what does $# mean? <rvtol+news@isolution.nl>
    Re: subprocess lifecycle <joe@inwap.com>
    Re: subprocesses lifecycle <hjp-usenet2@hjp.at>
    Re: subprocesses lifecycle <hjp-usenet2@hjp.at>
    Re: subprocesses lifecycle <ced@blv-sam-01.ca.boeing.com>
    Re: subprocesses lifecycle <whynot@pozharski.name>
    Re: trouble writing a setuid script <magloca@mailinater.com>
    Re: trouble writing a setuid script <petermichaux@gmail.com>
    Re: trouble writing a setuid script <joost@zeekat.nl>
    Re: trouble writing a setuid script <g_r_a_n_t_@dodo.com.au>
    Re: trouble writing a setuid script <jurgenex@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 30 Aug 2008 05:21:14 -0700 (PDT)
From: Ed <edwardjameshartnett@gmail.com>
Subject: question about data structures - what does $# mean?
Message-Id: <ead06ac0-82be-493e-b72f-362a8e17f16e@d1g2000hsg.googlegroups.com>

Howdy all!

Here's a little program:

#!/usr/bin/perl -w
my $d = {sid=>["lll"]};
print $#{$d->{sid}}."\n";

I expect this to print 1, but it prints 0.

What's up with that?

As I read this, $d is a ref to an anonymous assoc. array, which
contains one pair of values, named sid, with an anonymous array
containing one element: "lll".

So to get the number of elements in the array I try:
$#{$d->{sid}}

But that does not work. It gives me 0.

Meanwhile, this does work:
scalar(@{$d->{sid}})

Which made me realize I don't really know what the # does when used in
$#{$d->{sid}}.

Any help would be appreciated!

Thanks,

Ed


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

Date: Sat, 30 Aug 2008 12:53:23 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: question about data structures - what does $# mean?
Message-Id: <dbgib41p957eabi03mvgdogs453ragehsi@4ax.com>

Ed <edwardjameshartnett@gmail.com> wrote:
>So to get the number of elements in the array I try:
>$#{$d->{sid}}

Wrong operator. If you want the number of elements in an array then just
use the array in scalar context, if nothing else then by using scalar().

$# OTOH will return the last index in that array, which is one less than
the number of elements unless someone messed around with $[.

>But that does not work. It gives me 0.

Just as it should.

>Meanwhile, this does work:
>scalar(@{$d->{sid}})

Surprise, surprise.

jue


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

Date: Sat, 30 Aug 2008 14:53:12 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: question about data structures - what does $# mean?
Message-Id: <87iqtir7ev.fsf@zeekat.nl>

Ed <edwardjameshartnett@gmail.com> writes:

> Howdy all!
>
> Here's a little program:
>
> #!/usr/bin/perl -w
> my $d = {sid=>["lll"]};
> print $#{$d->{sid}}."\n";
>
> I expect this to print 1, but it prints 0.

0 is the correct response.

perldata says:

           $days               # the simple scalar value "days"
           $days[28]           # the 29th element of array @days
           $days{’Feb’}        # the ’Feb’ value from hash %days
           $#days              # the last index of array @days

Since perl arrays are indexed starting at 0 by default, an array
containing 1 element has a "last index" value of 0.

As you noted, the correct way to find out the length of an array is to
use scalar(@array).

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Sat, 30 Aug 2008 05:57:13 -0700 (PDT)
From: sanjeeb <sanjeeb25@gmail.com>
Subject: Re: question about data structures - what does $# mean?
Message-Id: <8e610c3b-c507-46c8-983e-f4831d795d1c@r15g2000prd.googlegroups.com>

On Aug 30, 5:21 pm, Ed <edwardjameshartn...@gmail.com> wrote:
> Howdy all!
>
> Here's a little program:
>
> #!/usr/bin/perl -w
> my $d = {sid=>["lll"]};
> print $#{$d->{sid}}."\n";
>
> I expect this to print 1, but it prints 0.
>
> What's up with that?
>
> As I read this, $d is a ref to an anonymous assoc. array, which
> contains one pair of values, named sid, with an anonymous array
> containing one element: "lll".
>
> So to get the number of elements in the array I try:
> $#{$d->{sid}}
>
> But that does not work. It gives me 0.
>
> Meanwhile, this does work:
> scalar(@{$d->{sid}})
>
> Which made me realize I don't really know what the # does when used in
> $#{$d->{sid}}.
>
> Any help would be appreciated!
>
> Thanks,
>
> Ed
$#array gives the last index  of the array not the number of elements
in the array, so you need to add 1 to $#array to get the number of
elements.
Since you have only one element its giving 0 , if you put 3 elements
in array it will give 2


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

Date: Sat, 30 Aug 2008 17:14:44 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: question about data structures - what does $# mean?
Message-Id: <g9bv6h.1kc.1@news.isolution.nl>

sanjeeb schreef:

> $#array gives the last index  of the array not the number of elements
> in the array, so you need to add 1 to $#array to get the number of
> elements.

For a Perl array, the number of elements is *normally* equal to the last
index plus one, but not *necessarily*.
Check out "$[" in perlvar.

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Sat, 30 Aug 2008 17:21:45 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: question about data structures - what does $# mean?
Message-Id: <g9c018.1d0.1@news.isolution.nl>

Ed schreef:

> #!/usr/bin/perl -w

Get rid of the "-w". Add:

    use strict;
    use warnings;


> my $d = {sid=>["lll"]};
> print $#{$d->{sid}}."\n";

There is no need to concatenate, you can just write 

    print $#{ $d->{sid} }, "\n";


> I expect this to print 1, but it prints 0.

Add a line with "$[ = 1;" somewhere above it, and it will. 
But you should really read perlvar first. 

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Sat, 30 Aug 2008 15:52:36 -0700
From: Joe Smith <joe@inwap.com>
Subject: Re: subprocess lifecycle
Message-Id: <Q8OdnRhdiLupUiTVnZ2dnUVZ_vzinZ2d@comcast.com>

Matthieu Imbert wrote:

> Currently, when I detect the timeout, I call die "error message". the
> message is displayed, but the script does not return until subprocesses
> finish (this may take several minutes, depending on what the
> subprocesses do).
> 
> Is there a way to force the end of all subprocesses when calling die?

perldoc -f exit
 ...
                The exit() function does not always exit immediately.  It calls
                any defined "END" routines first, but these "END" routines may
                not themselves abort the exit.  Likewise any object destructors
                that need to be called are called before the real exit.  If
                this is a problem, you can call "POSIX:_exit($status)" to avoid
                END and destructor processing.  See perlmod for details.

Also, {exec "/bin/false";}

	-Joe

P.S. Followup-To: comp.lang.perl.misc


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

Date: Sat, 30 Aug 2008 10:37:54 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: subprocesses lifecycle
Message-Id: <slrngbi1n2.20k.hjp-usenet2@hrunkner.hjp.at>

On 2008-08-29 22:01, C.DeRykus <ced@blv-sam-01.ca.boeing.com> wrote:
> On Aug 29, 12:53 pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
>> On 2008-08-29 12:19, C.DeRykus <c...@blv-sam-01.ca.boeing.com> wrote:
>>
>> > On Aug 28, 10:43 am, Hans Mulder <han...@xs4all.nl> wrote:
>> >> What you'd really want, is a way to tell C<open> that you don't want
>> >> C<close> to wait for this child.  As far as I know, there is currently
>> >> no simple way to achieve that.
>>
>> > Wouldn't backgrounding the task
>> > accomplish that:
>>
>> > open my $fd, "/some/task & |"
>> >   or die...
>>
>> Man, that's ugly!
>> But yes, I think that should work (although I haven't actually tried
>> it).
>>
>
> I'm not sure why a lone "&" tips the ugly balance :)

* Because it invokes a shell (which wouldn't otherwise be necessary)
* Because you have to think about it for a minute to figure out why it 
  works (if you figure it out at all - see Eric's post).


>> > However, child subprocesses would then need to be foregrounded with
>> > SIGCONT if the parent wants to kill them before exiting.
>>
>> No. SIGCONT doesn't "foreground" a process running in the background.
>> It continues a process which has been stopped. A running process can be
>> sent signals whether it is in the foreground or the background.
>>
>
> Yes, I mis-spoke but a SIGCONT actually is sent to the process
> group when a backgrounded job is
> moved to the foreground via "fg"
> to enable a terminal read for example.

"fg" moves a job to the foreground. But that job isn't necessarily in
the background, it can also be stopped. In the latter case of course a
SIGCONT is necessary.

	hp


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

Date: Sat, 30 Aug 2008 10:43:36 +0200
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: subprocesses lifecycle
Message-Id: <slrngbi21p.20k.hjp-usenet2@hrunkner.hjp.at>

On 2008-08-29 18:58, Eric Pozharski <whynot@pozharski.name> wrote:
> C.DeRykus <ced@blv-sam-01.ca.boeing.com> wrote:
> *SKIP*
>
>> open my $fd, "/some/task & |"
>>  or die...
>
>> However, child subprocesses would then need to be foregrounded with
>> SIGCONT if the parent wants to kill them before exiting.
>
> Backgrounding doesn't work.  I meant it doesn't matter.
>
> time perl -wle '
> open my $h, q{(sleep 1 ; /bin/echo -en xyz ) & |} or die $!;
> print `ps --cols 60 -O ppid t`;
> print <$h>;

You are waiting for input here - of course you can read "xyz\n" only
when the client writes it. So here is your 1 second delay. That has
nothing to do with close.

> print `ps --cols 60 -O ppid t`;'
[...]
> real    0m1.277s
> user    0m0.084s
> sys     0m0.080s
>       

If you remove print <$h>; the parent will exit immediately, but the
child will continue to run.

% time perl -wle '
open my $h, q{(sleep 1 ; /bin/echo -en xyz )& |} or die $!;
print `ps --cols 60 -O ppid t`;'
ps --cols 60 -O ppid t
  PID  PPID S TTY          TIME COMMAND
 2287 27943 R pts/3    00:00:00 perl -wle ?open my $h, q{(sl
 2288  2287 Z pts/3    00:00:00 [sh] <defunct>
 2289  2287 R pts/3    00:00:00 ps --cols 60 -O ppid t
 2290     1 S pts/3    00:00:00 sh -c (sleep 1 ; /bin/echo -
 2291  2290 S pts/3    00:00:00 sleep 1
27943 27942 S pts/3    00:00:00 zsh

perl -wle   0.00s user 0.01s system 71% cpu 0.011 total
  PID  PPID S TTY          TIME COMMAND
 2290     1 S pts/3    00:00:00 sh -c (sleep 1 ; /bin/echo -
 2291  2290 S pts/3    00:00:00 sleep 1
 2292 27943 R pts/3    00:00:00 ps --cols 60 -O ppid t
27943 27942 S pts/3    00:00:00 zsh

	hp


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

Date: Sat, 30 Aug 2008 05:56:41 -0700 (PDT)
From: "C.DeRykus" <ced@blv-sam-01.ca.boeing.com>
Subject: Re: subprocesses lifecycle
Message-Id: <2723d5df-5dd7-404f-9743-fc8a5d81350c@i20g2000prf.googlegroups.com>

On Aug 30, 1:37 am, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> On 2008-08-29 22:01, C.DeRykus <c...@blv-sam-01.ca.boeing.com> wrote:
>
>
>
> > On Aug 29, 12:53 pm, "Peter J. Holzer" <hjp-usen...@hjp.at> wrote:
> >> On 2008-08-29 12:19, C.DeRykus <c...@blv-sam-01.ca.boeing.com> wrote:
>
> >> > On Aug 28, 10:43 am, Hans Mulder <han...@xs4all.nl> wrote:
> >> >> What you'd really want, is a way to tell C<open> that you don't want
> >> >> C<close> to wait for this child.  As far as I know, there is currently
> >> >> no simple way to achieve that.
>
> >> > Wouldn't backgrounding the task
> >> > accomplish that:
>
> >> > open my $fd, "/some/task & |"
> >> >   or die...
>
> >> Man, that's ugly!
> >> But yes, I think that should work (although I haven't actually tried
> >> it).
>
> > I'm not sure why a lone "&" tips the ugly balance :)
>
> * Because it invokes a shell (which wouldn't otherwise be necessary)
> * Because you have to think about it for a minute to figure out why it
>   works (if you figure it out at all - see Eric's post).

 True,  the "&" forces a shell and complexity
 increases but, an "ugly" solution which
 requires only a single keystroke suddenly
 looks better even in a beauty contest..

 However, maybe Hans' suggested double fork
 is a more palatable solution.


>
> >> > However, child subprocesses would then need to be foregrounded with
> >> > SIGCONT if the parent wants to kill them before exiting.
>
> >> No. SIGCONT doesn't "foreground" a process running in the background.
> >> It continues a process which has been stopped. A running process can be
> >> sent signals whether it is in the foreground or the background.
>
> > Yes, I mis-spoke but a SIGCONT actually is sent to the process
> > group when a backgrounded job is
> > moved to the foreground via "fg"
> > to enable a terminal read for example.
>
> "fg" moves a job to the foreground. But that job isn't necessarily in
> the background, it can also be stopped. In the latter case of course a
> SIGCONT is necessary.
>

<off topic>
 Yes, a SIGCONT can come into play in both cases ---
 * when a foreground process is stopped (SIGTSTP)
 * a background process attempts to read from the
   terminal, gets a SIGTTIN,  and then is put
   into the foreground with "fg" to enable a
   terminal read.
</off topic>


--
Charles DeRykus



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

Date: Sat, 30 Aug 2008 13:10:41 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: subprocesses lifecycle
Message-Id: <1uino5x0ka.ln2@carpet.zombinet>

Peter J. Holzer <hjp-usenet2@hjp.at> wrote:
> On 2008-08-29 18:58, Eric Pozharski <whynot@pozharski.name> wrote:
*SKIP*
> If you remove print <$h>; the parent will exit immediately, but the
> child will continue to run.

> % time perl -wle '
> open my $h, q{(sleep 1 ; /bin/echo -en xyz )& |} or die $!;
> print `ps --cols 60 -O ppid t`;'
> ps --cols 60 -O ppid t
>  PID  PPID S TTY          TIME COMMAND
> 2287 27943 R pts/3    00:00:00 perl -wle ?open my $h, q{(sl
> 2288  2287 Z pts/3    00:00:00 [sh] <defunct>
> 2289  2287 R pts/3    00:00:00 ps --cols 60 -O ppid t
> 2290     1 S pts/3    00:00:00 sh -c (sleep 1 ; /bin/echo -
> 2291  2290 S pts/3    00:00:00 sleep 1
> 27943 27942 S pts/3    00:00:00 zsh

> perl -wle   0.00s user 0.01s system 71% cpu 0.011 total
>  PID  PPID S TTY          TIME COMMAND
> 2290     1 S pts/3    00:00:00 sh -c (sleep 1 ; /bin/echo -
> 2291  2290 S pts/3    00:00:00 sleep 1
> 2292 27943 R pts/3    00:00:00 ps --cols 60 -O ppid t
> 27943 27942 S pts/3    00:00:00 zsh

Yes, I know that that will exit immediately.  I've just attempted to
show that backgrounding doesn't work.

-- 
Torvalds' goal for Linux is very simple: World Domination


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

Date: Sat, 30 Aug 2008 10:20:30 +0200
From: magloca <magloca@mailinater.com>
Subject: Re: trouble writing a setuid script
Message-Id: <48b902ce$0$4245$6e1ede2f@read.cnntp.org>

Peter Michaux @ Saturday 30 August 2008 09:07:

> Hi,
> 
> I'm trying to write a setuid script and can't make it happen. I've
> trimmed it down to the very simple example below trying to have a
> logger.pl script add a message to a log file. This is my Bash
> transcript with all the pertinent details.
[code snipped]
> (I don't run into any errors when writing the same program in C.)

Yeah, I tried to do something like that once, too. Turned out the setuid
flag is ignored on scripts; it's only allowed on (binary) executables.
So that's also why your compiled C program works.

m.


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

Date: Sat, 30 Aug 2008 05:58:30 -0700 (PDT)
From: Peter Michaux <petermichaux@gmail.com>
Subject: Re: trouble writing a setuid script
Message-Id: <65d1d55b-0cfd-4b45-9211-e08536d51463@v13g2000pro.googlegroups.com>

On Aug 30, 1:20=A0am, magloca <magl...@mailinater.com> wrote:
> Peter Michaux @ Saturday 30 August 2008 09:07:
>
>
>
> > Hi,
>
> > I'm trying to write a setuid script and can't make it happen. I've
> > trimmed it down to the very simple example below trying to have a
> > logger.pl script add a message to a log file. This is my Bash
> > transcript with all the pertinent details.
> [code snipped]
> > (I don't run into any errors when writing the same program in C.)
>
> Yeah, I tried to do something like that once, too. Turned out the setuid
> flag is ignored on scripts; it's only allowed on (binary) executables.
> So that's also why your compiled C program works.

It seems it must be possible to write a setuid script because there is
a lot of fuss about it in "perldoc perlsec" which is also part of the
camel book.

Peter


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

Date: Sat, 30 Aug 2008 15:06:03 +0200
From: Joost Diepenmaat <joost@zeekat.nl>
Subject: Re: trouble writing a setuid script
Message-Id: <87ej46r6tg.fsf@zeekat.nl>

Peter Michaux <petermichaux@gmail.com> writes:

> It seems it must be possible to write a setuid script because there is
> a lot of fuss about it in "perldoc perlsec" which is also part of the
> camel book.

It's possible, but IIRC "most" linux systems ignore suid bits on
scripts. Not sure about other *nixes.

-- 
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/


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

Date: Sun, 31 Aug 2008 01:23:50 +1000
From: Grant <g_r_a_n_t_@dodo.com.au>
Subject: Re: trouble writing a setuid script
Message-Id: <2gpib490btip0k36ggaeo5rubrchv1lh21@4ax.com>

On Sat, 30 Aug 2008 05:58:30 -0700 (PDT), Peter Michaux <petermichaux@gmail.com> wrote:

>On Aug 30, 1:20 am, magloca <magl...@mailinater.com> wrote:
>> Peter Michaux @ Saturday 30 August 2008 09:07:
>>
>>
>>
>> > Hi,
>>
>> > I'm trying to write a setuid script and can't make it happen. I've
>> > trimmed it down to the very simple example below trying to have a
>> > logger.pl script add a message to a log file. This is my Bash
>> > transcript with all the pertinent details.
>> [code snipped]
>> > (I don't run into any errors when writing the same program in C.)
>>
>> Yeah, I tried to do something like that once, too. Turned out the setuid
>> flag is ignored on scripts; it's only allowed on (binary) executables.
>> So that's also why your compiled C program works.
>
>It seems it must be possible to write a setuid script because there is
>a lot of fuss about it in "perldoc perlsec" which is also part of the
>camel book.

Just add a C wrapper to call the script, something like:

#!/bin/bash
set -x
rm -f $1.c
rm -f ../$1.cgi
rm -f $1.cgi

echo "main () {
        execl (\"$PWD/$1\", \"$1\", (char *)0 );
        printf(\"Content-type: text/plain\\n\\n\");
        printf(\"$1.cgi: fatal - failed to start $1, wait, then refresh.\\n\");
}
" > $1.c

gcc $1.c -o $1.cgi
strip -s $1.cgi
chmod 04555 $1.cgi
mv $1.cgi ../
rm -f $1.c

Grant.
-- 
http://bugsplatter.id.au/


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

Date: Sat, 30 Aug 2008 16:45:47 GMT
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: trouble writing a setuid script
Message-Id: <c5uib45pounkfe83ksfcmvtgad95a8ilvc@4ax.com>

Peter Michaux <petermichaux@gmail.com> wrote:
>It seems it must be possible to write a setuid script [...]

Yes, it is. The question is, if you _OS_ will execute scripts as SUID or
not. This applies to any script, not just scripts written in Perl.

jue


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

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 V11 Issue 1828
***************************************


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