[9269] in Perl-Users-Digest
Perl-Users Digest, Issue: 2864 Volume: 8
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 13 12:07:18 1998
Date: Sat, 13 Jun 98 09:00:28 -0700
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, 13 Jun 1998 Volume: 8 Number: 2864
Today's topics:
Re: ascii to hex (Mick Farmer)
Automatically removing newsgroups from my active file (Andreas Mitsis)
Can you strip $z to get the local user? <admin@asarian-host.org>
Re: Can you strip $z to get the local user? (Jonathan Stowe)
Re: Certified Perl Programmers (Chris Nandor)
Re: CFV: comp.lang.perl.moderated moderated (M.J.T. Guy)
Re: CGI/Perl Error (Jonathan Stowe)
email alias for beta code user? <ctc@cisco.com>
Re: Help a newbie with arrays! (I think ... ) <psdspss@execpc.com>
how do I save a perl file (HHaque)
Re: how do I save a perl file (Jonathan Stowe)
Re: how do I save a perl file <psdspss@execpc.com>
leading zeros <al-judy@ix.netcom.com>
Re: leading zeros <tchrist@mox.perl.com>
Re: leading zeros <al-judy@ix.netcom.com>
Re: leading zeros (Jonathan Stowe)
Re: NEED PERL SCRIPT WRITTEN <postmaster@castleamber.com>
Re: novice problem? (Jonathan Stowe)
Re: perl script emulates "last" command (Jonathan Stowe)
Re: perl script emulates "last" command <tchrist@mox.perl.com>
Re: Puzzle challenge (clarified) (Tim Bunce) (Tim Bunce)
Re: Puzzle challenge (clarified) (Tim Bunce) (Tim Bunce)
Q: How best to implement plug-ins for a Perl script? camel@no.spam.leeds.ac.uk
refs and nested hashes (Gossamer)
Return values of comparison operators (Andrew Pimlott)
Re: REVIEW: Perl CGI Programming - No Experience Requir (Jonathan Stowe)
Re: Split string into newline-terminated lines <jbl@tfs.com>
Re: splitting config file <jdf@pobox.com>
Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 12 Jun 1998 09:59:06 GMT
From: mick@picus.dcs.bbk.ac.uk (Mick Farmer)
Subject: Re: ascii to hex
Message-Id: <EuFnqI.M35@mail2.ccs.bbk.ac.uk>
Dear Jason,
Use something like this script.
$string = shift
or die "Missing argument\n";
$format = '%2x' x length($string);
printf "$format\n", map(ord, split(//, $string));
Regards,
Mick
------------------------------
Date: 13 Jun 1998 11:10:30 GMT
From: mitsis@phobos.mz.rhein-main.de (Andreas Mitsis)
Subject: Automatically removing newsgroups from my active file
Message-Id: <slrn6o4nh5.419.mitsis@phobos.mz.rhein-main.de>
What's the best way to remove newsgroups from my active file using
ctlinnd from INN-1.7? I did write a little perl script to achieve
this:
#!/usr/bin/perl
foreach $file (@ARGV) {
open(FILE, $file) || warn "Can't open $file: $!\n";
while(<FILE>) {
push(@newslist, $_);
}
}
foreach $i (@newslist) {
exec '/usr/news/bin/ctlinnd', 'rmgroup', $i;
}
The newsgroups that I want to remove are stored in different files. For
example, I'd like to make a call like this
$ newsrm.pl news.list
where 'newsrm.pl' is the script above and news.list contains the newsgroups
I want to remove. Obviously it doesn't work this way. Although I can print
every newsgroup in news.list if I try to substitute the last foreach
statement with the following
foreach $i (@newslist) {
print $i;
}
What's wrong with the above statement in 'newrm.pl'. Any suggestions?
Thanks a lot,
Andreas Mitsis
------------------------------
Date: Sat, 13 Jun 1998 16:39:26 +0200
From: "Mark" <admin@asarian-host.org>
Subject: Can you strip $z to get the local user?
Message-Id: <199806131441.IAA15006@asarian-host.org>
Hi,
A dumb question maybe, but I was wondering if next to the user
home-directory defined in sendmail, $z, there is also a variable available
within sendmail.cf that holds just the name of the local user? So, if $z
equals "/usr/home/myuser", I was hoping to find a variable within
sendmail.cf that holds just "myuser". I want to use it so I can instruct
sendmail to use a fixed .forward directory. So, instead of:
O ForwardPath=$z/.forward
Something like:
O ForwardPath='/usr/home'./$myuser/.forward
Your help is much appreciated. :)
- Mark
--
For more information about this posting service, contact:
help@asarian-host.org -- for instant general information about our services
admin@asarian-host.org -- for the server's administrator
nclark@asarian-host.org -- for our PR manager
abuse@asarian-host.org -- for abuse of this posting service
If you wish to get an anonymous email/posting account, visit our sign-up page:
http://asarian-host.org/emailform.html
------------------------------
Date: Sat, 13 Jun 1998 15:49:54 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: Can you strip $z to get the local user?
Message-Id: <3582949b.4348082@news.btinternet.com>
On Sat, 13 Jun 1998 16:39:26 +0200, Mark wrote :
>Hi,
>
>A dumb question maybe, but I was wondering if next to the user
>home-directory defined in sendmail, $z, there is also a variable available
>within sendmail.cf that holds just the name of the local user? So, if $z
>equals "/usr/home/myuser", I was hoping to find a variable within
>sendmail.cf that holds just "myuser". I want to use it so I can instruct
>sendmail to use a fixed .forward directory. So, instead of:
>
>O ForwardPath=$z/.forward
>
>Something like:
>
>O ForwardPath='/usr/home'./$myuser/.forward
And the Perl question is ?
I think you must have made a typo for comp.mail.sendmail when you sent
this.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Sat, 13 Jun 1998 13:33:29 GMT
From: pudge@adelphia.net (Chris Nandor)
Subject: Re: Certified Perl Programmers
Message-Id: <pudge-1306980930450001@dynamic174.ply.adelphia.net>
In article <6lt43d$d8i$1@client3.news.psi.net>, abigail@fnx.com wrote:
# Well, you could write a program that harvests email addresses, and then
# sends certificates to those addresses....
#
# Or maybe not :)
LOL!
--
Chris Nandor mailto:pudge@pobox.com http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6'])
------------------------------
Date: 13 Jun 1998 13:27:29 GMT
From: mjtg@cus.cam.ac.uk (M.J.T. Guy)
Subject: Re: CFV: comp.lang.perl.moderated moderated
Message-Id: <6ltuo1$l1k$1@pegasus.csx.cam.ac.uk>
John Stanley <stanley@skyking.OCE.ORST.EDU> wrote:
>Yes, and when you are in DejaNews, you might wander through the
>perl.porters-gw archive. That is where the request for volunteers was
>made. If you weren't on that mailing list/newsgroup, you didn't know
>there was a chance to volunteer, until the RFD with the list of proposed
>moderators was posted.
You could always have contacted the proponent after the first RFD.
That's what I did.
Mike Guy
------------------------------
Date: Sat, 13 Jun 1998 14:48:31 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: CGI/Perl Error
Message-Id: <35828664.911260@news.btinternet.com>
On Fri, 12 Jun 1998 23:41:44 +0800, Walter Archie wrote :
>If anyone does actually read this message please take the time to help
>me.
>
>Goto: http://www.thealienz.com/cgi-bin/board/board.cgi
>
>Please can someone help me understand these errors of my script. If
>someone requires my script please let me know.
>
Just for those who cant or wont go the site:
<quote>
CGI Error
The specified CGI application misbehaved by not returning a complete
set of HTTP headers. The headers it did return are:
Literal @search now requires backslash at
f:\thealienz\cgi-bin\board\board.cgi line 445, within string
Missing right bracket at f:\thealienz\cgi-bin\board\board.cgi line
971, at end of line
syntax error at f:\thealienz\cgi-bin\board\board.cgi line 971, at EOF
Execution of f:\thealienz\cgi-bin\board\board.cgi aborted due to
compilation errors
</quote>
Your Perl program failed in compilation because of at least two errors
and the Diagnostiv messages that were emitted were attempted to be
interpreted as HTTP.
Perls diagnostic messages are documented in the perldiag manpage
(perldoc).
The first message is self explanatory - find the @ at line 451 and
place a \ in front of it (if of course it isnt meant to be an array -
if it is something is horribly wrong with your code)
The second error message may actually be a compound of several errors
or a simple typo - the error itself almost certainly does not occur at
the line mentioned this is just the point at which Perl has noticed
that something is wrong - however again it is fairly self explanatory
Perl has decided that you have fewer right brackets in your program
than it thinks you need. The fact that this is reported as being at
EOF might point to a degree of overzealous parenthisizing.
/J\.
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Fri, 12 Jun 1998 23:55:42 -0700
From: Chuck Chiang <ctc@cisco.com>
Subject: email alias for beta code user?
Message-Id: <3582226E.596A@cisco.com>
Hi,
I am using beta Perl code that supports multithread for doing my
hardware simulation. I am wondering whether there is a email alias
I can subscribe to so that I get latest info on problems and patches?
I ran into a few problems and wondering there are fixes for those
yet before I pull my hair out trying to debug them.
would be very helpful if the reply could also be cc to ctc@cisco.com.
thanks for the info
--chuck
------------------------------
Date: Sat, 13 Jun 1998 10:19:24 -0500
From: Deva Seetharam <psdspss@execpc.com>
Subject: Re: Help a newbie with arrays! (I think ... )
Message-Id: <6lu4qf$hi9@newsops.execpc.com>
Ryan Baghdad wrote:
> Hi - I work at a law school, and am faced with the unbearable task of
> sorting and analyzing survey results. So I've decided to take a stab at
> Perl and have it do the work for me. Unfortunately, I think my lack of
> understanding with arrays has me at a roadblock.
>
> I have no idea if this is good form, methodology, or what. Please
> critique me with no mercy! (I can take it.) Basically the idea is this:
> each survey is 14 questions with answers that can be represented
> numerically. So I put the answers to all the surveys (numbering over
> 200) into a text file, in this format:
> 1 => $recnum (record number)
> 4
> 2
> ...
> 3
> endofrec
> 2 => $recnum
> 4
> 3
>
> Anyway, the goal of this is that the professor can say to me, "Find me
> all the surveys that answered 'yes' [1] to question 13, 'no' [2] to
> question 4, and answered '3' to question 6."
>
> The way I'm doing it is trying to read *all* the survey answers into
> one big array, and then going through that array again trying to match
> up the specific questions, and incrementing a counter if it finds a
> match. My problem is the method I'm using to store each of these
> variables. There will be like 500+ active variables at once, after I've
> read them all in. So, for instance, this is how I'm reading the records
> now:
>
> sub read_recfile {
> $count = 0;
> print "enter recordbase: ";
> $fname = <STDIN>;
> open(readbuf,"<$fname");
> do {
> $count++;
> read(readbuf,$recnum,3);
> read(readbuf,$position[$count],2); # experimental form
> read(readbuf,$experience,2);
> read(readbuf,$race,2);
> read(readbuf,$sex,2);
> read(readbuf,$complaint,2);
> read(readbuf,$lawsuit,2);
> read(readbuf,$counseling,2);
> read(readbuf,$unfair,2);
> read(readbuf,$upward,2);
> read(readbuf,$promotion,2);
> read(readbuf,$counanother,2);
> read(readbuf,$insuff,2);
> read(readbuf,$marker,6);
> } until eof(readbuf);
> close(readbuf);
> $count--; # Does it make sense why I do this?
> print "record count: " . $count;
> exit;
>
> Anyway, see how I have $position[$count] above? The idea is that
> somehow I can create an array, based on the $count (which increments
> each loop) so that elsewhere in the program, I could reference
> $position[4] and it would give me the fourth person's survey response
> to the $position question. I think this is totally wrong. But I also
> think I'm on the right track.
>
> And then, to search, for, say, everyone you answered 1 to $position, I
> could just do:
>
> for ($x=1, $x=$count, $x++) {
> if ($position[$x] == 1) {
> $people_who_answered_yes++;}
> }
>
> Basically I need the fastest, easiest method of temporarily storing and
> searching through all those variables, which sounds like a case for
> arrays. But after scouring several online documents, I have yet to put
> together how to actually assign the values to the variables, and
> individually identify them. I'm guessing this is something really easy.
> I understand that you can do @survey=($recnum,$position,$race...); but
> how does this help me search through each of the individually?
>
> Anything you could do to help out would be greatly appreciated, and if
> this is totally incoherent, I truly apologize.
>
> As my news server doesn't always catch all responses, please cc: them
> to tpitb@iwaynet.net.
>
> Thanks a lot!
>
> ryan
Data structure you might require is a table or table like structure.
With rows being the responses of individuals, and columns being
the answers to 14 questions
something like
q1 q2
..... q14
person1 1 6
..... 2
person2
person3
U can implement a table, as an array of arrays.
I dont know whether u could change the file structure.
But, if you could, if u can structure the file in a similar fashion, it
could ease your processing.
For each person, create a row, with each row containing the 14 responses.
Choose a field separator(FS) to distinguish between the adjacent questions.
For eg
1:1:2:3:4:5.....:7
(you can use any FS, but I like ":" , colon.)
I have put together a few lines of code for you to look at :
*************************************************
open(INPUT, '....') or
die "Could not open the input file : $!";
while($record = <INPUT>)
{
@survey = split(/:/, $record);
push @survey_list, [@survey];
}
for $i (0..$#survey_list)
{
for $j ( 0..$#{$survey_list[$i]} )
{
print "Element at $i, $j is $survey_list[$i][$j]\n";
}
}
close(INPUT) or die "Could not close the file : $1";
*****************************************
I tested the above code using the following data file.
1:2:3:4:5:6:7:8:9:10:11:12:13:14
1:1:1:1:1:1:1:1:1:1 :1 :1 :1 :1
2:2:2:2:2:2:2:2:2:2 :2 :2 :2 :2
*****************************************
Using this structure, if u want to find out the responses to question 5.
for $i (0..$#survey_list)
{
print "Element at $i, $j is $survey_list[$i][4]\n";
}
Note that for nth question u have to access (n-1)th element.
Even if u cant change the file structure, a 2-D array like this
would help u.
Also refer Camel book, chapter 4, References and Nested Data Structures.
hope that helps.
Deva
------------------------------
Date: 13 Jun 1998 08:06:11 GMT
From: hhaque@aol.com (HHaque)
Subject: how do I save a perl file
Message-Id: <1998061308061100.EAA08909@ladder03.news.aol.com>
I access a unix account by telnet. I need to know how I can write and save a
script file. I am new to unix.
------------------------------
Date: Sat, 13 Jun 1998 14:48:34 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: how do I save a perl file
Message-Id: <35828ea9.2868238@news.btinternet.com>
On 13 Jun 1998 08:06:11 GMT, HHaque wrote :
>I access a unix account by telnet. I need to know how I can write and save a
>script file. I am new to unix.
Eh,oh
You can use any one of the text editors available on various unix
systems : vi,emacs,joe,ted,ev ... etc
Vi is almost certainly the one that is available on most systems.
However I have no intention on trying to explain how to use any editor
here - you would be better asking a local expert.
If you simply want to enter a quick program to test you can do
something like:
cp /dev/tty myprog.pl
<type your program here>
Ctrl-D
However this will not let you edit the resulting file.
You might be able to use an editor on your local system and then FTP
your file to the unix system - again you should ask a local expert
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Sat, 13 Jun 1998 09:59:12 -0500
From: Deva Seetharam <psdspss@execpc.com>
Subject: Re: how do I save a perl file
Message-Id: <6lu3ki$hi9@newsops.execpc.com>
HHaque wrote:
> I access a unix account by telnet. I need to know how I can write and save a
> script file. I am new to unix.
You need a editor to edit the file; say vi, pico or emacs.Write ur code in the
file and save it. (Say in vi it is :w)
Probably, you need to learn a little bit about the editors.
Dont ever forget the man command.
If you find time and energy, read the first chanpter of unix programming
environment by Kernighan and Pike. It would give u a kick start.
hope this helps.
Deva
Deva
------------------------------
Date: Sat, 13 Jun 1998 09:29:45 -0500
From: "Al & Judy Degutis" <al-judy@ix.netcom.com>
Subject: leading zeros
Message-Id: <6lu2ki$9je@sjx-ixn6.ix.netcom.com>
I need to use a 6 digit number which is also used as the counter, but it
must have leading zeros:
000001, 000002, etc.
This number will be printed to a file and used as the record number in two
files so that they can be linked baclk together.
Thanks
Al
------------------------------
Date: 13 Jun 1998 14:51:46 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: leading zeros
Message-Id: <6lu3m2$pm7$1@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, "Al & Judy Degutis" <al-judy@ix.netcom.com> writes:
:I need to use a 6 digit number which is also used as the counter, but it
:must have leading zeros:
:
:000001, 000002, etc.
That's what printf is for. The more you know about printf,
the happier you'll be when doing formatted output. And vice-versa.
--tom
--
------------------------------
Date: Sat, 13 Jun 1998 10:39:43 -0500
From: "Al & Judy Degutis" <al-judy@ix.netcom.com>
Subject: Re: leading zeros
Message-Id: <6lu6no$bsj@sjx-ixn6.ix.netcom.com>
>In comp.lang.perl.misc, "Al & Judy Degutis" <al-judy@ix.netcom.com> writes:
>:I need to use a 6 digit number which is also used as the counter, but it
>:must have leading zeros:
>:
>:000001, 000002, etc.
>
>That's what printf is for. The more you know about printf,
>the happier you'll be when doing formatted output. And vice-versa.
>
>--tom
Being a newbie, I need a good tutorial or reference on printf (and Perl).
Any suggestions?
Thanks
al
------------------------------
Date: Sat, 13 Jun 1998 15:49:52 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: leading zeros
Message-Id: <35829299.3833655@news.btinternet.com>
On Sat, 13 Jun 1998 09:29:45 -0500, Al & Judy Degutis wrote :
>I need to use a 6 digit number which is also used as the counter, but it
>must have leading zeros:
>
>000001, 000002, etc.
>
>This number will be printed to a file and used as the record number in two
>files so that they can be linked baclk together.
There was a post on this subject a short while ago:
I quote from the answer the answer I gave on that occassion:
<quote>
On 3 Jun 1998 17:06:41 GMT, Andy Chandler wrote :
>Hi all, looking for some help...
>
>I want to create a series of files based on incrementing zero-padded
>numbers. For example, if I have the number 0001, I want to automatically
>increment to 0002. I've got the incrementing numbers down (1,2,3,etc),
>but I need to know how to do it ith the zero-padded numbers.
>
To summarize:
#!perl
$theno = 1;
for(1 ... 10)
{
$out = sprintf("%04d",$theno++);
print $out,"\n";
}
That looks alright.
But wait ! Whats this:
#!perl
$theno = "0001";
for(1 ... 10)
{
$out = $theno++;
print $out,"\n";
}
</quote>
With this second approach be warned however that as soon as $theno is
treated as a number in any other way before or after the increment it
will lose its magical properties and print without the padding.
You might review the rest of the thread (via DejaNews) because it was
nine days ago now and a lot has happened since.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: 13 Jun 1998 09:22:08 GMT
From: "John Bokma" <postmaster@castleamber.com>
Subject: Re: NEED PERL SCRIPT WRITTEN
Message-Id: <01bd96ad$119dba20$02521e0a@tschai>
ALAN FEILER <ppdc@ix.netcom.com> wrote in article
<6lrac2$t6b@dfw-ixnews9.ix.netcom.com>...
> not certain on pay but will consider workout.
If you want to become rich you must invest or steal.
John
--
------------------------------------------------------------------
C A S T L E A M B E R Software Development (Java/Perl/C/CGI)
http://www.castleamber.com/ john@castleamber.com
NEW: http://www.binaries.org/ Guide to Program Binaries & Pictures
------------------------------
Date: Sat, 13 Jun 1998 15:49:56 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: novice problem?
Message-Id: <35829bb4.5122470@news.btinternet.com>
On Fri, 12 Jun 1998 16:26:20 -0700, Computing Services wrote :
>I'm trying to read in the contents of a directory then change the file
>names based on what the user inputs (see code below). I put the old and
>new file names into "file_names.txt". This *did* work until I tried to
>have the user input the directory with this code:
>
<snip>
I am not entirely sure about your method but I am sure you have you
reasons.
However a few points:
>
> #open the directory to read
> opendir(DIR,'.') || die "ERROR: cannot open $directory";
> @file_list = readdir(DIR);
@file_list = grep !/^\.{1,2}$/, readdir(DIR);
# will get rid of the "." and ".." files right away
<snip>
>
> foreach $key (keys %list) {
if ($list{$key ){ # dont do anything if there is no new name
> print "Old: $key, New: $list{$key}\n";
> rename($key, $key{$list}) || print "$!";
# Warning, you have your hash and key mixed up that should be:
rename($key,$list{$key}) || print "Cannot rename $key - $!\n";
}
>
> }
>}
>
I think that your error with the hash and key would probably be picked
up by the use of the -w flag with perl.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Sat, 13 Jun 1998 14:48:32 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: perl script emulates "last" command
Message-Id: <35828b3e.1993173@news.btinternet.com>
On 12 Jun 1998 23:52:01 GMT, Mr. Chow Wing Siu wrote :
>
>
>Hi,
>
>I would like to know any perl script can emulate the "last"
>command in UNIX.
>
I dont know of a script that does this but if there is one I am sure a
good place to start would be at www.perl.com.
However I'm sure you would agree with me that it would be more fun to
write your own.
The utmp(5) manpage gives the structure of the /var/adm/utmp (or
whatever) file that holds the information that holds the information.
Unfortunately the functions one might use to achive this in C
(getutent etc) are not available through the POSIX module (or so it
appears) so you would have to do a lot of the work yourself.
>Please mail. Thanks for help.
>
Eh,oh
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: 13 Jun 1998 14:54:00 GMT
From: Tom Christiansen <tchrist@mox.perl.com>
Subject: Re: perl script emulates "last" command
Message-Id: <6lu3q8$pm7$2@csnews.cs.colorado.edu>
[courtesy cc of this posting sent to cited author via email]
In comp.lang.perl.misc, Johnson.Chow@Comp.HKBU.Edu.HK writes:
:
Why must we have a 1,000% line-bloat? Why why why?
:I would like to know any perl script can emulate the "last"
:command in UNIX.
:Please mail. Thanks for help.
#!/usr/bin/perl
# whenon
# tchrist@perl.com
open (LASTLOG, "< /var/log/lastlog") || die "can't open /usr/adm/lastlog: $!";
$lastlog_t = 'L A12 A16';
$lastlog_s = length(pack($lastlog_t, ()));
# struct lastlog {
# time_t ll_time;
# char ll_line[12];
# char ll_host[16]; /* same as in utmp */
# };
if (!@ARGV) {
for ( $uid = 0;
read(LASTLOG, $buff, $lastlog_s) == $lastlog_s;
$uid++
)
{
next unless @p = getpwuid($uid);
($time, $line, $host) = unpack($lastlog_t, $buff);
$uid = $p[2];
$user = $p[0];
$uid{$user} = $uid;
### print STDERR "read $user\n";
$time{$user} = $time || 0;
$line{$user} = $line || '';
$host{$user} = $host || '';
}
for $user ( sort { $time{$a} <=> $time{$b} || $uid{$a} <=> $uid{$b} } keys %time ) {
$uid = $uid{$user};
$time = $time{$user};
$line = $line{$user};
$host = $host{$user};
write;
}
}
USER: foreach $user ( @ARGV ) {
if ($user =~ /^\d+$/) {
unless (@p = getpwuid($user)) { warn "no such uid $user\n"; next; }
$user = $p[0];
$uid = $p[2];
} elsif (!defined ($uid = getpwnam($user))) { warn "no user $user\n"; next; }
seek(LASTLOG, $uid * $lastlog_s, 0) or die "seek failed: $!";
($time, $line, $host) = read(LASTLOG, $buff, $lastlog_s) == $lastlog_s
? unpack($lastlog_t, $buff)
: ();
write;
}
format STDOUT_TOP =
@<<<<<<< @>>>>> @<<<<<<< @<<<<<<<<<<<<<<<<<< @*
'user', 'uid', 'line', 'host', 'when'
-------- ------ -------- ------------------- -----------------------
.
format STDOUT =
@<<<<<<< @>>>>> @<<<<<<< @<<<<<<<<<<<<<<<<<< @*
$user, $uid, $line, $host, $time ? scalar localtime $time : "[ never logged in ]"
.
__END__
If you don't have two formats there, you're in trouble.
--tom
--
Timesharing: the use of several people by the computer
------------------------------
Date: Sat, 13 Jun 1998 14:19:49 GMT
From: Tim.Bunce@ig.co.uk (Tim Bunce) (Tim Bunce)
Subject: Re: Puzzle challenge (clarified)
Message-Id: <EuHuH2.28D@ig.co.uk>
[Here's a (hopefully) clarified restatement of the puzzle]
A list of names in a specific order is given to a set of messengers
in a remote land.
The messengers travel independently to a destination were they
give the names to you.
The problem is that the messengers tend to be forgetful:
They _often_ miss out one or more names (but not all).
They _occasionally_ get the order wrong.
(The only significance is that _most_ of the messages will have the
right ordering of names.)
Names are never repeated or changed, only missed or reordered. You
don't know how long the original list of names was and it's possible
that all the correctly ordered messages are missing one or more names.
(The messengers always think they've got it right.)
For example:
Original list: foo bar baz boo
Messenger A says: foo bar boo # bar missing
Messenger B says: bar boo baz # reordered
Messenger C says: foo bar baz boo # complete
Messenger D says: boo foo bar baz # reordered
Messenger E says: bar # foo baz boo missing
Messenger F says: foo baz boo # bar missing
Messenger G says: foo bar baz # boo missing
Messenger H says: baz boo # foo bar missing
Messenger I says: bar boo # foo baz missing
The problem is to try find the full list of names and the original order
as far as is possible.
Suggestion (feel free to take another approach):
1. Separate the messages into groups which have the same name order.
Messages which are subsets of others with different orders
can be assigned to more than one group. (e.g. All but B & D in
one group, B & E in another, and D & E in another.)
2. Discard all but the largest group (which has the highest
probability of matching the original message.)
3. Merge the ordered lists to reconstitute the original list.
(As far as possible with the available information. Note that
it's not possible to be certain that the answer is complete.)
A solution implemented in perl would be ideal. Obviously steps 1 and 3
above are the interesting aspects of a perl implementation.
(I'd be interested in seeing implementations of either in isolation.)
Sorry, no prizes. Just the fun of the challenge and a thank you
(and maybe an explanation of the application domain).
Tim.
------------------------------
Date: Sat, 13 Jun 1998 14:38:16 GMT
From: Tim.Bunce@ig.co.uk (Tim Bunce) (Tim Bunce)
Subject: Re: Puzzle challenge (clarified)
Message-Id: <EuHvBs.2Fz@ig.co.uk>
In article <EuHuH2.28D@ig.co.uk>, Tim Bunce <timbo@ig.co.uk> wrote:
> [Here's a (hopefully) clarified restatement of the puzzle]
Two other points:
1. The number of messengers is likely to be between 3 and 10.
2. The actual problem being solved has nothing to do with messengers.
I just picked that as a simple analogy for the actual problem.
The actual problem has to do with information retreival. A 'high-level'
'strategic' set of ordered 'goals' has been passed down to be
implemented at a 'low-level' by a set of interconnected machines.
To compare the configuration of the machines with the original goals
the goals 'in use' need to be reconstituted from the low-level
information available from the machines. The nature of the low-level
information is that it misses out parts that are not relevent to that
machine. Also sometimes the machine has been reprogrammed by hand and
the low-level information has little resemblance to the orignal goals.
Tim.
------------------------------
Date: Sat, 13 Jun 1998 13:36:06 +0100 (BST)
From: camel@no.spam.leeds.ac.uk
Subject: Q: How best to implement plug-ins for a Perl script?
Message-Id: <3582E383.5E8F@no.spam>
A little while ago, it may have been two years or possibly
three, there was some discussion here about whether Perl
could be used as a vehicle for teaching Software
Engineering. IIRC, a body of opinion held that in
comparision with recognised teaching languages such as
Pascal or with the new kid on the block, Perl would be a
strange choice - its strengths being as a practical langauge
for getting a job done.
My question is a little about Software Engineering and so
may not have been fully discussed by many people.
One of the current suggestions for Macintosh software is
that part, and preferably most, of its functionality should
be implemented by plug-ins. This can be seen, par excellence,
in the latest version of Pagemaker and in many other
applications such as WebStar.
I have in mind a plug-in directory for Perl, which is scanned
at a suitable time and properly formatted files within it
are loaded and initialised (plugged in for want of a better
term).
Are there good ways of implementing such technology using
Perl, and if so what is the best way or what is the
currently reccommended way?
I have thought of two possibilites.
Each plug-in is contained in a file which contains a single
Perl program (a main() if you like). At start-up these files
are read in, and their contents assigned to suitably named
subroutines; videlicet:
(untested code)
#!perl -w
# The clever stuff in this code can be found in various
# places in the Perl FAQ.
use strict ;
my $dirPlugin = ":Plug-Ins:" ;
use lib $dirPlugin ; # doesn't work
use lib ":Plug-Ins:" ; # mac
my @plugins;
if( opendir( D, $dirPlugin ) ) {
my $fnPlugin;
while( defined ( $fnPlugin = readdir( D ) ) ) {
if ( $fnPlugin =~ /^([_a-zA-Z0-9]+)\.pl$/ ) {
if ( open( CODE, "<$dirPlugin$fnPlugin") ) {
$_ = select; select CODE; undef $/ ;
my $code = <CODE>;
select $_ ;
my $subRef = eval "sub { $code }";
if( $@ ) {
# error in plugin code ???
} else {
push @plugins, $subRef;
}
} else {
# failled to open $fnDat
}
} else {
# bad file name
}
}
} else {
# failled to open $dirPlugin
}
foreach( @plugins ) {
# print "[ $_ ]\n" ;
&$_( 9, -P1=>"Value1", -P2=>"Value2" );
} ;
# -----------------------------------------
An example plug in file is this:
my $cmd = shift;
my %params ;
while( @_ ) {
local( $_ ) = shift; # This technique for "named parameters"
last unless /-(.*)/ ; # cruelly and quite outrageous nicked
$params{ "\U$1" } = shift ; # from Stein's CGI module.
}
print "Try1: Called with $cmd\n" ;
foreach( sort keys %params ) {
print "\t$_\t => $params{ $_ }\n";
} ;
#------------------------------------------------
Another way would be to make each plugin a module, and pull them
in with a require statement. [Obiter dicta, the repeated assertion
that a use statement can be replaced by a slightly longer, equivalent,
require statement cannot be confirmed by me: I find that I have to
change the package declaration in the module as well].
(untested code)
#!perl -w
use strict ;
my $dirPlugin = ":Plug-Ins:" ;
use lib $dirPlugin ; # doesn't work
use lib ":Plug-Ins:" ; # mac
my @plugins;
if( opendir( D, $dirPlugin ) ) {
my $fnPlugin;
while( defined ( $fnPlugin = readdir( D ) ) ) {
if ( $fnPlugin =~ /^([_a-zA-Z0-9]+)\.pm$/ ) {
push @plugins, $1 ;
require $fnPlugin ;
} else {
# bad file name
}
}
} else {
# failled to open $dirPlugin
}
foreach( @plugins ) {
$_->PI_Run( -P1=>"Value1", -P2=>"Value2" );
} ;
# -----------------------------------------
An example plug in file is this:
#!Perl -w
=head1 NAME
SimplePlugin - A simple plugin
=head1 SYNOPSIS
=cut
package SimplePlugin ;
require Exporter;
@ISA = qw( Exporter );
@EXPORT = qw( );
@EXPORT_OK = qw( );
use strict;
BEGIN {
print "SimplePlugin says 'Hello World!'\n" ;
} ;
sub PI_Run () {
my %params ;
my $self = shift;
while( @_ ) {
local( $_ ) = shift; # This technique for "named parameters"
last unless /-(.*)/ ; # cruelly and quite outrageous nicked
$params{ "\U$1" } = shift ; # from Stein's CGI module.
}
print "SimplePlugin: Method Run() called with \n" ;
foreach( sort keys %params ) {
print "\t$_\t => $params{ $_ }\n";
} ;
}
END {
print "SimplePlugin says 'Goodbye World!'\n" ;
} ;
1;
__END__
#------------------------------------------------
The code to do this seems shorter, more easily understood, provides
BEGIN and END methods, and does not need to be passed a cmd code
as it can provide named methods for each item of the API. If
code in one of its methods is in error, them Perl is aware of the
filename and line number.
Does anyone have any better ideas? Are there gotchas or pitfalls that
I have missed? I appreciate that there may be little precedent for
implementing this sort of thing in Perl, and therefore the guardians
of Perl Power have probably not given much thought to it.
There was a post about Hash of Subroutines in the autumn of last year.
1K apologies if my posting an answer to a question that nobody has
asked is considered to be off-charter or otherwise wasteful of
electrons.
------------------------------
Date: 13 Jun 1998 11:08:21 GMT
From: gossamer@tertius.net.au (Gossamer)
Subject: refs and nested hashes
Message-Id: <slrn6o4nd5.kf2.gossamer@penguin.glasswings.com.au>
I'm 99% sure the answer to this is going to be one of those "kickself"
things, but I've been figuring all night and I can't figure it. So,
in desperation:
(Sorry for the length, I'm being as short as I can).
I have an object, $foo, and one of the variables I need the object to
hold is reference, $bar, to a nested hash, %baz. %baz is tied to a
database.
I know bits about problems with tied hashes and nesting, and reading
of the docs seems to indicate that MLDBM using GDBM_File and Storable
works, so I'm using that. The require line, for reference, being "use
MLDBM qw(GDBM_File Storable);"
In the constructor, I have (amongst other things):
my %baz;
tie(%baz, 'MLDBM', $filename, O_RDWR | O_CREAT, 0644) ||
die "Can't open \"$filename\": $!\n";
$self->{"bar"} = \%baz;
In the methods for the object, I've tried two ways of assigning values
to the hash:
${$self->{"bar"}}{$fred}{$barney} = $quux;
and:
my %baz = %{$self->{"bar"}};
$baz{$fred}{$barney} = $quux;
Neither of them give errors (yes, I have -w and use strict :)) but
neither of them work. The former sets ${$self->{"bar"}}{$fred} to {}
(it didn't exist at all before). The latter sets %baz correctly but
doesn't alter $self->{"bar"}.
Is this:
(a) Me misunderstanding references in a bad way?
(b) A limitation of MLDBM?
(c) None of the above?
And is it fixable? I -could- redesign away the need for the nested
hash to be tied, but it would involve ugly join()'d keys and I don't
want to do that because I need to be able to search for specific keys.
Gossamer
PS
Aren't metasyntactic variables fun :)
PPS
Yes, I'm goss from #perl.
--
: Gossamer - gossamer@tertius.net.au - http://www.tertius.net.au/~gossamer/
: We worked very hard on creating a name that would appeal to the
: majority of people, and it certainly paid off: thousands of people are
: using Linux just to be able to say 'OS/2? Hah. I've got Linux. What a
: cool name'. 386BSD made the mistake of putting a lot of numbers and
: weird abbreviations into the name, and is scaring away a lot of people
: just because it sounds too technical. -- Linus Torvalds
------------------------------
Date: 13 Jun 1998 15:24:11 GMT
From: andrew@pimlott.student.harvard.edu (Andrew Pimlott)
Subject: Return values of comparison operators
Message-Id: <slrn6o56mb.b9c.andrew@pimlott.student.harvard.edu>
I just read that in Icon you can do something like
if (0 < x < 10)
because < returns it's right operand. I thought Perl was pretty clever to
do something like this with || and &&, so why don't we have it for
comparison operators?
(Don't worry--I'm not suggesting changing existing behavior. Too late for
that. Drat.)
Andrew
[Email cc's of replies appreciated.]
--
"Do they give a Nobel Prize for attempted chemistry?"
- "Sideshow" Bob Terwilliger
------------------------------
Date: Sat, 13 Jun 1998 15:49:57 GMT
From: Gellyfish@btinternet.com (Jonathan Stowe)
Subject: Re: REVIEW: Perl CGI Programming - No Experience Required
Message-Id: <35829de6.5684792@news.btinternet.com>
On 13 Jun 1998 01:38:16 GMT, Tom Christiansen wrote :
>
> The sins begin to compound.
> o He doesn't explain the real difference between GET and POST
> (idempotency, caching, read vs write).
> o He teaches people to decode the form by hand. Wicked. Bad.
> Naughty. This makes him teach tr and s///g and many inappropriate
> things. Then we get lost in regular expressions. BAD!
>
> 6. Perl and the Complex Web Page
>
> o More confusion about GET vs POST.
> o He decodes the post by hand, and wrongly!. Failing grade for the
> whole book here. Why must people keep recreating the wheel, and
> wrongly! This is just plain bad programming.
>
So when do the posts that start:
I am not a programmer but I have been trying to do such and such CGI
based on the example used in "Perl CGI Programming - No Experience
Required" and it doesnt work ...
Begin ;-}
Given the stated enterprise of this book and other similar ones I am
surprised that they dont ever advocate the use of the available CGI
libraries. But there you go.
/J\
Jonathan Stowe
Some of your questions answered:
<URL:http://www.btinternet.com/~gellyfish/resources/wwwfaq.htm>
------------------------------
Date: Fri, 05 Jun 1998 16:03:39 -0700
From: Jim Lekas <jbl@tfs.com>
Subject: Re: Split string into newline-terminated lines
Message-Id: <3578794B.EF32E5B9@tfs.com>
@lines = split /^/m, $file;
--
James Lekas
Software Engineer
TRW Data Services
------------------------------
Date: 13 Jun 1998 09:44:05 -0500
From: Jonathan Feinberg <jdf@pobox.com>
To: fascist@posix.org
Subject: Re: splitting config file
Message-Id: <ra0tpdje.fsf@mailhost.panix.com>
fascist@posix.org writes:
> ok, I'd like to be able to parse the file in a specific way
> that I could perform a specific set of operations on each
> <blah>...</blah> chunk independently.
This might give you an idea or two. You might also consider using one
of the existing modules for parsing SGML.
#!//c/perl/bin/perl -w
use strict;
use Data::Dumper;
my @blahs = ();
my %current_blah = ();;
while (<DATA>) {
chomp;
next if /^\s*$/;
%current_blah = (), next if m!<blah>!i;
push(@blahs, {%current_blah}), next if m!</blah>!i;
/^([^:]+):\s*(.+)$/; #($1, $2) = split /:\s+*/
$current_blah{$1} = $2;
}
print Dumper(\@blahs);
__DATA__
<blah>
bleh: 1
frob: biscuit
</blah>
<blah>
bleh: 2
frob: cornpone
</blah>
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf/
------------------------------
Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 8 Mar 97)
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.
To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.
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.
The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.
The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.
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 V8 Issue 2864
**************************************