[22750] in Perl-Users-Digest
Perl-Users Digest, Issue: 4971 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun May 11 06:05:54 2003
Date: Sun, 11 May 2003 03:05:13 -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 Sun, 11 May 2003 Volume: 10 Number: 4971
Today's topics:
Re: alarm and readline (A Perl 5.8.0 bug or a feature?) <spambox@centrum.cz>
ANNOUNCEMENT: POE 0.26 Released! <troc@netrus.net>
Characters in Variables Crashing Program <sammie@greatergreen.com>
File pointers, EOF, +< <urael@zrgebcbyvf.arg.nh>
Re: File pointers, EOF, +< (Sam Holden)
Re: File pointers, EOF, +< <urael@zrgebcbyvf.arg.nh>
Help with Reg Expr. Thank you (Sara)
Re: Help with Reg Expr. Thank you (Sam Holden)
Re: Help with Reg Expr. Thank you <tassilo.parseval@rwth-aachen.de>
Re: Help with Reg Expr. Thank you <krahnj@acm.org>
Re: How can I pack an IP address into four binary bytes <spam@thecouch.homeip.net>
Re: How to find the size of a directory??? (Vinod. K)
Re: Month number being returned is incorrect <bigj@kamelfreund.de>
Re: pattern match A or B with Y or Z <bigj@kamelfreund.de>
Re: Powerful One-liner <abuse@mweb.co.za>
Re: Powerful One-liner <abuse@mweb.co.za>
Re: Powerful One-liner <krahnj@acm.org>
prob getting SNMP module to recognize my OID (Adam)
Re: RegExp Question <bigj@kamelfreund.de>
Re: Splitting, Sorting, then Rebuilding an Array <sammie@greatergreen.com>
Re: Splitting, Sorting, then Rebuilding an Array <bigj@kamelfreund.de>
stat and underscore in file name (big__smile)
Re: stat and underscore in file name <krahnj@acm.org>
Why Perl is so difficult to learn? <ddl76@singnet.com.sg>
Re: Why Perl is so difficult to learn? <spam@thecouch.homeip.net>
Re: Why Perl is so difficult to learn? <ddl76@singnet.com.sg>
Re: Why Perl is so difficult to learn? <tassilo.parseval@rwth-aachen.de>
Re: Why Perl is so difficult to learn? <krahnj@acm.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 11 May 2003 09:46:29 GMT
From: Jiri <spambox@centrum.cz>
Subject: Re: alarm and readline (A Perl 5.8.0 bug or a feature?)
Message-Id: <b9l65l$khfgv$1@ID-134981.news.dfncis.de>
In article <b9jktc$t3r$1@agate.berkeley.edu>, Ilya Zakharevich wrote:
>> gets called every second, even though Perl is waiting for an input line.
>> For some reason, the alarm function is not executed in Perl 5.8.0 (at
>> least on Slackware Linux 8.1--9.0).
>
> How do you know? In your code there is no check for whether it was
> called. At least set $|.
>
> The difference may be that PerlIO does not try to emulate
> line-buffering on TTYs... (Does it?)
That's a good point. Thanks. Pity I'm back to Perl 5.6.1, I'd like to
test it. I didn't know about the $| variable, I'm a relative Perl
newbie. I thought that all output in Perl was unbuffered. I should
really find some time to do some proper Perl reading, rather than just
hack at it from time to time.
However, I when I run this code on Perl 5.8.0 and waited for more than
one second, the output from the tick subroutine was only one message.
If what you say is true, I'd expect n messages, where n is the number of
seconds I waited before pressing enter.
Never mind, I'll stick with Perl 5.6.1 for the time being.
Many thanks.
--
Jiri
------------------------------
Date: Sun, 11 May 2003 02:43:11 GMT
From: Rocco Caputo <troc@netrus.net>
Subject: ANNOUNCEMENT: POE 0.26 Released!
Message-Id: <a0207b71035ff526c47f229a86a249df@TeraNews>
Keywords: multitasking, networking, framework, components, robust, cool
--------
Rejoice!
--------
Version 0.26 of the POE networking and multitasking framework has been
released. Thanks go out to everyone who assisted in its development.
This release is improved in several ways.
- Two filehandle leaks have been fixed in POE::Wheel::SocketFactory.
Thanks to Andrew Chen and Schuyler Erle for finding them.
- Perl 5.8.0 is supported now.
- Several fatal "internal inconsistency" errors were solved. Thanks
to Nick Williams and Jonathan Steinert for finding and patching
them.
- Performance has been significantly increased for Linux users. It
was estimated that POE on Linux became up to 4000% slower because
of a workaround for older, broken Linux kernels. That workaround
was removed for this release.
- POE::Wheel::ReadWrite finally has the socket shutdown methods that
Peter Chen suggested.
- Alan Hastings' patch for better Cygwin support has been applied.
- Dmitri Tikhonov's ProgramArgs parameter has been added to
POE::Wheel::Run. This lets subroutines be run in forked processes
without using closures to pass values into them.
- The included copy of ExtUtils::AutoInstall has been updated.
Automatic installation should be easier and work on more platforms.
- POE::Wheel::FollowTail's SeekBack behavior has been reverted to
that of version 0.23. Thanks go to Jim Westfall for pointing out
the difference.
- POE::NFA's stop() method works again.
- The tests are less strict about timeouts, which means they work
again on older (486 class) hardware.
As if that wasn't enough, POE's web site contains detailed change logs
for every public release ever.
- http://poe.perl.org/?POE_CHANGES
Even now the latest tarball should be hurtling towards your favorite
CPAN mirror. It is also on the web, and so is a Windows PPD. Users
who need advanced notice of changes can follow it via anonymous CVS or
POE's mailing list.
- http://poe.perl.org/?Where_to_Get_POE
- http://poe.perl.org/?POE_Support_Resources
Thanks again to everyone who helped with this release. Keep the
feedback and patches coming.
---------
About POE
---------
POE is a networking and multitasking framework for Perl. It has been in
active development since 1996, with its first open release in 1998. The
O'Reilly Perl Conference (now part of OSCON) named POE "Best New Module"
in 1999.
- http://poe.perl.org/?What_POE_Is
POE's users and developers continue to improve and build upon it. For
instance, see the CPAN for the most up-to-date list of POE based
modules.
- http://search.cpan.org/search?mode=module&query=POE::Component
POE's robustness and performance have made it an integral part of
mission critical applications since its first release. It is used in
several fields and in projects ranging from just a few lines of code to
tens of thousands.
- Financial:
Market servers, clients, billing systems, and automated trading
agents.
- Web:
Commerce servers, content management systems, application servers,
data warehouses, WAP proxies, ad exchanges, web crawlers/spiders,
and a variety of specialized agents.
- System Administration:
Large-scale host monitors and maintenance agents, distributed load
testers, a distributed file system (InterMezzo), radius monitors,
system log managers and reports, and spam detectors.
- Entertainment:
Interactive TV servers; mp3 jukeboxes and streaming servers; game
server monitors, managers, and tournament controllers; and a
plethora of IRC applications, services, and agents (bots).
- Software Development:
Compile farm managers, build managers, distributed testing
frameworks.
- Monitoring and Automation:
X10 home control systems, weather station monitors, alarm monitors.
We look forward to hearing how POE has helped you.
-- Rocco Caputo - troc@pobox.com - http://poe.perl.org/
------------------------------
Date: Sun, 11 May 2003 04:26:34 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Characters in Variables Crashing Program
Message-Id: <_hkva.807483$F1.101592@sccrnsc04>
Line in code of crash (reported by console):
push @DB, $oldsession if $oldsession =~ /$discpl/;
$Variable that crashes program:
*Reload*
Crash error:
Quantifier follows nothing in regex; marked by <-- HERE in m/* <-- HERE
Reload*/ at server27.pl line 185, <FH> line 12.
Is there a way to prevent this from happening (Like a special declaration on
variables)?
Thanks for any help,
Brad
------------------------------
Date: Sun, 11 May 2003 13:27:29 +0930
From: Henry <urael@zrgebcbyvf.arg.nh>
Subject: File pointers, EOF, +<
Message-Id: <urael-EFBB32.13272911052003@nswpull.telstra.net>
In article <slrnbbr41q.2vd.abigail@alexandra.abigail.nl>,
Abigail <abigail@abigail.nl> wrote:
> || Are writes _inserted_ at the current pointer position, or do they
> || _overwrite_ the contents (starting) at the current pointer position?
>
> They are overwritten. Files are just a stream of bytes in the file
> system, and disks are rigid structures. You can't easily insert something
> in a file.
In which case "+<" will place the pointer at the start of the file and,
providing you don't read anything, overwrite from that point onwards.
Got it.
If that's the case, what happens when you close the file? Where does
the EOF marker go? Does it stay where it is, or does it get placed at
the current pointer position?
Scenario 1: EOF stays where it is. If "+<" overwrites data at the
current pointer position and "close" does _not_ modify the position of
the EOF, then what happens if you overwrite the EOF? (By, for example,
writing 1025 bytes into a file which previously held 1024 bytes.)
Scenario 2: EOF placed at the current pointer position. What happens
with the "+<" approach if the file spans multiple blocks, and writing
terminates (for example) in the penultimate block? (Now you have an EOF
in block n-1 as well as block n.)
Basically, I'm wondering what logic deals with the EOF when the "+<"
approach is used.
Henry.
------------------------------
Date: 11 May 2003 06:55:44 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: File pointers, EOF, +<
Message-Id: <slrnbbrsvg.crr.sholden@flexal.cs.usyd.edu.au>
On Sun, 11 May 2003 13:27:29 +0930, Henry <urael@zrgebcbyvf.arg.nh> wrote:
> In article <slrnbbr41q.2vd.abigail@alexandra.abigail.nl>,
> Abigail <abigail@abigail.nl> wrote:
>
>> || Are writes _inserted_ at the current pointer position, or do they
>> || _overwrite_ the contents (starting) at the current pointer position?
>>
>> They are overwritten. Files are just a stream of bytes in the file
>> system, and disks are rigid structures. You can't easily insert something
>> in a file.
>
> In which case "+<" will place the pointer at the start of the file and,
> providing you don't read anything, overwrite from that point onwards.
> Got it.
>
> If that's the case, what happens when you close the file? Where does
> the EOF marker go? Does it stay where it is, or does it get placed at
> the current pointer position?
It stays where it is.
See perldoc -f truncate if you want it not to.
--
Sam Holden
------------------------------
Date: Sun, 11 May 2003 18:08:16 +0930
From: Henry <urael@zrgebcbyvf.arg.nh>
Subject: Re: File pointers, EOF, +<
Message-Id: <urael-1931D5.18081611052003@nswpull.telstra.net>
In article <slrnbbrsvg.crr.sholden@flexal.cs.usyd.edu.au>,
sholden@flexal.cs.usyd.edu.au (Sam Holden) wrote:
> >> || Are writes _inserted_ at the current pointer position, or do they
> >> || _overwrite_ the contents (starting) at the current pointer position?
> >>
> >> They are overwritten. Files are just a stream of bytes in the file
> >> system, and disks are rigid structures. You can't easily insert something
> >> in a file.
> >
> > In which case "+<" will place the pointer at the start of the file and,
> > providing you don't read anything, overwrite from that point onwards.
> > Got it.
> >
> > If that's the case, what happens when you close the file? Where does
> > the EOF marker go? Does it stay where it is, or does it get placed at
> > the current pointer position?
>
> It stays where it is.
Ok, so if "+<" overwrites data at the current pointer position and
"close" does _not_ modify the position of the EOF, then what happens if
you _overwrite_ the EOF?
Scenario 1: Write 1000 bytes into a file which previously held 999
bytes.
Assuming blocks of 512 bytes, the filesystem has still allocated only 2
blocks to the file. The next time you try to read the file, would you
get the first, predictable 1000 bytes, then an unpredictable 24 bytes
(the rest of the block)?
Does Perl keep reading until it encounters the first EOF? If so, and if
no EOF is encountered in the trailing 24 bytes, what happens next? Does
Perl generate an "EOF Not Found"-type error? The file-system presumably
knows that the file being read doesn't span more than two blocks and
thus will generate its own error and pass that to Perl (which will
presumably pass it to the script being run).
Scenario 2: Write 128 KB into a file which previously only held 999
bytes.
Will more blocks be allocated by the filesystem if a "+<" write exceeds
the original number of blocks assigned to the original file?
Henry.
------------------------------
Date: 11 May 2003 00:24:24 -0700
From: demone33@yahoo.it (Sara)
Subject: Help with Reg Expr. Thank you
Message-Id: <799fe45f.0305102324.83503b5@posting.google.com>
Hi all,
I'm a novice with Reg Exprs: please help with the following:
I want all the lines not starting with /home to be changed to /go-to-error-page
using sed:
sed -e 's/<reg expr?>/<...>/g'
Thank you very much.
Sara
------------------------------
Date: 11 May 2003 07:30:22 GMT
From: sholden@flexal.cs.usyd.edu.au (Sam Holden)
Subject: Re: Help with Reg Expr. Thank you
Message-Id: <slrnbbrv0e.d7p.sholden@flexal.cs.usyd.edu.au>
On 11 May 2003 00:24:24 -0700, Sara <demone33@yahoo.it> wrote:
> Hi all,
>
> I'm a novice with Reg Exprs: please help with the following:
>
> I want all the lines not starting with /home to be changed to /go-to-error-page
> using sed:
>
> sed -e 's/<reg expr?>/<...>/g'
>
> Thank you very much.
Maybe you should try asking somewhere where sed is talked about. Rather than
some other random place where a different language/program is discussed.
But since you asked here:
perl -lpe '$_="/go-to-error-page" if index $_, "/home"'
--
Sam Holden
------------------------------
Date: 11 May 2003 08:12:20 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Help with Reg Expr. Thank you
Message-Id: <b9l0l4$8po$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Sam Holden:
> On 11 May 2003 00:24:24 -0700, Sara <demone33@yahoo.it> wrote:
>> Hi all,
>>
>> I'm a novice with Reg Exprs: please help with the following:
>>
>> I want all the lines not starting with /home to be changed to /go-to-error-page
>> using sed:
>>
>> sed -e 's/<reg expr?>/<...>/g'
>>
>> Thank you very much.
>
> Maybe you should try asking somewhere where sed is talked about. Rather than
> some other random place where a different language/program is discussed.
>
> But since you asked here:
>
> perl -lpe '$_="/go-to-error-page" if index $_, "/home"'
I had to look twice to get it. It makes use of the fact that index()
returns 0 if the substr is at the beginning of the string. This would be
a little more explicit:
perl -lpe '$_="/go-to-error-page" if index($_, "/home") != 0'
Granted, readability is not the main criterion for one-liners.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Sun, 11 May 2003 08:57:07 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Help with Reg Expr. Thank you
Message-Id: <3EBE1039.CB70A5AB@acm.org>
Sara wrote:
>
> I'm a novice with Reg Exprs: please help with the following:
>
> I want all the lines not starting with /home to be changed to /go-to-error-page
> using sed:
>
> sed -e 's/<reg expr?>/<...>/g'
You probably want the comp.unix.shell newsgroup for sed questions
however in perl you can do:
perl -lpe's%^(?!/home).*%/go-to-error-page%'
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sun, 11 May 2003 00:58:55 -0400
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: How can I pack an IP address into four binary bytes ?
Message-Id: <gMkva.21696$Bm2.228581@wagner.videotron.net>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
You can use the inet_aton function provided in the Socket library:
use Socket;
$np = inet_aton("234.243.9.3");
Although perldoc Socket says not to assume that the results will always
be 32-bits (four bytes) long.
Another alternative is to pack it yourself. There are a couple of ways
to do it.
$ip = '234.243.9.3';
$packed = join("", map {chr($_)} split(/\./, $ip));
#or
$packed = pack("C4", split(/\./, $ip));
The first example with inet_aton will not return anything if the IP is
not valid, the join and pack examples might return skewed results with
bad IPs as input since I haven't encorporated error checking into them.
Best of luck.
Anthony wrote:
> For example if my IP address is
>
> 234.243.09.3
>
> how do I verify the number < 255 and then write it to a file as a four
> binary bytes ?
>
> Thanks,
> Anthony
>
>
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQE+vdiTeS99pGMif6wRAt8GAKCMR1obsWfvwQRFqmrtKR/2FVSWqgCgqPxE
wNNGsRDKZ1ZbDeC3W0JnuVQ=
=/pm6
-----END PGP SIGNATURE-----
------------------------------
Date: 10 May 2003 22:06:25 -0700
From: pkvinu@indiatimes.com (Vinod. K)
Subject: Re: How to find the size of a directory???
Message-Id: <bde4ceed.0305102106.71a5929b@posting.google.com>
"Jürgen Exner" <jurgenex@hotmail.com> wrote in message news:<wCHua.1313$0K4.876@nwrddc03.gnilink.net>...
> Vinod. K wrote:
>
> > I am stuck up with a problem. As i said above in the subject, I want
> > to know is there any module/operators to find out the size of a
> > directory????
>
> Not quite clear what you really want.
>
> Taking your question literally ("size of the directory") then just
> print -s 'directoryname';
> Details see 'perldoc -f -X'.
> However typically this will be just a few KB because the filenames and inode
> number don't take up much space.
>
> Or do you mean the size of all files in that directory?
> Then have a look at opendir() and readdir() to get the names of all files in
> the directory and then just sum up their sizes using -s.
>
> Or do you mean the size of all files in this directory and recursively all
> sub directories? Then have a look at File::Find. In the wanted() function
> just add the size of each file (from -s) in a global variable.
>
> jue
Hi,
Sorry, the details were not given. Yes, I meant this only. I wanted to
have the size of a directory which includes files/any subdirectories,
if any, recursively.
Is there any specific command/module related to finding out the size
of a directory(like how we will get in unix environment using 'du -sk
<dir>' command).
Looks to be no such command/module. I need go by as u told above using
File:Find only ,right????
- Vinod.
------------------------------
Date: Sun, 11 May 2003 08:39:46 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: Month number being returned is incorrect
Message-Id: <pan.2003.05.10.07.24.15.476670@kamelfreund.de>
Peter Richards wrote at Sat, 10 May 2003 13:30:46 +1000:
> my (undef, undef, undef, $day, $month, $year, @rest) =
> localtime(time);
> $outfile = sprintf "%s/%4d-%2.2d-%2.2d-", $base_dir, $year + 1900,
> $month, $day;
>
> and I read somewhere that the month value returned by localtime is 0
> to 11, and that is what the problem is, a (flat) file created in May
> will have month value of 4.
>
> How do I increment the variable $month by 1 ? Is it just:
>
> $month++;
I prefer not to think about the whole problem,
instead I write it simple as:
use POSIX qw/strftime/;
$outfile = "$base_dir/" . strftime("%Y-%m-%d-", localtime);
Greetings,
Janek
------------------------------
Date: Sun, 11 May 2003 08:39:47 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: pattern match A or B with Y or Z
Message-Id: <pan.2003.05.10.07.38.33.546179@kamelfreund.de>
stinkbomb wrote at Fri, 09 May 2003 11:04:39 -0400:
> how would you find pattern A or B and replace it with Y or Z?
> If we find A, we replace it with Y.
> If we find B, we replace it with Z.
>
> I *think* I've seen this before but don't remember.
Well, the standard way seems to create a hash and substituting
the keys with it's values:
my %substitute = (A => 'Y',
B => 'Z');
my $keys = join "|", keys %substitute;
s/($keys)/$substitute{$1}/g;
Disadvantages are that it becomes slow,
when there a lot of different expressions,
and it can lead to problems if there are some regexp characters inside.
It's often extremely useful,
if you know that the matches can be matched with a more general one,
e.g. they are words:
s/(\w+)/$substitute{$1} || ""/ge;
Erm, however, as I feel that it is best not to think about that little
problem, I really favor using a module:
use Regexp::Subst::Parallel;
my $replaces_str = subst($string,
qr/A/ => 'Y',
qr/B/ => 'Z'
);
Best Wishes,
Janek
------------------------------
Date: Sun, 11 May 2003 06:37:03 +0200
From: "Nico Coetzee" <abuse@mweb.co.za>
Subject: Re: Powerful One-liner
Message-Id: <pan.2003.05.11.04.37.01.356279@mweb.co.za>
As I said - all was tested on my Linux system, and the output verifies
with known correct values.
On Sat, 10 May 2003 21:48:56 +0000, John W. Krahn wrote:
> Nico Coetzee wrote:
>>
>> Here is a couple of solutions for nr. 2 of your question:
>>
>> a) Who is defined as normal users ( uid > 500 ) on the system (unix).
>> Parse the output of the cat command:
>>
>> cat /etc/passwd | perl -e 'while(<>){ /^(\w+):x:(\d+):/; if ( $2 > 500 ) { print "$1\n"; }}'
>
> UUOC. In fact you can use Perl's built-in getpw* functions.
This is just 'another' way :)
>
> perl -le'$a[2] > 500 and print $a[2] while @a = getpwent'
>
>
>> b) Print the environment variables:
>>
>> perl -e 'foreach $key ( keys %ENV ) { print "$key=$ENV{$key}\n"; }'
>
> man env
>
Again - yet another way :) I tried to illustrate how you can use Perl -
not only the command line utilities. As I mentioned, many of the examples
can also be done in gawk(1).
>
>> c) Print number of empty lines in a text file:
>>
>> cat program.pl | perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";'
>
> UUOC. It also doesn't count empty lines correctly. You do realise that
> the expression (<>!=~/^\W$/) evaluates to ( <> != ~ /^\W$/ )? You do
> realise that (<>)&&(<> reads two lines every time through the loop?
>
My function works 100% on Linux. I have test now again several times with
various files - the output is accurate and consistant.
> perl -lne'$c += /^\s*$/}{print $c' program.pl
Gives LESS lines then there actually is :(
>
>
>> d) How many lines in a text file contains the TAB character:
>>
>> cat parser.pl | perl -e '$c=0;while(<>){ if ( /\t/ ) {$c++;}}print "$c\n";'
>
> UUOC.
>
> perl -lne'$c += /\t/}{print $c' parser.pl
>
This is cool :)
>
>
> John
Cheers
--
Nico Coetzee
http://www.itfirms.co.za/
http://za.pm.org/
http://forums.databasejournal.com/
To the systems programmer, users and applications serve only to provide a
test load.
------------------------------
Date: Sun, 11 May 2003 07:50:11 +0200
From: "Nico Coetzee" <abuse@mweb.co.za>
Subject: Re: Powerful One-liner
Message-Id: <pan.2003.05.11.05.50.10.591685@mweb.co.za>
BTW: I tested now on a RedHat system, and there it works as you said, so I
may have another more serious problem on my Mandrake system.
Sorry about that - and thanks. My MDK system seems to be broken and I
would not have found it without this thread.
Cheers
On Sun, 11 May 2003 06:37:03 +0200, Nico Coetzee wrote:
> As I said - all was tested on my Linux system, and the output verifies
> with known correct values.
>
> On Sat, 10 May 2003 21:48:56 +0000, John W. Krahn wrote:
>
>> Nico Coetzee wrote:
>>>
>>> Here is a couple of solutions for nr. 2 of your question:
>>>
>>> a) Who is defined as normal users ( uid > 500 ) on the system (unix).
>>> Parse the output of the cat command:
>>>
>>> cat /etc/passwd | perl -e 'while(<>){ /^(\w+):x:(\d+):/; if ( $2 > 500 ) { print "$1\n"; }}'
>>
>> UUOC. In fact you can use Perl's built-in getpw* functions.
>
> This is just 'another' way :)
>
>>
>> perl -le'$a[2] > 500 and print $a[2] while @a = getpwent'
>>
>>
>>> b) Print the environment variables:
>>>
>>> perl -e 'foreach $key ( keys %ENV ) { print "$key=$ENV{$key}\n"; }'
>>
>> man env
>>
>
> Again - yet another way :) I tried to illustrate how you can use Perl -
> not only the command line utilities. As I mentioned, many of the examples
> can also be done in gawk(1).
>
>>
>>> c) Print number of empty lines in a text file:
>>>
>>> cat program.pl | perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";'
>>
>> UUOC. It also doesn't count empty lines correctly. You do realise that
>> the expression (<>!=~/^\W$/) evaluates to ( <> != ~ /^\W$/ )? You do
>> realise that (<>)&&(<> reads two lines every time through the loop?
>>
>
> My function works 100% on Linux. I have test now again several times with
> various files - the output is accurate and consistant.
>
>> perl -lne'$c += /^\s*$/}{print $c' program.pl
>
> Gives LESS lines then there actually is :(
>
>>
>>
>>> d) How many lines in a text file contains the TAB character:
>>>
>>> cat parser.pl | perl -e '$c=0;while(<>){ if ( /\t/ ) {$c++;}}print "$c\n";'
>>
>> UUOC.
>>
>> perl -lne'$c += /\t/}{print $c' parser.pl
>>
>
> This is cool :)
>
>>
>>
>> John
>
> Cheers
--
Nico Coetzee
http://www.itfirms.co.za/
http://za.pm.org/
http://forums.databasejournal.com/
To the systems programmer, users and applications serve only to provide a
test load.
------------------------------
Date: Sun, 11 May 2003 08:38:56 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Powerful One-liner
Message-Id: <3EBE0BF6.B88FD252@acm.org>
Nico Coetzee wrote:
>
> As I said - all was tested on my Linux system, and the output verifies
> with known correct values.
>
> On Sat, 10 May 2003 21:48:56 +0000, John W. Krahn wrote:
>
> > Nico Coetzee wrote:
> >>
> >> c) Print number of empty lines in a text file:
> >>
> >> cat program.pl | perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";'
> >
> > UUOC. It also doesn't count empty lines correctly. You do realise that
> > the expression (<>!=~/^\W$/) evaluates to ( <> != ~ /^\W$/ )? You do
> > realise that (<>)&&(<> reads two lines every time through the loop?
>
> My function works 100% on Linux. I have test now again several times with
> various files - the output is accurate and consistant.
>
> > perl -lne'$c += /^\s*$/}{print $c' program.pl
>
> Gives LESS lines then there actually is :(
I guess that depends on how you define "empty lines". :-)
$ perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";' <<TEXT
TEXT
1
$ perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";' <<TEXT
0
0
TEXT
1
$ perl -e '$c=0;while((<>)&&(<>!=~/^\W$/)){$c++;}print "$c\n";' <<TEXT
text
text
TEXT
1
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sun, 11 May 2003 03:02:29 GMT
From: pls@post.here (Adam)
Subject: prob getting SNMP module to recognize my OID
Message-Id: <lderbvkkrg0qscv4m7i9ajuk6jgspfkbt0@4ax.com>
Hello all,
I seem to be misunderstanding the SNMP module's documentation as I
have been unable to get a simple value using their example. It
doesn't even get to contacting the server as it is not recognizing my
OID! For get() I have tried *many* variables taken directly from the
Squid MIB file. Cmdline snmpwalk from this same package (net-snmp)
works fine when given version 1, the non-standard port, and an SBMP
variable. So I am doing something wrong, what is it? Any pointers to
working code? All I really want to do is read all the values, which I
can when I feed snmpwalk "enterprises.nlanr.squid"
I also include, at the bottom, the error that gets stored in $sess.
thanks,
Adam
#!/usr/local/bin/perl -w
use strict;
use SNMP;
SNMP::addMibDirs(</usr/local/share/snmp/mibs>);
SNMP::loadModules(</usr/local/share/snmp/mibs/SQUID-MIB.txt>);
#[also just tried SQUID-MIB.txt since I have the dir added above]
my $value;
my $key;
my $sess = new SNMP::Session(DestHost => "squidbox", Community =>
"public", Version => 1, RemotePort => 3401 );
$sess->get('cacheUptime.0');
# [ tried many variables above ]
my $result = $sess;
my %hash = %{$result};
print "ref session = ", ref($sess);
while ( ($key, $value) = each %hash) {
print "Key: $key = value: $value\n";
}
exit;
$ ./snmp.pl
error: get: unknown object ID (cacheUptime) at
/usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/SNMP.pm line 575.
ref session = SNMP::SessionKey: UseLongNames = value: 0
Key: UseEnums = value: 0
Key: UseNumeric = value: 0
Key: LocalPort = value: 0
Key: SessPtr = value: SnmpSessionPtr=SCALAR(0x217b10)
Key: ErrorStr = value: Unknown Object Identifier
Key: UseSprintValue = value: 0
Key: Community = value: public
Key: ErrorNum = value: -58
Key: RetryNoSuch = value: 0
Key: Retries = value: -1
Key: RemotePort = value: 3401
Key: Version = value: 1
Key: ErrorInd = value: 0
Key: DestHost = value: squidbox:3401
Key: Timeout = value: -1
------------------------------
Date: Sun, 11 May 2003 08:39:43 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: RegExp Question
Message-Id: <pan.2003.05.10.07.28.41.842406@kamelfreund.de>
Gunnar Hjalmarsson wrote at Sat, 10 May 2003 00:19:23 +0200:
> Anthony Saffer wrote:
>> Let's say I have an entry in a log file like this:
>>
>> 1/13/03 "Joe Somebody"<joe@somewhere.com>
>>
>> How would I extract ONLY the email address from this line?
>
> $line = '1/13/03 "Joe Somebody"<joe@somewhere.com>';
> ($address) = $line =~ /<([^>]+)>/;
Or a bit shorter:
($address) = $line =~ /<(.+?)>/;
Cheerio,
Janek
------------------------------
Date: Sun, 11 May 2003 01:12:58 GMT
From: "Brad Walton" <sammie@greatergreen.com>
Subject: Re: Splitting, Sorting, then Rebuilding an Array
Message-Id: <ushva.806252$F1.101055@sccrnsc04>
> #Format info for return to archive
> #?? $updatearchive = (\t,@newsess);
> return;
> }
>
> Still not sure how to make $updatearchive = @newsess with tabs in betweeen
> each variable...
Ok I got it working, and much less code. Could probably still be better, but
this is working:
sub update_player_archive {
#Grab session variables
my (@newsess,$n);
my @currentsess = split /\t/, $adddata;
my @oldsess = split /\t/, $updatedata;
#Increment archive data with new data
for ($n = 0; $n < 6; $n++) {
@newsess[$n] = @currentsess[$n];
}
@newsess[6] = (@oldsess[6] + @currentsess[6]);
@newsess[7] = @currentsess[7];
for ($n = 8; $n < 32; $n++) {
@newsess[$n] = (@oldsess[$n] + @currentsess[$n]);
}
for ($n = 32; $n < 34; $n++) {
unless (!@currentsess[$n]) {
@newsess[$n] = @currentsess[$n];
}
else {
@newsess[$n] = @oldsess[$n];
}
}
#Format info for return to archive
push @updatearchive, join "\t", @newsess[0..33];
return;
}
Any problems or tips you see or have would be appreciated.
Thanks,
Brad
------------------------------
Date: Sun, 11 May 2003 08:39:45 +0200
From: "Janek Schleicher" <bigj@kamelfreund.de>
Subject: Re: Splitting, Sorting, then Rebuilding an Array
Message-Id: <pan.2003.05.10.07.18.16.110110@kamelfreund.de>
Brad Walton wrote at Sat, 10 May 2003 09:25:42 +0000:
> This has got to be the hard way to write this... If you know of a more
> efficient way, it would be appreciated:
>
> sub sort_by_name {
> my $rebuild;
>
> #Grab data and sort by callsign
> foreach $archivedata (@archivedata) {
> my
> ($sess,$cals,$plid,$mapid,$tcur,$tcon,$scor,$stat,$eak,$esak,$etk,$eck,$eatk
> ,$eaaak,$ewk,$esp,$fak,
>
> $fsak,$ftk,$fck,$fatk,$faaak,$fwg,$fsp,$fb,$hb,$hab,$fr,$hr,$fbmb,$hbmb,$ipa
> d,$ctry,$empty,$emptyy) = split /\t/, $archivedata;
> $cals =~ tr/a-z/A-Z/;
> $rebuild =
> "$sess\t$cals\t$plid\t$mapid\t$tcur\t$tcon\t$scor\t$stat\t$eak\t$esak\t$etk\
> t$eck\t$eatk\t$eaaak\t$ewk\t$esp\t$fak\t$fsak\t$ftk\t$fck\t$fatk\t$faaak\t$f
> wg\t$fsp\t$fb\t$hb\t$hab\t$fr\t$hr\t$fbmb\t$hbmb\t$ipad\t$ctry\t$empty\t$emp
> tyy";
If I understand the last few lines right,
all you want to do is to uppercase the second element of a tab separated
file, of course there's a shorter way:
my @col = split /\t/, $archivedata;
$col[1] = uc $col[1]; # or I prefer to write $_ = uc for $col[1]
$rebuild = join "\t", @col;
> @formateddata = (@formateddata,$rebuild);
> }
> @formateddata = sort(@formateddata);
> }
A very short way to write the whole snippet could be:
sub sort_by_name {
@formateddata = sort map {my @d = split /\t/;
$_ = uc for $d[1];
join "\t", @d} @archivedata;
}
[untested]
Best Wishes,
Janek
------------------------------
Date: 11 May 2003 01:33:14 -0700
From: big__smile@hotmail.com (big__smile)
Subject: stat and underscore in file name
Message-Id: <299205ff.0305110033.16b2e49c@posting.google.com>
hey!!
i have a problem when i use stat($file) if $file has a filename with
underscore it stuffs up and says use of uninitialized value
help!!!!!!
------------------------------
Date: Sun, 11 May 2003 09:06:51 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: stat and underscore in file name
Message-Id: <3EBE11BA.EAB02364@acm.org>
big__smile wrote:
>
> i have a problem when i use stat($file) if $file has a filename with
> underscore it stuffs up and says use of uninitialized value
Are you sure? Which OS?
$ ls -l _
-rw-r--r-- 1 john users 187 May 11 01:59 _
$ perl -le'$file = "_"; @s = stat($file); print "@s"'
769 136629 33188 1 500 100 0 187 1052643557 1052643544 1052643544 4096 8
John
--
use Perl;
program
fulfillment
------------------------------
Date: Sun, 11 May 2003 13:33:57 +0800
From: "ldd" <ddl76@singnet.com.sg>
Subject: Why Perl is so difficult to learn?
Message-Id: <b9kmor$9jt$1@mawar.singnet.com.sg>
Hi,
I am a very newbie. Have a few question regarding Perl
1) What does the second asterisk do in this expression?
$line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
2) What is the difference between these 3 statements from number 1?
$line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
$line =~ s/^([^ ] *) *([^ ] *)/$2 $1/;
$line =~ s/^([^ ] *) *([^ ])*/$2 $1/;
3) If I want to swap the positions of entire word ending with "ood" and
"uter", can anyone provide and example?
$line = "this is a good testing computer";
Thanks
------------------------------
Date: Sun, 11 May 2003 02:17:29 -0400
From: Mina Naguib <spam@thecouch.homeip.net>
Subject: Re: Why Perl is so difficult to learn?
Message-Id: <WVlva.22625$Bm2.231522@wagner.videotron.net>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
ldd wrote:
> Hi,
>
> I am a very newbie. Have a few question regarding Perl
>
> 1) What does the second asterisk do in this expression?
The same thing the first asterisk does in the expression. As a matter
of fact, the same thing any asterisk does in any perl-style regular
expression.
See perldoc perlre or http://www.perldoc.com/perl5.8.0/pod/perlre.html
>
> $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
>
> 2) What is the difference between these 3 statements from number 1?
>
> $line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
^
an extra space here --------+
>
> $line =~ s/^([^ ] *) *([^ ] *)/$2 $1/;
^ ^
extra spaces here -+---------+
>
> $line =~ s/^([^ ] *) *([^ ])*/$2 $1/;
^
extra space here --+
>
> 3) If I want to swap the positions of entire word ending with "ood" and
> "uter", can anyone provide and example?
>
> $line = "this is a good testing computer";
TIMTOWTDI - There Is More Than One Way To Do It
One way is to split the sentence into array elements over the space(s)
using the split function - see perldoc -f split or
http://www.perldoc.com/perl5.8.0/pod/func/split.html
Then loop over the array with for. Use a simple regular expression to
find if the element matches "ood" or "uter", record their index
locations and values. Then after the loop, do the swapping using the
temporary variables you remembered during the loop.
Alternatively you can use 1 substitution regex like so:
$line =~ s/(\w+(?:ood|uter)\b)(.*?)(\w+(?:ood|uter)\b)/$3$2$1/;
Although it has the limitation of doing only 1 switch, and could
mistakenly do no substitution of a word like "computer" existed twice in
a sentence. It will also not work if the 2 words are separated by a
newline, etc.. It'll work your your sample data in $line, however since
you haven't outlined all your requirements, it might not work for any
data you might throw at it.
Some of the above limitations are easily solved by small tweaks to the
regex, some are inherent limitations to using a regex for this type of
problem, and all of it is explained quite well in the first URL in my reply.
Best of luck.
-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQE+ver9eS99pGMif6wRAv1bAKCaUWfEu6y3bmDid5H7fb/6zMaxwACgyJSZ
Q6nP1JfaAoePDy0KDSteVBk=
=gDlg
-----END PGP SIGNATURE-----
------------------------------
Date: Sun, 11 May 2003 15:02:20 +0800
From: "ldd" <ddl76@singnet.com.sg>
Subject: Re: Why Perl is so difficult to learn?
Message-Id: <b9kruh$9u6$1@mawar.singnet.com.sg>
Sorry I mean why is the oufput so different between these 3 examples?
$line = "this is a test";
eg1) $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
print "$line\n";
$line = "this is a test";
2) $line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
print "$line\n";
$line = "this is a test";
3) $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
print "$line\n";
$line = "this is a test";
I really can't figure out 2 and 3 egs.
"Mina Naguib" <spam@thecouch.homeip.net> wrote in message
news:WVlva.22625$Bm2.231522@wagner.videotron.net...
> -----BEGIN xxx SIGNED MESSAGE-----
> Hash: SHA1
>
>
> ldd wrote:
> > Hi,
> >
> > I am a very newbie. Have a few question regarding Perl
> >
> > 1) What does the second asterisk do in this expression?
>
> The same thing the first asterisk does in the expression. As a matter
> of fact, the same thing any asterisk does in any perl-style regular
> expression.
>
> See perldoc perlre or http://www.perldoc.com/perl5.8.0/pod/perlre.html
>
> >
> > $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
> >
> > 2) What is the difference between these 3 statements from number 1?
> >
> > $line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
> ^
> an extra space here --------+
>
> >
> > $line =~ s/^([^ ] *) *([^ ] *)/$2 $1/;
> ^ ^
> extra spaces here -+---------+
>
>
> >
> > $line =~ s/^([^ ] *) *([^ ])*/$2 $1/;
> ^
> extra space here --+
>
> >
> > 3) If I want to swap the positions of entire word ending with "ood" and
> > "uter", can anyone provide and example?
> >
> > $line = "this is a good testing computer";
>
> TIMTOWTDI - There Is More Than One Way To Do It
>
> One way is to split the sentence into array elements over the space(s)
> using the split function - see perldoc -f split or
> http://www.perldoc.com/perl5.8.0/pod/func/split.html
>
> Then loop over the array with for. Use a simple regular expression to
> find if the element matches "ood" or "uter", record their index
> locations and values. Then after the loop, do the swapping using the
> temporary variables you remembered during the loop.
>
> Alternatively you can use 1 substitution regex like so:
> $line =~ s/(\w+(?:ood|uter)\b)(.*?)(\w+(?:ood|uter)\b)/$3$2$1/;
>
> Although it has the limitation of doing only 1 switch, and could
> mistakenly do no substitution of a word like "computer" existed twice in
> a sentence. It will also not work if the 2 words are separated by a
> newline, etc.. It'll work your your sample data in $line, however since
> you haven't outlined all your requirements, it might not work for any
> data you might throw at it.
>
> Some of the above limitations are easily solved by small tweaks to the
> regex, some are inherent limitations to using a regex for this type of
> problem, and all of it is explained quite well in the first URL in my
reply.
>
> Best of luck.
>
> -----BEGIN xxx SIGNATURE-----
> Version: GnuPG v1.2.1 (GNU/Linux)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
> iD8DBQE+ver9eS99pGMif6wRAv1bAKCaUWfEu6y3bmDid5H7fb/6zMaxwACgyJSZ
> Q6nP1JfaAoePDy0KDSteVBk=
> =gDlg
> -----END PGP SIGNATURE-----
>
------------------------------
Date: 11 May 2003 08:06:50 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: Why Perl is so difficult to learn?
Message-Id: <b9l0aq$8iq$1@nets3.rz.RWTH-Aachen.DE>
Also sprach ldd:
> I am a very newbie. Have a few question regarding Perl
>
> 1) What does the second asterisk do in this expression?
>
> $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
An asteriks in regular expressions is a quantifier relating to the
character (or pattern) preceeding it. In the above case it follows a
space. So it matches between zero and arbitrarily many spaces. It is
greedy, that is, it tries to match as many as possible.
> 2) What is the difference between these 3 statements from number 1?
>
> $line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
The second capturing pair of parens now matches one character that is
not a space followed by many or zero spaces. It will match for instance
"a "
"a "
but not
"ab "
"a b "
> $line =~ s/^([^ ] *) *([^ ] *)/$2 $1/;
Now the same as above is valid for the first pair of parens.
> $line =~ s/^([^ ] *) *([^ ])*/$2 $1/;
([^ ])* matches as many non-space characters as there are. But: the
parens can only capture one character so they capture the last charater
of such a string:
$_ = "abc ";
/([^ ])*/ and print $1;
__END__
c
> 3) If I want to swap the positions of entire word ending with "ood" and
> "uter", can anyone provide and example?
>
> $line = "this is a good testing computer";
This should work:
s/(.*?)([^ ]*?ood\b)(.*?)([^ ]*?uter\b)/$1$4$3$2/g;
The \b assertion makes sure that 'ood' and 'uter' only match when they
are followed by a word-boundary, that is, if they are at the end of the
word in this case. The ? following a * makes the match non-greedy which
means: match as few characters as possible.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Sun, 11 May 2003 08:48:57 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Why Perl is so difficult to learn?
Message-Id: <3EBE0E4C.884BD2F6@acm.org>
ldd wrote:
>
> I am a very newbie. Have a few question regarding Perl
>
> 1) What does the second asterisk do in this expression?
>
> $line =~ s/^([^ ]*) *([^ ]*)/$2 $1/;
>
> 2) What is the difference between these 3 statements from number 1?
>
> $line =~ s/^([^ ]*) *([^ ] *)/$2 $1/;
>
> $line =~ s/^([^ ] *) *([^ ] *)/$2 $1/;
>
> $line =~ s/^([^ ] *) *([^ ])*/$2 $1/;
[^ ] is a single non-space character. [^ ]* is zero or more non-space
characters. [^ ] * is a single non-space character followed by zero or
more space characters.
> 3) If I want to swap the positions of entire word ending with "ood" and
> "uter", can anyone provide and example?
>
> $line = "this is a good testing computer";
$line =~ s/(\w+ood)(.*)(\w+uter)/$3$2$1/;
John
--
use Perl;
program
fulfillment
------------------------------
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.
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 4971
***************************************