[25111] in Perl-Users-Digest
Perl-Users Digest, Issue: 7361 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 5 09:05:40 2004
Date: Fri, 5 Nov 2004 06:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Fri, 5 Nov 2004 Volume: 10 Number: 7361
Today's topics:
Re: DEFINEs in Perl? <do-not-use@invalid.net>
Re: DEFINEs in Perl? <do-not-use@invalid.net>
Re: DEFINEs in Perl? (Anno Siegel)
Re: DEFINEs in Perl? <tassilo.von.parseval@rwth-aachen.de>
EOF issue <onedoesnot@needto.know>
Re: EOF issue <Graham.T.removethis.Wood@oracle.andthis.com>
Re: EOF issue <noreply@gunnar.cc>
Re: EOF issue <wyzelli@yahoo.com>
Re: EOF issue <onedoesnot@needto.know>
Re: EOF issue <peter@semantico.com>
FAQ 9.2: My CGI script runs from the command line but n <comdog@panix.com>
Re: Global symbol requires explicit package name (Anno Siegel)
Re: Load information in Solaris (Perl Script.) <dog@dog.dog>
Re: Load information in Solaris (Perl Script.) <bik.mido@tiscalinet.it>
Re: map()'s BLOCK [was: "Re: Q: re Inline and Benchmark (Anno Siegel)
Net::NIS crashing with Sendmail::Milter <nba@abo.fi>
Re: not logical <mshelor@cpan.dot.org>
Re: number of characters in a string (Anno Siegel)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 05 Nov 2004 10:54:31 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: DEFINEs in Perl?
Message-Id: <yzdfz3oy620.fsf@invalid.net>
"daniel kaplan" <nospam@nospam.com> writes:
> [description of macros]
> Sorry for the long explination, but didn't know how to call DEFINE for those
> who don't know C, but might know the equivalent in Perl.
Non-Perlish suggestion: if you find you can't do this within Perl, you
you can maybe use an external macro processor to preprocess the Perl
source code. Two that come to mind are the C preprocessor, and the
Unix 'm4'.
------------------------------
Date: 05 Nov 2004 11:00:31 +0100
From: Arndt Jonasson <do-not-use@invalid.net>
Subject: Re: DEFINEs in Perl?
Message-Id: <yzdbrecy5s0.fsf@invalid.net>
"Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
> Compilation at runtime, well, that's
> almost a contradiction in itself.
Not really. There are or have been languages that compile the source
code (or some simple transformation of it) only when it is executed
the first time.
What Perl does with a regular expression just before using it for
matching (or just once, if /o is used) is called compilation, too.
------------------------------
Date: 5 Nov 2004 10:13:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: DEFINEs in Perl?
Message-Id: <cmfjor$cfq$1@mamenchi.zrz.TU-Berlin.DE>
daniel kaplan <nospam@nospam.com> wrote in comp.lang.perl.misc:
> Hi all,
>
> I tried looking this up in perldoc and faq, but other than using the word
> DEFINE (where I know it from C)I don't know how else to describe it so can't
> find it.
>
> In Perl I am writing:
>
> sub_XYZ (value_a, value_b, 1);
Unlikely. You may be writing "sub_XYZ ($value_a, $value_b, 1)" or
similar. Please take some care with the code you present (even pseudo-
code). Hundreds of readers have to read the line twice and conclude
you meant one thing but wrote another.
> within sub_XYZ I have:
>
> if($third_value == 1)
> do this
> elsif($third_value == 2)
> do that
> elsif($third_value == 3)
> do the other thing
More sloppy pseudo-code. Perl code would be better.
The possible use of "constant" has been pointed out. However, the
standard Perl way of handling this is a dispatch table:
my %action = (
do_this => sub {
# do this
},
do_that => sub {
# do that
},
other_thing => sub {
# do other thing
},
);
In sub_XYZ, replace the if-elsif sequence with
$action{ $third_value}->();
and call sub_XYZ as
sub_XYZ( $value_a, $value_b, 'do_this');
That is perfectly self-documenting and maintainable.
Anno
------------------------------
Date: Fri, 5 Nov 2004 12:14:45 +0100
From: "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de>
Subject: Re: DEFINEs in Perl?
Message-Id: <slrncomo55.5ie.tassilo.von.parseval@localhost.localdomain>
Also sprach Arndt Jonasson:
> "Tassilo v. Parseval" <tassilo.von.parseval@rwth-aachen.de> writes:
>> Compilation at runtime, well, that's
>> almost a contradiction in itself.
>
> Not really. There are or have been languages that compile the source
> code (or some simple transformation of it) only when it is executed
> the first time.
Which is why I said almost.
> What Perl does with a regular expression just before using it for
> matching (or just once, if /o is used) is called compilation, too.
It's a different kind of compilation as a pattern isn't compiled into an
op. Instead, the pattern is prepared into something the regular
expression understands when the match-op is due.
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: Fri, 5 Nov 2004 12:13:52 -0000
From: "IanW" <onedoesnot@needto.know>
Subject: EOF issue
Message-Id: <cmfrib$qcn$1@blackmamba.itd.rl.ac.uk>
I'm looping through each line of a text file, with a format like this:
# start of file
===============
Line 1
Line 2
Line 3
===============
Line 1
Line 2
Line 3
===============
Line 1
Line 2
Line 3
# end of file
I am processing it in chunks where "===============" is the start of each
chunk.
So after opening the file (FH), I while-loop through it like this:
my @chunk = ();
while(my $ln = <FH>){
if(($ln =~ /^={15}$/ or eof) and scalar @chunk > 0){
# process @chunk
undef @chunk;
}
push(@chunk,$ln);
}
That works nicely except that when it gets to the end of the file "Line 3"
above is not added to @chunk. I would have expected eof to test positive
when it gets to the end of the blank line. Am I missing something obvious
here or is there a quirk with the usage of eof?
IanW
------------------------------
Date: Fri, 05 Nov 2004 12:33:49 +0000
From: Graham Wood <Graham.T.removethis.Wood@oracle.andthis.com>
Subject: Re: EOF issue
Message-Id: <1CKid.15$DY5.61@news.oracle.com>
IanW wrote:
> I'm looping through each line of a text file
<snip>
>
> my @chunk = ();
> while(my $ln = <FH>){
> if(($ln =~ /^={15}$/ or eof) and scalar @chunk > 0){
> # process @chunk
> undef @chunk;
> }
> push(@chunk,$ln);
> }
>
> That works nicely except that when it gets to the end of the file "Line 3"
> above is not added to @chunk. I would have expected eof to test positive
> when it gets to the end of the blank line. Am I missing something obvious
> here or is there a quirk with the usage of eof?
>
> IanW
Your problem is that your while loop exits when it hits eof and
therefore you never hit the if statement when eof is true.
I'd suggest removing the eof from the condition and adding a "process
@chunk" after the while loop.
Someone will doubtless suggest a more elegant solution.
Graham
------------------------------
Date: Fri, 05 Nov 2004 14:16:41 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: EOF issue
Message-Id: <2v1d0lF2gfi03U1@uni-berlin.de>
IanW wrote:
> I'm looping through each line of a text file, with a format like
> this:
>
> # start of file
> ===============
> Line 1
> Line 2
> Line 3
> ===============
> Line 1
> Line 2
> Line 3
> ===============
> Line 1
> Line 2
> Line 3
>
> # end of file
>
> I am processing it in chunks where "===============" is the start of
> each chunk.
You can try playing with the $/ variable. See "perldoc perlvar".
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Fri, 05 Nov 2004 13:35:54 GMT
From: "Peter Wyzl" <wyzelli@yahoo.com>
Subject: Re: EOF issue
Message-Id: <_kLid.15873$K7.1192@news-server.bigpond.net.au>
"IanW" <onedoesnot@needto.know> wrote in message
news:cmfrib$qcn$1@blackmamba.itd.rl.ac.uk...
> I'm looping through each line of a text file, with a format like this:
>
<snip>
> I am processing it in chunks where "===============" is the start of each
> chunk.
>
> So after opening the file (FH), I while-loop through it like this:
>
> my @chunk = ();
> while(my $ln = <FH>){
> if(($ln =~ /^={15}$/ or eof) and scalar @chunk > 0){
> # process @chunk
> undef @chunk;
> }
> push(@chunk,$ln);
> }
>
> That works nicely except that when it gets to the end of the file "Line 3"
> above is not added to @chunk. I would have expected eof to test positive
> when it gets to the end of the blank line. Am I missing something obvious
> here or is there a quirk with the usage of eof?
Here is how I would approach that problem...
(assume filehandle "IN" already opened for reading)
my @chunks; # dont need to explicitly declare an empty list, that is
default.
{
local $/ = ''; #slurp mode
@chunks = split /={15}\n/, <IN>; # puts all the 'chunks' into @chunks in
one read
}
# close <IN> here..
for (@chunks){
#process chunks...
}
BTW, your problem is that the while loop encounters the eof and self
terminates therefore your test never happens. Part of the magic of the
while (<FH>){
}
construct...
HTH
--
Wyzelli
${^__}=[qw(Hacker Perl another Just)];
------------------------------
Date: Fri, 5 Nov 2004 13:45:58 -0000
From: "IanW" <onedoesnot@needto.know>
Subject: Re: EOF issue
Message-Id: <cmg0v3$sgt$1@blackmamba.itd.rl.ac.uk>
"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:2v1d0lF2gfi03U1@uni-berlin.de...
> IanW wrote:
> > I'm looping through each line of a text file, with a format like
> > this:
> >
> > # start of file
> > ===============
> > Line 1
> > Line 2
> > Line 3
> > ===============
> > Line 1
> > Line 2
> > Line 3
> > ===============
> > Line 1
> > Line 2
> > Line 3
> >
> > # end of file
> >
> > I am processing it in chunks where "===============" is the start of
> > each chunk.
>
> You can try playing with the $/ variable. See "perldoc perlvar".
Well, yes, I could split the file into chunks with something like this:
open(FH,$file);
local $/;
$lines = <FH>;
close(FH);
@chunks = $lines =~ /={15}.+?(?=\={15}|$)/g;
foreach(@chunk){
# process chunk
}
(that's untested so there's probably something wrong with it, but I guess
you know what I mean)
but I'd still like to know why eof isn't working as I would expect it to.
Bigus
------------------------------
Date: Fri, 05 Nov 2004 14:02:53 +0000
From: Peter Hickman <peter@semantico.com>
Subject: Re: EOF issue
Message-Id: <418b8813$0$27237$afc38c87@news.easynet.co.uk>
IanW wrote:
> my @chunk = ();
> while(my $ln = <FH>){
> if(($ln =~ /^={15}$/ or eof) and scalar @chunk > 0){
> # process @chunk
> undef @chunk;
> }
> push(@chunk,$ln);
> }
The while loop is only executed whilst there is data to process, once the files
is passed the end then the loop stops processing. So the eof part of your test
is never reached. Thus after the loop is completed you have data in @chunk but
do not try to process it.
my @chunk = ();
while(my $ln = <FH>){
if($ln =~ /^={15}$/){
process( @chunk ) if @chunk;
undef @chunk;
}
push(@chunk,$ln);
}
process( @chunk ) if @chunk;
eof is rarely used, in my experience.
------------------------------
Date: Fri, 5 Nov 2004 11:03:01 +0000 (UTC)
From: PerlFAQ Server <comdog@panix.com>
Subject: FAQ 9.2: My CGI script runs from the command line but not the browser. (500 Server Error)
Message-Id: <cmfml5$8de$1@reader1.panix.com>
This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.
--------------------------------------------------------------------
9.2: My CGI script runs from the command line but not the browser. (500 Server Error)
Several things could be wrong. You can go through the "Troubleshooting
Perl CGI scripts" guide at
http://www.perl.org/troubleshooting_CGI.html
If, after that, you can demonstrate that you've read the FAQs and that
your problem isn't something simple that can be easily answered, you'll
probably receive a courteous and useful reply to your question if you
post it on comp.infosystems.www.authoring.cgi (if it's something to do
with HTTP or the CGI protocols). Questions that appear to be Perl
questions but are really CGI ones that are posted to comp.lang.perl.misc
are not so well received.
The useful FAQs, related documents, and troubleshooting guides are
listed in the CGI Meta FAQ:
http://www.perl.org/CGI_MetaFAQ.html
--------------------------------------------------------------------
Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short. They represent an important
part of the Usenet tradition. They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.
If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile. If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.
Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release. It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.
The perlfaq manual page contains the following copyright notice.
AUTHOR AND COPYRIGHT
Copyright (c) 1997-2002 Tom Christiansen and Nathan
Torkington, and other contributors as noted. All rights
reserved.
This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.
------------------------------
Date: 5 Nov 2004 09:29:01 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Global symbol requires explicit package name
Message-Id: <cmfh4t$ass$1@mamenchi.zrz.TU-Berlin.DE>
snowdog <snowdogdb@yahoo.com> wrote in comp.lang.perl.misc:
> Can someone please help me. I've got a perl script I've written to
> send notifications to another application. The following lines are in
> the same sub under the same package (line#'s included for reference).
Are they in the same lexical scope?
> The line 255 clearly defined the $action variable. Line 341 tries to
> use it in a method for $notificationObj. Perl is telling me that
> global symbol $action requires explicit package name on line 341.
> Why, it IS defined on line 255.
A lot can happen in the intervening 80+ lines.
> Funny thing is, I didn't have this problem until I moved the entire
> code block under a package main;
If the package statement is relevant, your code should show it.
> statement (I'm trying to get this
> thing to run as a service).
What does that mean, and how does it require a "package main" statement?
> 255: my $action = "notify";
> 341: my $notificationResult = $notificationObj->invoke($action,
> "HowIs_Adapter",
> $main::configRef->{'Config.howisServer'},
> "HowIs Alarm",
> );
Your code doesn't demonstrate the error (it would complain about
"$notificationObj"). Prepare a self-contained piece of code that
can be run anywhere and shows your problem. My guess is that in
doing so you will find the error yourself, otherwise post it.
Anno
------------------------------
Date: Fri, 5 Nov 2004 08:46:27 +0100
From: "Peter Michael" <dog@dog.dog>
Subject: Re: Load information in Solaris (Perl Script.)
Message-Id: <cmfb51$8d0$1@tgx093.str.allianz.de>
Alice,
"Alice Stamp" <alstamp@gmail.com> schrieb im Newsbeitrag
news:f3849269.0411041709.66d46b10@posting.google.com...
> I have a script which makes use of the cpu data which I can read from
> /proc/stat on Linux. I need to know the number of jiffies the cpu has
> spent in user, system, and idle modes since boot up - easy enough to
> do on Linux :
[snip]
> I now need to write a similar perl script which does the same on
> solaris (i.e. end up with the amount of time the cpu has spent in each
> mode in three variables.
Have a look at the Solaris::Kstat module.
HTH,
Peter
------------------------------
Date: Fri, 05 Nov 2004 14:33:30 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: Load information in Solaris (Perl Script.)
Message-Id: <rkemo0dshlk3nr9fgknb6ih5tsl6c95hsj@4ax.com>
On 4 Nov 2004 17:09:50 -0800, alstamp@gmail.com (Alice Stamp) wrote:
>open (DATA, "/proc/stat");
As a side note to what has already been said, that is an unfortunate
choice as the name for your fh. Why not using a lexical fh anyway?
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: 5 Nov 2004 11:43:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: map()'s BLOCK [was: "Re: Q: re Inline and Benchmark"]
Message-Id: <cmfp06$gft$1@mamenchi.zrz.TU-Berlin.DE>
Michele Dondi <bik.mido@tiscalinet.it> wrote in comp.lang.perl.misc:
> On Wed, 03 Nov 2004 00:23:13 +0100, Michele Dondi
> <bik.mido@tiscalinet.it> wrote:
>
> > { map {
> > no strict 'refs';
> > $_ => \&{$_};
> > } qw/sol1 sol2 .../ };
>
> Except that this won't work! When I try it in actual code I get
>
> "no" not allowed in expression at foo.pl line 12, at end of line
> BEGIN not safe after errors--compilation aborted at foo.pl line 12.
>
> Of course I can do
>
> {
> no strict 'refs';
> cmpthese 500, { map { $_ => \&{$_} }
> qw/sol1 sol2 etc/ };
> }
>
> instead, but IMHO the no-strict-'refs'-ization logically best applies
> to the map()'s code block. Now I know that there are easy workarounds,
> e.g. using the "EXPR-form" of map() with a do(); still I wonder why
> the block of the "BLOCK-form" of map() doesn't behave like a "normal"
> block.
Well, there are blocks in Perl, and then there are blocks. Bare blocks,
loop blocks and sub blocks all have slightly different rules about what
can legally appear inside, so I'm unsurprised that "use" (and "no")
can go in some but not in others.
The wording of the message is a bit misleading -- the immediate
environment of "no" is definitely a block, and "no ..." is a statement,
not part of an expression. I think I'll be able to live with it...
Anno
------------------------------
Date: Fri, 05 Nov 2004 16:02:37 +0200
From: Fredrik Nyberg DC <nba@abo.fi>
Subject: Net::NIS crashing with Sendmail::Milter
Message-Id: <418b885e$1@newsflash.abo.fi>
Hi!
I have written a milter script in perl that queries a NIS-server. After
several thousands, sometime hundreds of thousands, of connections, the
script always seems to crash when doing yp_match (tied version results
in the same behavior), like so:
<snip>
my $nis_status;
my $nis_value;
($nis_status, $nis_value) = Net::NIS::yp_match($domain, 'passwd.byname',
$username);
<snip>
Sometimes a Segmentation Fault is reported to the console, at othertimes
the program is just no longer running. Everything works fine until the
crash, there is no forewarning.
I am using perl 5.8.5 och Fedora Core 2 with the latest modules from CPAN.
Anyone here have any similar experiences or tips on how to debug?
Thank you,
Nybä
------------------------------
Date: Fri, 05 Nov 2004 06:36:02 -0700
From: Mark Shelor <mshelor@cpan.dot.org>
Subject: Re: not logical
Message-Id: <2a-dneJLa4daHBbcRVn-2A@comcast.com>
Anonymous wrote:
> Why doesn't ([^\s]) not produce the same result as (\S)?
>
> TIA
Your question uses a double-negative, and is therefore equivalent to:
Why does ([^\s]) produce the same result as (\S)?
Since (\S) does in fact produce the same result as ([^\s]), the
situation you describe is not "not logical" ;)
Regarding Tassilo's mention of Unicode, the following excerpt from
"perlre" might shed some light:
POSIX traditional Unicode
[:^digit:] \D \P{IsDigit}
[:^space:] \S \P{IsSpace}
[:^word:] \W \P{IsWord}
Locales can indeed affect the behavior of isspace(), but one would hope
that any such variations would apply equally to both [^\s] and \S.
Mark
------------------------------
Date: 5 Nov 2004 13:25:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: number of characters in a string
Message-Id: <cmfv06$j4m$1@mamenchi.zrz.TU-Berlin.DE>
Xevo <x3v0-usenet@yahoo.com> wrote in comp.lang.perl.misc:
> spamsaket@yahoo.com (machoq) wrote in message
> news:<e715de76.0411011551.3fb201c7@posting.google.com>...
> > I have reading a line from a text file
> > the line is like
> >
> > abc\"123
> > I need to find the number of characters in this string... (which
> > should be 8, however if i do a
> >
> > $var = $line;
> > print length($var); i get 7
> >
> > How do i workaround it ?
> > regards
> > -Machoq !
>
> Well if the line is "abc\"123", then the \ would be counted as a
> backreference for the " character. Therefore, \" is only counted as
^^^^^^^^^^^^^
Backreferences are a different case of "\". You mean "quote" or "escape"
(sometimes called "backwhack").
Anno
------------------------------
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 7361
***************************************