[17589] in Perl-Users-Digest
Perl-Users Digest, Issue: 5009 Volume: 9
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Dec 1 14:06:08 2000
Date: Fri, 1 Dec 2000 11:05:15 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <975697515-v9-i5009@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 1 Dec 2000 Volume: 9 Number: 5009
Today's topics:
Re: Anybody know what the carriage control characters a (Villy Kruse)
Best way to use av_push (XSUB) <eric.kort@vai.org>
chdir with CGI question <vautourNO@SPAMunb.ca>
Re: checking if mail address is valid (Villy Kruse)
Re: compute time difference nobull@mail.com
directing output to the active window fredm6816@my-deja.com
do u get fed up with regexp <xerxes_2k@my-deja.com>
Re: do u get fed up with regexp <jeff@vpservices.com>
Re: do u get fed up with regexp (Tad McClellan)
Re: do u get fed up with regexp <ren.maddox@tivoli.com>
exit() with negative value <mjcarman@home.com>
Re: exit() with negative value (Mark Wright)
Re: flock under Solaris behaves strangely nobull@mail.com
Re: force the browser to download instead of viewing th <mjcarman@home.com>
Re: force the browser to download instead of viewing th nobull@mail.com
Re: Gotta weird problem...disappearing variables <ronh@iainc.com>
Re: how do i escape from perldoc (Villy Kruse)
Re: how to print file to browser? nobull@mail.com
Re: how to print file to browser? nobull@mail.com
localtime problem under ActivePerl <Guido@VanHoecke.org>
Re: Most efficient way of getting a random row from db <mcameron@mirusweb.com>
Re: Need a web-based real-time option quote provider... (Tad McClellan)
need regular expression guru! fhinchey@my-deja.com
Re: next if length < 80; # why can't Perl be made to r (John J. Trammell)
Re: next if length < 80; # why can't Perl be made to r <ren.maddox@tivoli.com>
Re: next if length < 80; # why can't Perl be made to r nobull@mail.com
Re: next if length < 80; # why can't Perl be made to r <jeffp@crusoe.net>
Nice Perl 1-liner <gbw@theo1.physik.uni-stuttgart.de>
Re: Nice Perl 1-liner (Rafael Garcia-Suarez)
Re: Nice Perl 1-liner simbean@my-deja.com
Re: Nice Perl 1-liner (Tad McClellan)
Re: Nice Perl 1-liner <ren.maddox@tivoli.com>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 1 Dec 2000 18:42:37 GMT
From: vek@pharmnl.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: Anybody know what the carriage control characters are ?
Message-Id: <slrn92fs7m.8pk.vek@pharmnl.ohout.pharmapartners.nl>
On Thu, 30 Nov 2000 09:27:42 -0600, Michael Carman <mjcarman@home.com> wrote:
>
>I don't know all of those, (and I don't think that a "move to end of
>line" char even exists) but backspace is "\b" in Perl, and you can use
>"\f" (formfeed) to return to the beginning of the current line.
>
That would be '\r'. The result of formfeed is a bit undefied as there
is no paper form to feed on a screen. Often the result is blanking
the screen and start at the upper left corner.
So
\r do carriage return, that is return to start of same line.
\n do line feed (should be the same char pos, but sometime as
carriage return function is performed as well).
\f eject the current page and load the next.
\b move the print position one step backwards.
Villy
------------------------------
Date: Fri, 1 Dec 2000 11:32:21 -0500
From: "Eric" <eric.kort@vai.org>
Subject: Best way to use av_push (XSUB)
Message-Id: <908jhf$1535$1@msunews.cl.msu.edu>
I am using Inline::C to write an XSUB. I am processing an image, and when
certain events occur, I want to push a blue pixel (0,0,255) onto an existing
one dimensional array holding the RGB image data.
So I could do it this way:
av_push(avPtr_Image, newSViv(0), newSViv(0), newSViv(255));
This will occur tens of thousands of times per image, and I am worried that
this will create tens of thousands of SV*'s that will then be taking up
memory and not serving any purpose after I have pushed the values onto the
array.
Alternately, I could:
sv_setiv(svPtr_r, 0)
sv_setiv(svPtr_g, 0)
sv_setiv(svPtr_b, 255)
av_push(avPtr_Image, svPtr_r, svPtr_g, svPtr_b);
Which makes more sense, or neither?
Thanks,
Eric
------------------------------
Date: Fri, 01 Dec 2000 14:47:28 -0400
From: Gil Vautour <vautourNO@SPAMunb.ca>
Subject: chdir with CGI question
Message-Id: <3A27F240.B3F159AE@SPAMunb.ca>
Hello,
I have a Perl CGI script that is run from the Secure side (SSL) of my
server. Within the script I'm trying to chdir and output a file to my
home directory on the same server. The problem is that I get the "No
such file or directory error". The path is correct so I'm assuming it
is because the script doesn't have permissions to do this? Is there a
way that this can be done?
Thanks,
------------------------------
Date: 1 Dec 2000 18:48:03 GMT
From: vek@pharmnl.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: checking if mail address is valid
Message-Id: <slrn92fsih.8pk.vek@pharmnl.ohout.pharmapartners.nl>
On Thu, 30 Nov 2000 14:58:53 GMT,
Sander van Gennip <sander@veenhoven.com> wrote:
>
>This may seem like a naive remark, but mail servers do it don't they?
>I mean a mail server should connect to some foreign address before
>sending the mail to it. Failing to connect should be a reliable
>indication that the address is invalid (in which case the mail is
>bounched to the sender).
>
That is not how things work. The existence of a MX or A record for
the name is taken as evidence that the address is valid. If the
server can't be reached, the delivery will be retried repeatedly
for several days before the message is bounced. The same happens
if the DNS server for the domain doesn't answer. Only if the DNS
server says the name does not exist will the message be bounced
immidiately.
Villy
------------------------------
Date: 01 Dec 2000 18:43:58 +0000
From: nobull@mail.com
Subject: Re: compute time difference
Message-Id: <u91yvsgexd.fsf@wcl-l.bham.ac.uk>
bing-du@tamu.edu writes:
> Does Perl have a similar function to DateDiff in VB?
Yes, it has a FAQ too.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 01 Dec 2000 16:44:02 GMT
From: fredm6816@my-deja.com
Subject: directing output to the active window
Message-Id: <908kgg$dtj$1@nnrp1.deja.com>
How can I open an application (i.e. notepad) and be able to type text
into that application?
I'm relatively new to Perl, but I do know how to accomplish opening
applications using the system() command. I'm more concerned with how
to direct text output to that window.
Thanks,
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Fri, 01 Dec 2000 16:31:55 GMT
From: arse <xerxes_2k@my-deja.com>
Subject: do u get fed up with regexp
Message-Id: <908jpr$d9t$1@nnrp1.deja.com>
what is wrong with this regexp
it should replace the first character of each word in the string with a
uppercase letter if its lowercase
$name=~ s/\b([a-z])/uc($1)/eg;
thanx
--
[][][]{}{}~~';:.<<//?|1¬!"£$$%^^&*(())__+/*+
oooh random characters i must be coool!
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Fri, 01 Dec 2000 09:00:40 -0800
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: do u get fed up with regexp
Message-Id: <3A27D938.1374015A@vpservices.com>
arse wrote:
>
> what is wrong with this regexp
> it should replace the first character of each word in the string with a
> uppercase letter if its lowercase
>
> $name=~ s/\b([a-z])/uc($1)/eg;
No, I do not get fed up with regexp. I get fed up with people who post
"problems" without ever stating what the problem is and then whine about
Perl being hard to use. If that regex doesn't do what you think it
should, then show what input you gave it, what you thought the output
should be, and what it was instead. What I tried was this:
my $name = "the quick brown fox jumped over the lazy dog.";
$name =~ s/\b([a-z])/uc($1)/eg;
print $name;
And what it outputed was this:
The Quick Brown Fox Jumped Over The Lazy Dog.
Which is what I expected it to output.
--
Jeff
------------------------------
Date: Fri, 1 Dec 2000 11:27:10 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: do u get fed up with regexp
Message-Id: <slrn92fkau.9j9.tadmc@magna.metronet.com>
arse <xerxes_2k@my-deja.com> wrote:
> what is wrong with this regexp
Nothing.
>it should replace the first character of each word in the string with a
>uppercase letter if its lowercase
It does. There is something you are not telling us. Tell us.
>$name=~ s/\b([a-z])/uc($1)/eg;
You don't need eval(), you can get the job done just with interpolation:
$name =~ s/\b([a-z])/\U$1/g;
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 01 Dec 2000 10:46:19 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: do u get fed up with regexp
Message-Id: <m33dg82ip0.fsf@dhcp11-177.support.tivoli.com>
arse <xerxes_2k@my-deja.com> writes:
> what is wrong with this regexp
> it should replace the first character of each word in the string with a
> uppercase letter if its lowercase
>
> $name=~ s/\b([a-z])/uc($1)/eg;
Works just fine for me. Perhaps you could give a complete piece of
code that exhibits the problem?
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: Thu, 30 Nov 2000 13:04:21 -0600
From: Michael Carman <mjcarman@home.com>
Subject: exit() with negative value
Message-Id: <3A26A4B5.2CDF1FA7@home.com>
Is it practical to exit() a program with a negative value? Nothing in
the docs suggest that it isn't, but when I try to capture the return
value in the debugger for testing (e.g. $x = system('foo') / 256), I'm
getting unsigned values back. That is, if 'foo' does an exit(1), $x = 1.
But if 'foo' calls exit(-1), $x = 255.
So is exit() doing this or is system()? My money's on system(), but I'd
like a sanity check. Any suggestions on a work-around? At the moment all
I can come up with is this:
$x = system('foo') / 256;
if ($x >> 7) {
$x -= 256;
}
But I don't like that because it relies on the program calling 'foo' to
fiddle with the result. I'd like to 'foo' to return the right result
directly. Regardless of who's doing it, I seem to be limiting myself to
127 errors in a file before things become ambigous, but I'm okay with
that.
For reference, my desire to use negative numbers for exit status is
deliberate. I have a small program that searches files for a particular
type of error and returns the number of errors found. If something goes
wrong (like being unable to open a file) I want to abort with a
meaningful status. Since zero would imply that everything checked out
okay, and a positive number that the program worked and errors were
found, I'm left with all things < 0.
-mjc
------------------------------
Date: Fri, 01 Dec 2000 17:43:29 GMT
From: mark@giallo.demon.nul (Mark Wright)
Subject: Re: exit() with negative value
Message-Id: <3a27e253.12995049@194.159.73.10>
One joyful day (Thu, 30 Nov 2000 13:04:21 -0600 to be precise), Michael
Carman <mjcarman@home.com> decided that the Usenet community would
benefit from this remarkable comment:
>Is it practical to exit() a program with a negative value? Nothing in
>the docs suggest that it isn't, but when I try to capture the return
>value in the debugger for testing (e.g. $x = system('foo') / 256), I'm
>getting unsigned values back. That is, if 'foo' does an exit(1), $x = 1.
>But if 'foo' calls exit(-1), $x = 255.
I think the exit status is limited to 8 bits, so -1 == 255.
<...>
>For reference, my desire to use negative numbers for exit status is
>deliberate. I have a small program that searches files for a particular
>type of error and returns the number of errors found. If something goes
>wrong (like being unable to open a file) I want to abort with a
>meaningful status. Since zero would imply that everything checked out
>okay, and a positive number that the program worked and errors were
>found, I'm left with all things < 0.
You could just set aside, say, 16 values for internal error codes and
return the number of found errors + 16. This limits you to 255 - 16
found errors but makes it easy to check for internal errors (you could
do status -= 16 to give the number of found errors if 0 or positive,
error code if negative).
Just a thought.
Mark Wright
- mark@giallo.demon.nl
--
perl -e "print join' '=>map{$_ if s/(\w*)(.)ay/\U$2\E$1/}grep/^\w/=>reverse qw+ackerhay :o( erlpay :o| notheraay :o\ ustjay :o)+"
------------------------------
Date: 01 Dec 2000 17:47:12 +0000
From: nobull@mail.com
Subject: Re: flock under Solaris behaves strangely
Message-Id: <u9bsuwghjz.fsf@wcl-l.bham.ac.uk>
Ronny <ronald_f@my-deja.com> writes:
> my $fl=flock(X,LOCK_EX+LOCK_NB);
Because the function LOCK_EX() does not have a null prototype and
because + can be a unary operator this parses as:
my $fl=flock(X,LOCK_EX(LOCK_NB()));
Try:
my $fl=flock(X,LOCK_EX|LOCK_NB);
> unless ($fl)
> {
> print "flock failed: $fl\n";
> }
>
> Strangely, it never goes to 'flock failed'. For instance, when I start
> this program in one xterm window and, while it sleeps, start another
> copy in another window, flock returns 0 in *both* cases.
I do not understand what makes you say flock() returns 0 if it never
gets to 'flock failed'.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Thu, 30 Nov 2000 13:28:33 -0600
From: Michael Carman <mjcarman@home.com>
Subject: Re: force the browser to download instead of viewing the file.
Message-Id: <3A26AA61.AFD75E25@home.com>
Trung Mai wrote:
>
> I want to have a link in perl which will force the browser to
> download the file.
*sigh* FANPQ (Frequently Asked Non-Perl Question)
You can't do that. It's up to the browser (as configured by the user) to
decide what to do with a file. Let them handle it however they want.
-mjc
------------------------------
Date: 01 Dec 2000 18:40:56 +0000
From: nobull@mail.com
Subject: Re: force the browser to download instead of viewing the file.
Message-Id: <u93dg8gf2f.fsf@wcl-l.bham.ac.uk>
tmai@prince.carleton.ca (Trung Mai) writes:
> I want to have a link in perl which will force the browser to download
> the file.
You can't.
This has nothing to do with Perl.
This does not stop it being asked here a lot.
Please see numberous previous threads on the question which explain
how you can _advise_ not force the browser to download.
> please help??
Why should we if you won't help yourself?
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 01 Dec 2000 12:20:02 -0500
From: Ron Hartikka <ronh@iainc.com>
Subject: Re: Gotta weird problem...disappearing variables
Message-Id: <3A27DDC2.41C2A2F@iainc.com>
Did you find the problem? Just curious.
Daniel Bohling wrote:
> This one blows me away...
> the variable $body contains a news story on one line
> it is suddenly undefined on the next
> then reappears.
>
> sub index {
> my $self = shift;
> my $id = shift;
> my $title = shift;
> my $body = shift;
>
> print "id:\t$id\n" if $self->{print};
> print "title:\t$title\n" if $self->{print};
>
> print "Body:\t|$body|\n" if $self->{print};
> #^^^^^^^^^^^prints $body just fine
> unless ($body) { $self->die_gracefully("Missing body:$body"); }
> #^^^^^^^^^^^$body is undefined here?
> print "Body:\t|$body|\n" if $self->{print};
> #^^^^^^^^^^^prints $body just fine again
> $self->die_gracefully("Missing or incorrect id no.") unless ($id =~ /^\d+$/);
> $self->die_gracefully("Missing title.") unless $title;
> #^^^^^^^^^^^the two lines above work fine as well
------------------------------
Date: 1 Dec 2000 18:54:32 GMT
From: vek@pharmnl.ohout.pharmapartners.nl (Villy Kruse)
Subject: Re: how do i escape from perldoc
Message-Id: <slrn92fsv8.8pk.vek@pharmnl.ohout.pharmapartners.nl>
On Fri, 1 Dec 2000 09:02:08 -0500, Tad McClellan <tadmc@metronet.com> wrote:
> man more
>
The system in question probably uses "less" which requires you
to use the 'q' key to quit.
man less
Villy
------------------------------
Date: 01 Dec 2000 17:47:54 +0000
From: nobull@mail.com
Subject: Re: how to print file to browser?
Message-Id: <u9aeagghit.fsf@wcl-l.bham.ac.uk>
Jeff Zucker <jeff@vpservices.com> writes:
> nobull@mail.com wrote:
> >
> > "EM" <me@privacy.net> writes:
> >
> > > but i need to do this without first having to open the file into the server
> > > memory because it could be very big
> >
> > The code for this is not exactly complex but nonetheless personally
> > I'd use copy() from File::Copy rather than re-invent then wheel:
> >
> > use File::Copy 'copy';
> > copy('/home/me/file.zip',\*STDOUT);
>
> Hmm, that might not work without the optional third parameter of a
> buffer size.
Can't think why.
> "The default buffer size depends upon the file, but will generally be
> the whole file (up to 2Mb)".
For current technology 2Mb is a sensible default.
> Also, depending on OS, the OP might want to binmode before copy().
File::Copy does binmode() the filehandles, it also uses syswrite().
Can you say "belt and braces"?
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: 01 Dec 2000 17:48:21 +0000
From: nobull@mail.com
Subject: Re: how to print file to browser?
Message-Id: <u98zq0ghi2.fsf@wcl-l.bham.ac.uk>
Jeff Zucker <jeff@vpservices.com> writes:
> BUT, I just experimented and File::Copy does something funny with the
> order of printing. Nobull, will this really work for what the OP
> wanted? How would you print the Content header using this?
Hmmm... File::Copy::copy() forgets to flush it's filehandle - I'd call
this a bug. Trivial work-around:
{ local $| = 1; print ''; }
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 01 Dec 2000 18:08:00 GMT
From: "Guido Van Hoecke" <Guido@VanHoecke.org>
Subject: localtime problem under ActivePerl
Message-Id: <4ORV5.6834$lR2.274527@afrodite.telenet-ops.be>
There seems to be a problem with localtime and gmtime in
ActiveState's perl, v5.6.0 built for MSWin32-x86-multi-thread.
My timezone is MET which is the same as GMT+1,
so localtime should be one hour later as gmtime.
But it's not :
print
'timezone = ' . $ENV{TZ} . '\n' .
'localtime = ' . localtime . '\n' .
'gmtime = ' . gmtime . '\n';
prints
timezone = MET
localtime = Fri Dec 1 17:38:15 2000
gmtime = Fri Dec 1 17:38:15 2000
where it is really 18:38 local time.
On the other hand if I display the time of
a recently created file with both localtime and gmtime,
it shows the correct time of the file,
but gmtime also specifies the localtime :
`dir loctim.pl > dir.txt`;
($seconds, $minutes, $hours, ) = (localtime($mtime))[0..2];
printf "File dir.txt was created at localtime %02d:%02d:%02d\n",
$hours, $minutes, $seconds;
($seconds, $minutes, $hours, ) = (gmtime($mtime))[0..2];
printf "File dir.txt was created at gmtime %02d:%02d:%02d\n",
$hours, $minutes, $seconds;
prints :
File dir.txt was created at localtime 18:38:15
File dir.txt was created at gmtime 18:38:15
Does anyone experience the same problems,
or is there a workaround or fix for this problem.
Any suggestions are most welcome,
Guido.
------------------------------
Date: Fri, 01 Dec 2000 16:24:55 GMT
From: Mike Cameron <mcameron@mirusweb.com>
Subject: Re: Most efficient way of getting a random row from db query?
Message-Id: <3A27DEE4.A1DC1828@mirusweb.com>
Sorry, I guess if I had thought more I could have answered my own question
with Benchmark. Here are the results:
Benchmark: timing 10000 iterations of array, hash...
array: 33 wallclock secs (12.79 usr + 2.93 sys = 15.72 CPU) @ 636.13/s
(n=10000)
hash: 34 wallclock secs
(12.28 usr + 2.49 sys = 14.77 CPU) @ 677.05/s (n=10000)
So I guess if I need the hash that is not too large a price to pay
------------------------------
Date: Fri, 1 Dec 2000 10:12:41 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Need a web-based real-time option quote provider...
Message-Id: <slrn92ffv9.99d.tadmc@magna.metronet.com>
David Sisk <davesisk@ipass.net> wrote:
>OK, perhaps I didn't word this succintly enough for folks who can only
>perform literal interpretation,
That was not the problem.
The problem was that we discuss Perl programming here. If you
have a question about programming in Perl, this is the place
to ask.
If you are searching for something, this is not the place.
A "search engine" can be used for searching.
>so let me re-phrase the question:
Rephrasing an off-topic question does not make it on-topic.
What is your Perl question?
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 01 Dec 2000 17:53:13 GMT
From: fhinchey@my-deja.com
Subject: need regular expression guru!
Message-Id: <908oi7$hle$1@nnrp1.deja.com>
I'm trying to do a find and substituion in a text file. The text file
constains tab delimted columns representing an article. the first field
contains the articles title, the second the body text. Within the body
I have created a special tag "<left></left>" to indicate an image to be
left aligned with the body text. (I have similar tags for right and
center aligning).
I need a regular expression to find the instances of the "left" tag,
and replace them with HTML table text, keeping the stuff between the
tags (i.e. the image name). I've been fooling around with some code but
haven't had much luck. Any help would be much appreciated. Thanks!
-Frank
$file1 = 'article.txt';
open(INFO, $file1) || die "can't open $file1: $!";
$leftinsert_start = "<Table cellpadding=4 cellspacing=0 border=0
align=left><tr><td>";
$leftinsert_end = "</td></tr></Table>";
while ($entry=<INFO>) {
($title, $body) = split ("\t", $entry);
$body =~ s/ ( [<left>] .+ [<\/left>] ) / ($leftinsert_start) \1
($leftinsert_end) /g;
}
print (qq{Content-type: text/html\n\n<html>
$body
});
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: 1 Dec 2000 16:32:41 GMT
From: trammell@nitz.hep.umn.edu (John J. Trammell)
Subject: Re: next if length < 80; # why can't Perl be made to recognize this?
Message-Id: <slrn92eqc8.iv6.trammell@nitz.hep.umn.edu>
On Fri, 1 Dec 2000 04:35:54 -0800, jnpool <cwfuchs@sfu.ca> wrote:
>Hello Perl savants,
>
>I noticed this statement in the 3rd edition of Programming Perl (page 97)
>and
>it wasn't obvious to me as to why the language couldn't easily be made to
>accomodate this statement.
>
>Is there more insight on this to help out my intuition?
Great question. What clarified this for me was the examples
further down the page; they're all tricks to disambiguate the
"<", so that length() doesn't think it's the start of an
argument.
As for why the language can't be made to accomodate this, I
don't have enough mana to answer your question. Any takers?
--
John J. Trammell
johntrammell@yahoo.com
------------------------------
Date: 01 Dec 2000 09:49:00 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: next if length < 80; # why can't Perl be made to recognize this?
Message-Id: <m3lmu02lcj.fsf@dhcp11-177.support.tivoli.com>
Marco Natoni <foo@bar.va> writes:
> Chris,
>
> jnpool wrote:
> > I noticed this statement in the 3rd edition of Programming Perl
> > (page 97) and it wasn't obvious to me as to why the language
> > couldn't easily be made to accomodate this statement. Is there
> > more insight on this to help out my intuition?
>
> /lenght/ is a sub that calculates the... lenght of a given string. If
> no scalars are passed as parameter, the /$_/ variable is evaluated. The
> obvious conclusion in this case is that a previous statement has
> produced a significant result in /$_/.
The point is:
% perl -e '{next if length < 80}'
Warning: Use of "length" without parens is ambiguous at -e line 1.
Unterminated <> operator at -e line 1.
Changing it to:
% perl -e '{next if length() < 80}'
works fine.
It does seem like it should be possible to disambiguate this
particular situation, but it is probably a case of looking too far
ahead in the stream.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 01 Dec 2000 18:33:40 +0000
From: nobull@mail.com
Subject: Re: next if length < 80; # why can't Perl be made to recognize this?
Message-Id: <u966l4gfej.fsf@wcl-l.bham.ac.uk>
"jnpool" <cwfuchs@sfu.ca> writes:
> I noticed this statement in the 3rd edition of Programming Perl
> (page 97) and it wasn't obvious to me as to why the language
> couldn't easily be made to accomodate this statement.
Unless you've ever tried to write a compiler interpreter it wouldn't
be.
Compilers for programing languages like to use simple parsers which
don't relay on unlimited look ahead. That is to say that I should be
able to stick my finger anywhere in the code and read the file up to
that point and know what it means.
Now consider
"next if length < 80;"
If I stick my finger after the < you cannot tell if it is the opening
of a <FILE> operator or a numerical comparison.
Perl guesses wrong.
Actually the Perl interpreter does sometimes use limited lookahead so
perhaps it could be made to DWIM in this case but it's probably not
worth it.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Fri, 1 Dec 2000 11:15:01 -0500
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: next if length < 80; # why can't Perl be made to recognize this?
Message-Id: <Pine.GSO.4.21.0012011110540.5826-100000@crusoe.crusoe.net>
[posted & mailed]
On Dec 1, jnpool said:
>I noticed this statement in the 3rd edition of Programming Perl (page
>97) and it wasn't obvious to me as to why the language couldn't easily
>be made to accomodate this statement.
When Perl sees
length <
it thinks you're either reading from a filehandle, or using the <...> form
of the glob() function.
japhy% perl -w
print if length < 20; # <-- Perl wanted <20> or < 20 >
Warning: Use of "length" without parens is ambiguous at - line 1.
Unterminated <> operator at - line 1.
--
Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
CPAN - #1 Perl Resource (my id: PINYAN) http://search.cpan.org/
PerlMonks - An Online Perl Community http://www.perlmonks.com/
The Perl Archive - Articles, Forums, etc. http://www.perlarchive.com/
------------------------------
Date: Fri, 01 Dec 2000 17:29:29 +0100
From: Georg Woeste <gbw@theo1.physik.uni-stuttgart.de>
Subject: Nice Perl 1-liner
Message-Id: <3A27D1E9.C029EF2E@theo1.physik.uni-stuttgart.de>
Hi,
I am using this nice Perl program, which fits in one line.
Perhaps it is well known, then forget this message, but I
found it very appealing, to wirte a non trivial program in
one line without using any variables!
shell> perl -e "print grep {(/BEGIN/) .. (/END/)} <>;" file.text
This little program prints from a text file only the lines
from the line which contains BEGIN, to the line which
contains END.
Bye,
G.
------------------------------
Date: Fri, 01 Dec 2000 16:42:37 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Nice Perl 1-liner
Message-Id: <slrn92fl9e.iia.rgarciasuarez@rafael.kazibao.net>
Georg Woeste wrote in comp.lang.perl.misc:
> Hi,
>
> I am using this nice Perl program, which fits in one line.
> Perhaps it is well known, then forget this message, but I
> found it very appealing, to wirte a non trivial program in
> one line without using any variables!
>
> shell> perl -e "print grep {(/BEGIN/) .. (/END/)} <>;" file.text
Yes, but it can be made shorter through the use of the -n switch
(described in perlrun):
perl -ne 'print if /BEGIN/../END/' file.text
--
# Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/
------------------------------
Date: Fri, 01 Dec 2000 16:49:31 GMT
From: simbean@my-deja.com
Subject: Re: Nice Perl 1-liner
Message-Id: <908kqn$eb4$1@nnrp1.deja.com>
> shell> perl -e "print grep {(/BEGIN/) .. (/END/)} <>;" file.text
Isn't this a command you could as well issue directly in the linux (or
unix) shell?
I am not into it, but I'll bet you could get the same output if you put
that in bash ...
Ciao,
SimBean.
Sent via Deja.com http://www.deja.com/
Before you buy.
------------------------------
Date: Fri, 1 Dec 2000 11:32:40 -0500
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Nice Perl 1-liner
Message-Id: <slrn92fkl8.9j9.tadmc@magna.metronet.com>
Georg Woeste <gbw@theo1.physik.uni-stuttgart.de> wrote:
>
>I am using this nice Perl program, which fits in one line.
>Perhaps it is well known, then forget this message, but I
>found it very appealing, to wirte a non trivial program in
>one line without using any variables!
^^^^^^^^^^^^^
>shell> perl -e "print grep {(/BEGIN/) .. (/END/)} <>;" file.text
That code _does_ use variables.
Just because you do not see any does not mean that none are being used.
But it is nice to not have to type the variable's name anywhere.
grep() uses the $_ variable.
But yes, Perl one-liners are slick :-)
--
Tad McClellan SGML consulting
tadmc@metronet.com Perl programming
Fort Worth, Texas
------------------------------
Date: 01 Dec 2000 11:16:02 -0600
From: Ren Maddox <ren.maddox@tivoli.com>
Subject: Re: Nice Perl 1-liner
Message-Id: <m3wvdk12r1.fsf@dhcp11-177.support.tivoli.com>
simbean@my-deja.com writes:
> > shell> perl -e "print grep {(/BEGIN/) .. (/END/)} <>;" file.text
> Isn't this a command you could as well issue directly in the linux (or
> unix) shell?
>
> I am not into it, but I'll bet you could get the same output if you put
> that in bash ...
Nope. This is taking advantage of Perl's flip-flop operator "..",
which is not available in bash.
--
Ren Maddox
ren@tivoli.com
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 V9 Issue 5009
**************************************