[25000] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 7250 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 14 18:12:03 2004

Date: Thu, 14 Oct 2004 15:10:14 -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           Thu, 14 Oct 2004     Volume: 10 Number: 7250

Today's topics:
        python for perl programmers? (Chris Richmond - MD6-FDC ~)
    Re: python for perl programmers? <postmaster@castleamber.com>
        References to an array in a foreach <mail.david@dsl.pipex.com>
    Re: References to an array in a foreach <dwall@fastmail.fm>
    Re: References to an array in a foreach <1usa@llenroc.ude.invalid>
    Re: References to an array in a foreach <todd@espresso.local>
    Re: References to an array in a foreach <nobull@mail.com>
    Re: Restricting a program to one running instance <bik.mido@tiscalinet.it>
    Re: Restricting a program to one running instance <bik.mido@tiscalinet.it>
        setting cookies (mod_perl) <usenet@root.mailshell.com>
    Re: setting cookies (mod_perl) <nobull@mail.com>
    Re: String and Array Programming in Perl (DeveloperGuy)
    Re: String and Array Programming in Perl <nobull@mail.com>
    Re: String and Array Programming in Perl <whitey@newmail.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 14 Oct 2004 20:26:16 +0000 (UTC)
From: crichmon@filc8046.fm.intel.com (Chris Richmond - MD6-FDC ~)
Subject: python for perl programmers?
Message-Id: <ckmnd8$ro1$1@news01.intel.com>

Hi Folks,

   Not out of choice, I'm going to have to learn python on
Windows.  I've used perl on Unix forever and totally hate
the idea, but like I said, no choice.

What I'm looking for is a some sort of a Learning python
for perl programmers.  I don't think I need plain old
programming help, but something that will translate all
those nice perl concepts into the equivelant python
constructs.  I don't have a *.python.advocacy on this
news server, otherwise I would have started there.
I've already checked out the docs on python.org, and didn't
really like what I saw.

I don't get: you have to tell a subroutine a variable is a
global if you want to write it, otherwise its local, else
not required if its read only?  WTF?

Thx, Chris

-- 
 Chris Richmond         | I don't speak for Intel & vise versa    



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

Date: 14 Oct 2004 21:46:25 GMT
From: John Bokma <postmaster@castleamber.com>
Subject: Re: python for perl programmers?
Message-Id: <Xns9582AA9FA9DCCcastleamber@130.133.1.4>

Chris Richmond - MD6-FDC ~ wrote:

> Hi Folks,
> 
>    Not out of choice, I'm going to have to learn python on
> Windows.  I've used perl on Unix forever and totally hate
> the idea, but like I said, no choice.
> 
> What I'm looking for is a some sort of a Learning python
> for perl programmers.  I don't think I need plain old
> programming help, but something that will translate all
> those nice perl concepts into the equivelant python
> constructs.

I personally think that is a bad idea.

> I don't have a *.python.advocacy on this
> news server, otherwise I would have started there.
> I've already checked out the docs on python.org, and didn't
> really like what I saw.

That happens with every new language you learn :-D. I wonder how many 
people really liked Perl the first time ;-)

> I don't get: you have to tell a subroutine a variable is a
> global

our ?

> if you want to write it, otherwise its local, else
> not required if its read only?  WTF?

Laziness? :-)


-- 
John                               MexIT: http://johnbokma.com/mexit/
                           personal page:       http://johnbokma.com/
        Experienced programmer available:     http://castleamber.com/
            Happy Customers: http://castleamber.com/testimonials.html


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

Date: Thu, 14 Oct 2004 19:59:52 +0000
From: David Green <mail.david@dsl.pipex.com>
Subject: References to an array in a foreach
Message-Id: <pan.2004.10.14.19.59.52.96858@dsl.pipex.com>

Hey all,

I have a problem with references to arrays and would be grateful if
someone could give me some help or alternatively propose a better method
to use in solving my problem.

Take for example the following code:

my @animals = ( "\@reptiles", "\@mammals", "\@birds" );
my @birds = ( "owl", "swan", "pelican" );
my @reptiles = ( "crocodile", "gecko" );
my @mammals = ( "fox", "cheeta", "man" );

What I think I've done here is set up an array called @animals which
contains references to the three arrays @birds, @reptiles and @mammals.

What I would like to do is use a pair of <foreach> loops to read each
value from the three arrays in sequence (exact order unimportant).

Defining two worker variables:
my $thiskey, $thatkey;

foreach $thatkey (@animals) {
	
	foreach $thiskey (@$thatkey) {   # Here I try to dereference the array
	print $thiskey . ",";            # and presumably fail?
	};
};

I would expect the output:
"crocodile,gecko,fox,cheeta,man,owl,swan,pelican", but instead get no
output. To even get this code to run I have to remove <use strict> else
Perl refuses to run the code.

The ultimate aim in this seemingly strange setup is to allow an unlimited
number of members to @animals and an unlimited number of animals in each
different 'kingdom'. In reality i'm actually using such a system to manage
a lot of metadata for a package manager I'm working on.

How can I get the output I'm after (preferably retaining strict refs) or
alternatively provide data organised in such a way. If any more
information helps or I'm unclear please let me know,

Thanks,
Dave

-- 
David Green (mail.david@dsl.pipex.com)
Hands up for human rights!
http://www.amnesty.org



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

Date: Thu, 14 Oct 2004 19:54:15 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: References to an array in a foreach
Message-Id: <Xns9582A1C96B387dkwwashere@216.168.3.30>

David Green <mail.david@dsl.pipex.com> wrote:

> I have a problem with references to arrays and would be grateful
> if someone could give me some help or alternatively propose a
> better method to use in solving my problem.
> 
> Take for example the following code:
> 
> my @animals = ( "\@reptiles", "\@mammals", "\@birds" );

@animals now contains the strings '@reptiles', '@mammals', and 
'@birds', but no references.

> my @birds = ( "owl", "swan", "pelican" );
> my @reptiles = ( "crocodile", "gecko" );
> my @mammals = ( "fox", "cheeta", "man" );

It's probably a good thing that @animals only contains strings, 
because the arrays you meant to reference didn't exist yet.
 
> What I think I've done here is set up an array called @animals
> which contains references to the three arrays @birds, @reptiles
> and @mammals. 

Nope, sorry.


> What I would like to do is use a pair of <foreach> loops to read
> each value from the three arrays in sequence (exact order
> unimportant). 

I'd suggest reading perllol.


> Defining two worker variables:
> my $thiskey, $thatkey;
> 
> foreach $thatkey (@animals) {
>      
>      foreach $thiskey (@$thatkey) {   # Here I try to dereference
>      the array print $thiskey . ",";            # and presumably
>      fail? 
>     };
>};
> 
> I would expect the output:
> "crocodile,gecko,fox,cheeta,man,owl,swan,pelican", but instead get
> no output. To even get this code to run I have to remove <use
> strict> else Perl refuses to run the code.

Don't do that. Perl knows more about it than you do at the moment. 
:-)

I'm not the best explainer (or at least I'm too lazy to type a lot of 
stuff), so how about trying this:

    use strict;
    use warnings;

    my @birds    = qw(owl swan pelican);
    my @reptiles = qw(crocodile gecko);
    my @mammals  = qw(fox cheeta man);

    my @animals  = \( @reptiles, @mammals, @birds );

    print join ', ', map { @$_ } @animals;



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

Date: 14 Oct 2004 20:00:03 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: References to an array in a foreach
Message-Id: <Xns9582A2C548DB0asu1cornelledu@132.236.56.8>

David Green <mail.david@dsl.pipex.com> wrote in
news:pan.2004.10.14.19.59.52.96858@dsl.pipex.com: 

> Hey all,
> 
> I have a problem with references to arrays and would be grateful if
> someone could give me some help or alternatively propose a better
> method to use in solving my problem.
> 
> Take for example the following code:
> 
> my @animals = ( "\@reptiles", "\@mammals", "\@birds" );
> my @birds = ( "owl", "swan", "pelican" );
> my @reptiles = ( "crocodile", "gecko" );
> my @mammals = ( "fox", "cheeta", "man" );
> 
> What I think I've done here is set up an array called @animals which
> contains references to the three arrays @birds, @reptiles and
> @mammals. 

That is absolutely one hundred percent wrong. Where have you every seen 
such syntax? You can't make stuff up and expect it to work you know.

What you have done instead is to create an array called @animals 
containing the strings '@reptiles', '@mammals', and '@birds'.

> What I would like to do is use a pair of <foreach> loops to read each
> value from the three arrays in sequence (exact order unimportant).
> 
> Defining two worker variables:
> my $thiskey, $thatkey;

Always declare your variables in the smallest possible scope. Don't give 
them misleading names (such as $key).

> foreach $thatkey (@animals) {
>      
>      foreach $thiskey (@$thatkey) {   # Here I try to dereference the
>      array print $thiskey . ",";            # and presumably fail?

I am speechless. 

You can't make stuff up like this.

> I would expect the output:
> "crocodile,gecko,fox,cheeta,man,owl,swan,pelican", but instead get no
> output. 

Why would you expect that?

> To even get this code to run I have to remove <use strict>
> else Perl refuses to run the code.

It is a BAD SIGN when you have to remove use strict to get your code to 
run. Why don't you instead read the message you get? WHile you are at it, 
you could also add a simple use diagnostics to get a more detailed 
description.
 
> The ultimate aim in this seemingly strange setup is to allow an
> unlimited number of members to @animals and an unlimited number of
> animals in each different 'kingdom'. In reality i'm actually using
> such a system to manage a lot of metadata for a package manager I'm
> working on. 

Please let us know what it is so we know never ever under any 
circumstances to use it.

> If any more information helps or I'm unclear please let me know,

You are very clear. You refuse to read.

Have you looked at perldoc perlref?

Sinan.


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

Date: Thu, 14 Oct 2004 15:14:30 -0500
From: Todd de Gruyl <todd@espresso.local>
Subject: Re: References to an array in a foreach
Message-Id: <slrncmtnhg.f2r.todd@espresso.local>

On Thu, 14 Oct 2004, David Green <mail.david@dsl.pipex.com> wrote:

> my @animals = ( "\@reptiles", "\@mammals", "\@birds" );

Two problems:
The format "\@array" gives you the same thing as '@array', in other
words, a literal ampersand.  What you probably meant to do is:  

  my @animals = ( \@reptiles, \@mammals, \@birds );

The second problem here is that you are referencing arrays that don't
exist yet.  If you had started out with:

  use warnings;
  use strict;

then Perl would have told you some of the problems (once you fixed the
problem with the quotes, anyway). 

> my $thiskey, $thatkey;

This should probably be:

  my ($thiskey, $thatkey); # [1]

or better yet, move the my into the foreach statements:
 
  foreach my $thatkey (@animals) {
    foreach my $thiskey (@$thatkey) { 
      print $thiskey . ",";          
    };
  };


### Here it is with the errors fixed:

use warnings;
use strict;

my @birds = ( "owl", "swan", "pelican" );
my @reptiles = ( "crocodile", "gecko" );
my @mammals = ( "fox", "cheeta", "man" );
my @animals = ( \@reptiles, \@mammals, \@birds );
 
foreach my $thatkey (@animals) {
	foreach my $thiskey (@$thatkey) { 
 	  print $thiskey . ",";          
 	};
};

###

[1] from perldoc -f my: If more than one value is listed, the list must
    be placed in parentheses.  

-- 
Todd de Gruyl


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

Date: Thu, 14 Oct 2004 21:06:56 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: References to an array in a foreach
Message-Id: <ckmm0h$5dr$1@sun3.bham.ac.uk>



David Green wrote:

> Hey all,
> 
> I have a problem with references to arrays and would be grateful if
> someone could give me some help or alternatively propose a better method
> to use in solving my problem.
> 
> Take for example the following code:
> 
> my @animals = ( "\@reptiles", "\@mammals", "\@birds" );
> my @birds = ( "owl", "swan", "pelican" );
> my @reptiles = ( "crocodile", "gecko" );
> my @mammals = ( "fox", "cheeta", "man" );
> 
> What I think I've done here is set up an array called @animals which
> contains references to the three arrays @birds, @reptiles and @mammals.

No you have not.  @animals is an array of strinf.  If you want it to be 
an array of references you'd need to remove the quotes from the first line.

> 
> What I would like to do is use a pair of <foreach> loops to read each
> value from the three arrays in sequence (exact order unimportant).
> 
> Defining two worker variables:
> my $thiskey, $thatkey;

Firstly you mean "declaring" not "defining".  Secondly you are suffering 
from the unfortuante affliction of "premature declaration".  You should 
always declare all variables in the smallest applicable scope unless you 
have a positive reason to do otherwise.  In the case of for-loop 
iterator variables the correct place to delare these variables is in the 
for statement itself.

> foreach $thatkey (@animals) {
> 	
> 	foreach $thiskey (@$thatkey) {   # Here I try to dereference the array
> 	print $thiskey . ",";            # and presumably fail?
> 	};
> };

If @animals where an array of array references that code would would 
work just fine.

> 
> I would expect the output:
> "crocodile,gecko,fox,cheeta,man,owl,swan,pelican", but instead get no
> output. To even get this code to run I have to remove <use strict> else
> Perl refuses to run the code.

If your smoke alarm is sounding, put out the fire, do not remove the 
batteries.  Did you actually read the error Perl gave you?  It told you 
what was wrong.  It told you that you were using a string where you 
needed a reference.



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

Date: Fri, 15 Oct 2004 00:06:22 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Restricting a program to one running instance
Message-Id: <cfntm0dei9ae6htnrm9094nbrdui4ag20v@4ax.com>

On Wed, 13 Oct 2004 22:28:01 +0100, Ben Morrow <usenet@morrow.me.uk>
wrote:

>> I apologize if the subject is not really appopriate to this post, but
>> I really couldn't think of a better one...
>
>How's that? :)

Lack of fantasy? ;-)

Also, since I had apologized, you may have continued that thread
anyway, ot at least include in the subject line an indication of the
change, a la '[was: "something else"]'.

I say so because I was inadvertently skipping over this thread... (of
course the subject eventually did catch my attention!)

>> However (also) for learning purposes, I've been thinking of improving
>> it to cope with the unlikely possibility that a badly crashed machine
>> prevented my signal handlers or C<END> block to take care of lockfile
>> removal.
>
>What I have done in the past in this situation is
>
>sysopen my $LOCK, $lockfile, WR_ONLY | O_CREAT, 0600
>    or die "can't open lockfile: $!";
>flock $LOCK, LOCK_EX | LOCK_NB
>    or die "I'm already running!";

Well, one reason I hadn't used flock() in the first place is that I'm
on nfs and I know it not to be an atomic operation there. Also, this
is to be called sporadically (say when I login, e.g. from my
~/.bash_profile), so that doesn't seem to be a problem.

>If you wanted to be really careful you could check that $! ==
>EWOULDBLOCK before concluding you were already running.

Good to know, I didn't know about this...

>The advantage of this, obviously, is that the kernel will take care of
>releasing the lock however the process terminates (and if the whole
>machine goes down the lock's *certainly* gone :) ).

I see...

>> (i) writing the pid of the running instance into the lockfile and
>> check for its existence,
>> (ii) once (i) is done, then also check that the process corresponding
>> to the stored pid corresponds to a program called like $0.
>> 
>> Now, the questions are: would (i) be a good idea?
>
>Yes. It would be anyway, so you can find it and kill it easily if it
>gets jammed.

Fine! This will be easily accomplished.

>> Would (ii) be a good idea too?
>
>Not really. The risk that either it will be a process that matches that
>shouldn't (not *that* likely, but rather more so if someone's being
>malicious) or a process that doesn't match that should (consider
>starting the program as 'prog', '~/bin/prog', 'perl ../bin/prog'; I have

I see your point. In this particular case the program is started from
~/bin so there's be no problem of this kind. However your
considerations are definitely worth taking into account in a (not too
much) hypothetical general case.

>If you make the pidfile secure, then you can simply check if that pid is
>still running (kill 0 => $pid); if you're worried about pid wraparound
>then you really need to get some positive check that the process
>concerned is one of yours, such as (in the extreme case) connecting to a
>Unix-domain socket the other copy is listening on and performing a
>secure authentication exchange.

Huh?!? No, it's not really the case for *this* particular program! :-)

<OT>
What is the English idiom, provided one exists, but I strongly suspect
so, for "(ab)using an exaggerate tool for a task that doesn't really
deserve it?" In Italian there's an expression that could be roughly
translated as "to use a bazooka to shot a robin".
</OT>

>> above questions are positive, then when I open a file with sysopen(),
>> can/should I subsequently use the "standard" IO operators (i.e. those
>> I'd use with an open()ed file) and functions to read and write to it?
>
>Yes. Perl's sysopen does an open(2) and then a fdopen(3) (or the PerlIO
>equivalent), so the FH you get back is exactly equivalent to one from
>open.

OK! This is (most of) all I wanted to know...

>> As for the point (ii) above, since the program is to be run on Linux
>> anyway, it would seem sensible to me to read the relevant info from
>> /proc myself (don't know if it could be doable under other UNICES
>
>Plan9? :)

BTW: I don't know anything about Plan9, apart having heard its name
before. What do you mean? I suppose it {does,did}n't have anything
like /proc, {does,did} it?

>I'd probably recommend using a module on principle (the Proc::
>hierachy); in practice, I'd probably just grep it out of
>/proc/pid/cmdline :)

That's what I'm about to do too, at least for experimenting.

>> Last, since I'm not much confident with this kind of programming,
>> could you please be so kind and post some explicit minimal code
>> snippet?
>
>Oh, come now, the advantage of /proc is that it's just like ordinary
>files; and I'm quite sure you can write a program to read a pid from a

I meant on the whole subject (i.e. say, "(i)+(ii)"), but...

>file, open another file based on that, and check if it matches
>/^\Q$^X\E\s+\Q$0\E(?:\s|$)/ :).

 ...you're right: I realized that I can do all this myself!


TY very much,
Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Fri, 15 Oct 2004 00:06:23 +0200
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Restricting a program to one running instance
Message-Id: <hdntm0hie7jii35v9fkssmhi1np1gepv0e@4ax.com>

On 14 Oct 2004 11:33:42 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno
Siegel) wrote:

>> If you make the pidfile secure, then you can simply check if that pid is
>> still running (kill 0 => $pid); if you're worried about pid wraparound
>> then you really need to get some positive check that the process
>> concerned is one of yours, such as (in the extreme case) connecting to a
>> Unix-domain socket the other copy is listening on and performing a
>> secure authentication exchange.
>
>That should only be necessary if the lock-able process must run under
>various user-id's.  If it is always run under the same id, the lock
>file permissions should only allow the owner to open the file.  Also,
>(perhaps needless to say) the lock file must be specific to that process,
>in particular, no other program (of yours) should ever lock that file.
>Then, unless the uid is compromised, if there is a lock, it *must*
>belong to the one process that can hold it.  

Sorry if I'm being overly dumb, but when you say "lock" do you mean
lock() or simply a suitably sysopen()ed lockfile or both? (Intuition
suggests me the latter, but I may well be wrong!)

Also, as I said in the other post, in this particular case the program
is started ~/bin/. I can resonably imagine writing "similar" scripts
to be installed "system-wide", but then I'd probably allow one
instance per user per machine, so I'd simply stick with either using a
per HOME lockfile or a /tmp per-user lockfile (as I've seen other
programs doing).


Michele
-- 
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
 .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


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

Date: Thu, 14 Oct 2004 20:15:06 +0200
From: "un" <usenet@root.mailshell.com>
Subject: setting cookies (mod_perl)
Message-Id: <416ec22a$1@news.broadpark.no>

I'm trying to learn mod_perl 2

I've created the following script:
package smmod;

use strict;
use warnings;

use Apache::RequestRec ();
use Apache::RequestIO ();
use Apache::Const -compile => qw(OK);

sub handler {
  my $r = shift;
  $r->headers_out->add('Set-Cookie' => 'myCookie=vanilla'); # BUG ?
  $r->content_type('text/plain');
  $r->print("Hello");
  return Apache::OK;
}
1;

And I've added the following to my apache conf file:
PerlModule smmod
<Location /sm>
  SetHandler modperl
  PerlResponseHandler smmod
</Location>

Yet I keep getting this error when I use the script:
[Thu Oct 14 19:42:56 2004] [error] [client XXXX] Can't locate object method 
"add" via package "APR::Table" at C:/Program Files/Apache 
Group/Apache2/lib/perl/smmod.pm line 13.\n

It's probably simple, but I'm not able to figure out what's wrong. Anybody?

Thanks in advance




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

Date: Thu, 14 Oct 2004 20:56:20 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: setting cookies (mod_perl)
Message-Id: <ckmlcl$54p$1@sun3.bham.ac.uk>



un wrote:

>   $r->headers_out->add('Set-Cookie' => 'myCookie=vanilla'); # BUG ?

> Yet I keep getting this error when I use the script:
> [Thu Oct 14 19:42:56 2004] [error] [client XXXX] Can't locate object method 
> "add" via package "APR::Table" at C:/Program Files/Apache 
> Group/Apache2/lib/perl/smmod.pm line 13.\n
> 
> It's probably simple, but I'm not able to figure out what's wrong. Anybody?

Try

  require APR::Table;

or

  use APR::Table ();

The mod_perl module often creates objects of classes for which it has 
not (full) loaded the methods.

I've never really know why the mod_perl docs use use() rather then 
require().



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

Date: 14 Oct 2004 11:30:31 -0700
From: Phillip.Small@gmail.com (DeveloperGuy)
Subject: Re: String and Array Programming in Perl
Message-Id: <64f06fa2.0410141030.5bca909e@posting.google.com>

I have looked in the online sections you have recommended but what is
wrong with the script?  What is wrong with push commands and how can I
answer the following from the ps-aef command?
#and the following questions have to be answered
  #how many users are running programs?
  #what is the total time used by each user in hours: minutes?
  #who is running the longest process, list user, time and name of
program

#! /usr/bin/perl



open PS, "data.txt" or die "could not open file  $!";
my @tmpline;



foreach $i (<data.txt>) {
my @users = @tmpline[i];
 if @tmpline[0] != @users
 push my @users, $_;

}
 
  

Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrncmoeug.vlt.tadmc@magna.augustmail.com>...
> DeveloperGuy <Phillip.Small@gmail.com> wrote:
> 
> > I am very very new to Perl 
> 
> 
> We will still expect that you use Perl rather than something
> merely Perlish-looking.
> 
> 
> > and am trying automate a process in my AIX
> > Unix box.  I issed the command ps -aef and sent it to a file.  
> 
> 
> You can do that from within Perl itself, no need for a file.
> 
>    my @ps_lines = `ps -aef`;     # backwards single quotes
> or
>    my @ps_lines = qx/ps -aef/;   # backwards single quotes in disguise
> or
>    open PS, 'ps -aef|' or die "could not run ps  $!";
>    while ( <PS> ) ...
> 
> 
> > How do
> > I get how many different users running programs, the total time for
> > each user in hours:minutes format, and who is running the longest
> > process and the program name?  
> 
> 
> By parsing the output of the ps command.
> 
> You might want to use Perl's unpack() or substr() functions
> to help you with that.
> 
> 
> > I am not familiar with using the loops.
> 
> 
> Then become familiar with using the loops, they are documented in:
> 
>    perldoc perlsyn
> 
> 
> >  I know that I can probably use the date command to specify the date. 
> 
> 
> You can do that from within Perl too, no need for an external date program.
> 
>    perldoc -f localtime
>    perldoc -f gmtime
> 
> 
> > This is where I am stuck thus far. Please help anyone...
> > 
> > #! /usr/bin/perl
> > 
> > use strict;
> 
> 
> When you put that in your programs you are making a promise:
> 
>    I promise to declare my variables before using their short names.
> 
> If you break your promise, then perl will refuse to run your program.
> 
> 
> > use warnings;
> > 
> > @users;
> 
> 
> You have not declared that variable, so perl refuses to run your program.
> 
>    my @users;
> 
> 
> > @tmpfile = OPEN(DataFileHandle, /home/smallp/data.txt);
> 
> 
> Perl does not have an OPEN() function, only an open() function.
> 
> Case matters.
> 
> Put 'quotes' around your strings.
> 
> open() returns a single thing, no need for an array to hold its return value.
> 
> It is a convention to use all UPPER CASE for filehandles.
> 
> You should always, yes *always*, check the return value from open()
> to ensure that you actually got what you asked for:
> 
>    open DATA_FILEHANDLE, '/home/smallp/data.txt' or 
>       die "could not open '/home/smallp/data.txt'  $!";
> 
> 
> Your code never makes use of the filehandle. You will need to *read*
> from it to get the data to process...
> 
> 
> > $tmpfile[0];
> > $users[0];
> 
> 
> Those are do-nothing statements, they have no useful effect.
> 
> What were you hoping those 2 lines of code would do for you?
> 
> 
> >    for ($count= 0; $count <= $#users; $count++;) {
> 
> 
> A more Perlish way to get the same thing is:
> 
>    foreach my $count ( 0 .. $#users ) {
> 
> 
> >      If $tmpline[0] eq $users[i] 
> 
> 
> Perl does not have an "If" keyword, only an "if" keyword.
> 
> Case (still) matters.
> 
> You need (parenthesis) around the condition in an if statement.
> 
> 
> >       if TRUE then exit
> 
> 
> Perl does not even have a "then" keyword, nor a "TRUE" keyword.
> 
> This is not Perl code. What language is it?


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

Date: Thu, 14 Oct 2004 19:59:28 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: String and Array Programming in Perl
Message-Id: <ckmi20$3k8$1@sun3.bham.ac.uk>



DeveloperGuy rudely vomits TOFU in our faces:

[ TOFU corrected ]

> Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrncmoeug.vlt.tadmc@magna.augustmail.com>...
> 
>>DeveloperGuy <Phillip.Small@gmail.com> wrote:
>>
>>
>>>I am very very new to Perl 
>>
>>
>>We will still expect that you use Perl rather than something
>>merely Perlish-looking.
>>
> I have looked in the online sections you have recommended but what is
> wrong with the script? 

Like Tad said, it is not writen in  Perl.

Before asking human beings to help you should first get all the help you 
can from your computer.  Put

   use strict;
   use warnings;

at the top and fix _all_ the mistakes Perl tells you about.

Only then ask humans to help.

Do not post TOFU.  It is very rude.  When you are very rude then people 
will be less likely to want to help you.

  What is wrong with push commands and how can I
> answer the following from the ps-aef command?
> #and the following questions have to be answered
>   #how many users are running programs?
>   #what is the total time used by each user in hours: minutes?
>   #who is running the longest process, list user, time and name of
> program
> 
> #! /usr/bin/perl
> 
> 
> 
> open PS, "data.txt" or die "could not open file  $!";
> my @tmpline;
> 
> 
> 
> foreach $i (<data.txt>) {
> my @users = @tmpline[i];
>  if @tmpline[0] != @users
>  push my @users, $_;

That is not the syntax of the Perl if statement.


The syntax of the Perl if statement can be found in the 'perlsyn' manual.

http://search.cpan.org/~nwclark/perl-5.8.5/pod/perlsyn.pod#Compound_Statements

There appart from that there are many mistakes Perl would tell you about 
if you let it.



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

Date: Thu, 14 Oct 2004 16:54:46 -0500
From: Whitey Johnson <whitey@newmail.net>
Subject: Re: String and Array Programming in Perl
Message-Id: <pan.2004.10.14.21.54.45.631174@newmail.net>

On Thu, 14 Oct 2004 10:28:26 -0700, DeveloperGuy muttered incoherently:

> Okay, I finally got the script to read the file.  
> 
> #! /usr/bin/perl
<snip>
> open(DataFileHandle, "/home/smallp/data.txt");
> 
> @tmpfile = <DataFileHandle>;
> @users;
> $users[0];
> 
> foreach $i (@tmpfile)
> {
> print "$i\n"; 
> }
> close DataFileHandle;

I am kinda new to Perl myself but wouldn't it be quicker to do it this way:
----
my @pslist = `ps -aux`;

foreach (@pslist) {
                print "$_\n";
        }
---
That way you don't have to worry about running ps from the command line
you can get a new output from it everytime you run your script.



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

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 7250
***************************************


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