[19947] in Perl-Users-Digest
Perl-Users Digest, Issue: 2142 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Nov 16 09:05:49 2001
Date: Fri, 16 Nov 2001 06:05:10 -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: <1005919509-v10-i2142@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Fri, 16 Nov 2001 Volume: 10 Number: 2142
Today's topics:
Re: a good perl editor - know one?? <tim@vegeta.ath.cx>
Re: ActiveState Install to Win NT Fails henry.mcguinness@dphpc.ox.ac.uk
array ref. and splice <lhalie@areyna.etrade.nl>
Re: array ref. and splice (Anno Siegel)
Re: Best language for low IQ programmers? <pulse@nyc.rr.com>
Can someone tell me what i am doing wrong. Unix sockets <thechile@ntlworld.com>
CGI-Mysql question <stevoarnold@yahoo.co.uk>
Re: CGI-Mysql question <bernard.el-hagin@lido-tech.net>
Extracting number from an string <robert_loui@yahoo.com>
Re: Extracting number from an string <bernard.el-hagin@lido-tech.net>
Re: Find files?? <mgjv@tradingpost.com.au>
Re: IO::Socket write fail <goldbb2@earthlink.net>
no FETCH in IO::String (Simon)
Re: OT: URL To Text (url2txt) <simonmcc@nortelnetworks.com>
Personal ad script? <kfisher@nyc.rr.com>
Re: Pretty-printing CSS (Eric Bohlman)
Re: Regular Expression Madness <bernard.el-hagin@lido-tech.net>
relinquishing group permissions <sh@planetquake.com>
Re: self-printing perl program <joe+usenet@sunstarsys.com>
Sorting observation (Mark Jason Dominus)
Re: specific array items, loop iterator <bernard.el-hagin@lido-tech.net>
Re: specific array items, loop iterator nobull@mail.com
Re: specific array items, loop iterator <bernard.el-hagin@lido-tech.net>
Re: URL To Text (url2txt) <comdog@panix.com>
win32::netadmin problem <wschow@Comp.HKBU.Edu.HK>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 16 Nov 2001 12:37:36 GMT
From: Tim Hammerquist <tim@vegeta.ath.cx>
Subject: Re: a good perl editor - know one??
Message-Id: <slrn9va2kb.13s.tim@vegeta.ath.cx>
Paul Spitalny <pauls@cascadelinear.com> wrote:
> Anyone know of a text editor (For a windows platform) specifically
> targeted to PERL that has features like color coding of key works and
> other nice development aids?
gvim.
Available on Win32.
More customizable than most.
(Yes, does syntax coloring/highlighting.)
Can be compiled with and scripted using Perl.
^^^ ^^ ^^^^^^^^ ^^^^ ^^^ ^^^^^^^^ ^^^^^ ^^^^^
How much more "targeted to [Perl]" can you get?
(Technically, (g)vim can be compiled with any combination of Perl,
Python, Ruby, and/or Tcl.) I didn't see Lisp on the list, so Emacs'ers
may be out of luck. ;)
HTH
Tim Hammerquist
--
Emacs is a nice OS - but it lacks a good text editor.
That's why I am using Vim.
-- Anonymous
------------------------------
Date: Fri, 16 Nov 2001 14:01:37 +0000 (UTC)
From: henry.mcguinness@dphpc.ox.ac.uk
Subject: Re: ActiveState Install to Win NT Fails
Message-Id: <9t3681$7q7$1@news.ox.ac.uk>
which versions fail for you?
ActivePerl-5_6_1_625-TRIAL3-etc..zip works well for me, but 629 / 630 failed to install.
>> >> I haven't been able to get ActiveState Perl (or Python either,
>> >> for that matter) to install on Windows NT for several builds
>> >> now.
------------------------------
Date: Fri, 16 Nov 2001 12:33:32 +0100
From: "Louis Halie" <lhalie@areyna.etrade.nl>
Subject: array ref. and splice
Message-Id: <9t2tgu$al7$1@azure.nl.gxn.net>
Hi,
i have a simple question, but it puzzles me.
In the Perl programming book of Larry Wall and co, they state that in order
to change the size of an array, a subroutine needs a reference of that array
as an argument (or typeglob). (perl 5 , second edition page 116)
But if i look at the function splice, which also changes the size of an
array, its first argument is just an array.
Can somebody perhaps mail me how this is possible?
thanks, Louis Halie
lhalie@areyna.etrade.nl
------------------------------
Date: 16 Nov 2001 12:33:48 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: array ref. and splice
Message-Id: <9t313c$i3i$1@mamenchi.zrz.TU-Berlin.DE>
According to Louis Halie <lhalie@areyna.etrade.nl>:
> Hi,
>
> i have a simple question, but it puzzles me.
> In the Perl programming book of Larry Wall and co, they state that in order
> to change the size of an array, a subroutine needs a reference of that array
> as an argument (or typeglob). (perl 5 , second edition page 116)
>
> But if i look at the function splice, which also changes the size of an
> array, its first argument is just an array.
Yes, but it's passed to the function as a reference. Perl knows the
builtins and deals with the parameters accordingly. This happens at
compile time.
When you call a perl sub as "foo( @array)", what the sub sees is
the array @_ which contains the same elements as @array at the time
of call. The array structure @array however is not accessible from
the sub.
You can use prototypes to make a user-defined sub work like splice:
sub my_splice (\@$$@) { ...
tells the compiler that the first argument of my_splice must be
an array, and to hand down a *reference* to that array to the sub.
(See perldoc perlsub for more.) So the claim of the book remains
true: To change the size of an array, a sub needs a reference to
that array.
Anno
------------------------------
Date: Fri, 16 Nov 2001 05:55:45 GMT
From: no <pulse@nyc.rr.com>
Subject: Re: Best language for low IQ programmers?
Message-Id: <3BF4AA25.A72DC4BB@nyc.rr.com>
Frances Klein wrote:
>
> Java? I love to drink java at the end of a good meal. Can anyone recommend
> a nice kosher restaurant in the southern half of Manhattan, full of
> atmosphere and lots of choices beyond the usual?
KATZ's
------------------------------
Date: Fri, 16 Nov 2001 11:08:07 -0000
From: "Scott" <thechile@ntlworld.com>
Subject: Can someone tell me what i am doing wrong. Unix sockets
Message-Id: <pu6J7.457$yV.205257@news6-win.server.ntlworld.com>
I am trying to get the below code to print out the data sent to the
listening port and send a response back to the browser but I can get the
damn thing working
I open up a listening socket here on port 80 and then this code. It listens
and prints out the where the connection came from bit but I can't get it to
print the HTTP 1.0 GET request that the browser should have sent, nor can I
get the printing to the client stream to work so the browser reports a 404.
while () {
$paddr = accept(Client,Server);
my $buf = <$paddr>;
my($port,$iaddr) = sockaddr_in($paddr);
my $name = gethostbyaddr($iaddr,AF_INET);
print "connection from $name [",inet_ntoa($iaddr), "] at port $port\n";
print "$buf\n\n";
print Client "HTTP/1.0 404\n\nContent-type: text/html\n\nBad command";
}
Any help or pointers as to what i'm doing wrong mucho appreciated.
Scott
------------------------------
Date: Fri, 16 Nov 2001 10:12:37 -0000
From: "Steve" <stevoarnold@yahoo.co.uk>
Subject: CGI-Mysql question
Message-Id: <9t2p1f$e3s$1@pheidippides.axion.bt.co.uk>
I am hoping someone on this group has some advice, if not, you may like to
point me in the right direction. My topic is:
I have created a cgi script using perl and it connects to a mysql database
on my server to retrieve data and print it to the screen in various manners.
The script needs to run 2 sql queries and at the moment it follows this type
of pattern:
connect to db;
run query1;
close db;
print results1;
connect to db;
run query2;
close db;
print results2;
is this the best way to do it? should i do all the processing while the db
is open so as to open it only once or are there any performance issues. a
shorter way of coding the script would be to adopt the following pattern:
connect to db;
run query1;
print results1;
run query2;
print results2;
close database;
would this method be acceptable or is there a danger in having the database
connected for longer?
many thanks,
Steve A
------------------------------
Date: Fri, 16 Nov 2001 10:25:01 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: CGI-Mysql question
Message-Id: <slrn9v9tc6.3il.bernard.el-hagin@gdndev25.lido-tech>
On Fri, 16 Nov 2001 10:12:37 -0000, Steve <stevoarnold@yahoo.co.uk> wrote:
> I am hoping someone on this group has some advice, if not, you may like to
> point me in the right direction. My topic is:
> I have created a cgi script using perl and it connects to a mysql database
> on my server to retrieve data and print it to the screen in various manners.
> The script needs to run 2 sql queries and at the moment it follows this type
> of pattern:
>
> connect to db;
> run query1;
> close db;
> print results1;
> connect to db;
> run query2;
> close db;
> print results2;
>
> is this the best way to do it?
How is this a Perl question? And don't say "But my CGI script
is written in Perl!" coz that's irrelevant.
Cheers,
Bernard
------------------------------
Date: Fri, 16 Nov 2001 14:21:08 +0100
From: "Robert" <robert_loui@yahoo.com>
Subject: Extracting number from an string
Message-Id: <9t32k0$k8g$1@newstoo.ericsson.se>
Hi
I was wondering if some one please could tell me how i can extract the
number from an input string.
If i have an string like: $String ="x4.5.6"
i want an output like :$FirstNr= 4; $SecNr= 5; $ThirdNr=6;
Or If i have an string like: $String ="x4.5"
i want an output like :$FirstNr= 4; $SecNr= 5;
Or If i have an string like: $String ="x4"
i want an output like :$FirstNr= 4;
ELSE if it was some thing else $FirstNr= 0;
Thankyou Robert
------------------------------
Date: Fri, 16 Nov 2001 13:29:28 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: Extracting number from an string
Message-Id: <slrn9va85u.3il.bernard.el-hagin@gdndev25.lido-tech>
On Fri, 16 Nov 2001 14:21:08 +0100, Robert <robert_loui@yahoo.com> wrote:
> Hi
>
> I was wondering if some one please could tell me how i can extract the
> number from an input string.
> If i have an string like: $String ="x4.5.6"
> i want an output like :$FirstNr= 4; $SecNr= 5; $ThirdNr=6;
>
> Or If i have an string like: $String ="x4.5"
> i want an output like :$FirstNr= 4; $SecNr= 5;
>
> Or If i have an string like: $String ="x4"
> i want an output like :$FirstNr= 4;
>
> ELSE if it was some thing else $FirstNr= 0;
my ( @numbers ) = $String =~ m/(\d+)/g;
local $" = '][';
print "[@numbers]";
This will give you an array of all the numbers in the string if
your definition of a number is one or more digits next to each other.
You can't put the numbers in separate scalars unless you know
ahead of time how many there will be. So in the second example
above you could do:
my ( $FirstNr, $SecNr ) = $String =~ m/(\d+)/g;
If you knew ahead of time that there would be two matches.
Also see the FAQ on matching numbers to make these
solutions more robust.
Cheers,
Bernard
------------------------------
Date: Fri, 16 Nov 2001 18:33:05 +1100
From: Martien Verbruggen <mgjv@tradingpost.com.au>
Subject: Re: Find files??
Message-Id: <slrn9v9g9h.3j7.mgjv@martien.heliotrope.home>
On 15 Nov 2001 14:49:04 -0800,
Donavon <djlerman@yahoo.com> wrote:
> How can I do the following on a win32 system? I found a reference to
> a module called 'FILE::FIND' but a could not find it anywhere...
It's called File::Find (case matters in Perl), and it comes with
Perl. It should be installed with your Perl, and if you use the
ActiveState installation, there will be HTML documentation. Otherwise
perldoc File::Find
should get you the documentation.
> #!/perl/bin
#!/usr/bin/perl -w
use strict;
The path isn't that important, but the perl -w bit is. So is the use
of the strict pragma.
> @files = ("file1.txt",
> "file2.doc",
> ...,
> ...,)
>
> foreach $i (@files) {
> # find $i on drive R:\
> # if ("found") {
> # print "file location is: ". ?????????;
> # } else {
> # print "file $i not found\n";
> # }
> }
So.. You have a list of files, and you are trying to find them in a
certain directory (in this case "r:/")? What I would do, is create a
hash with those file names, and then use File::Find to mark the ones
in the hash as found. Since multiple copies could exist, I'd keep an
array of found instances:
#!/usr/local/bin/perl -w
use strict;
use File::Find;
# The files to find
my @files = qw(
perl.pod
perlfaq.pod
perlfunc.pod
);
# Where to start searching
my $root = "/opt/perl/lib";
# Create a hash with the file names as keys, and undef as the values
my %found;
@found{@files} = ();
# This is what does the work for each file
sub wanted
{
# We're only interested in plain files
return unless -f $File::Find::name;
# If the current file name is in the list, add the path to it to
# the array
push @{$found{$_}}, $File::Find::name if exists $found{$_};
}
find({wanted => \&wanted}, $root);
# Now inspect the %found hash for keys that have values, and print
# them
while (my ($file, $array_ref) = each %found)
{
next unless $array_ref;
print "Found instances of '$file' in\n";
print "\t$_\n" for @{$array_ref};
print "\n";
}
If you just want to print the files when you find them:
#!/usr/local/bin/perl -w
use strict;
use File::Find;
my @files = qw(
perl.pod
perlfaq.pod
perlfunc.pod
);
my $root = "/opt/perl/lib";
my %found;
@found{@files} = ();
sub wanted
{
return unless -f $File::Find::name;
print "Found $File::Find::name\n" if exists $found{$_};
}
find({wanted => \&wanted}, $root);
Documentation you might want to read: File::Find, perldata (for the
hash slice), perlop (for qw//), perlfunc (for -f, push, exists and
each). You probably also want to have a look at perlref, perllol and
perldsc to read up about references and how they are used in Perl to
build complex data structures.
Martien
--
|
Martien Verbruggen | If at first you don't succeed, try
Trading Post Australia Pty Ltd | again. Then quit; there's no use
| being a damn fool about it.
------------------------------
Date: Fri, 16 Nov 2001 00:42:48 -0500
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: IO::Socket write fail
Message-Id: <3BF4A758.2EC75F5@earthlink.net>
Mark wrote:
>
> Dear people,
>
> I am having a problem with writing to the client of a UNIX domain
> socket. Ok, lets say I have a daemon, like this:
>
> $sock = IO::Socket::UNIX -> new (Local => UXSOCKADDR,
> Type => SOCK_STREAM,
> Listen => 10) || exit 1;
>
> while ($client = $sock -> accept ()) {
> print STDERR "Got new connection! ($client)\n";
> print $client "Hi, how are you?\n";
> while (defined ($from_client = <$client>)) {
> chomp ($from_client);
> print $client ">$from_client<\n";
> }
> }
>
> This works, but is incredibly crude. The program waits for input from
> the client, and only then processes possible commands. Ok, so I did
> this:
>
> $sock = IO::Socket::UNIX -> new (Local => UXSOCKADDR,
> Type => SOCK_STREAM,
> Listen => 10) || exit 1;
> $sel = IO::Select -> new ($sock);
>
> for (;;) {
> foreach $client ($sel -> can_read (0.1)) {
> if ($client == $sock) {
> $client = $sock -> accept ();
> $sel -> add ($client);
> nonblock ($client);
> } else {
> my $input;
> my $got = $client -> recv ($input, POSIX::BUFSIZ, 0);
> unless (defined ($got) && length ($input)) {
> disconnect ($client);
> next;
> }
> $inbuffer{$client} .= $input;
> push (@{$ready{$client}}, $2)
> while ($inbuffer{$client} =~
> s/(<command>((.|\n)*?)<\/command>\n)//m);
> }
> }
> foreach $client (keys %ready) {
> while ($request = shift @{$ready{$client}}) {
> print $client ......
> }
> delete $ready{$client};
> }
> }
>
> This snippet of code iterates over the possible clients, and starts
> processing commands until it receives a valid <command></command> pair
> from its clients. So far so good. Except that I can no longer write to
> the client! No matter what I do, the "print $client ......" does
> nothing. Absolutely nothing gets parsed to the client. I did a ktrace
> on it, and it reveals that no data of any kind gets parsed to the
> client.
As uri said, hashes stringify their keys, so keys %ready will return
strings, not socket objects.
You've already come up with one solution, but I have a few suggestions,
in addition to that.
If you are selecting on a socket for reading, then the first time you
call sysread or recv after select() indicates readyness for that socket,
it *will* succeed, *without* blocking. Thus, there's no need for
setting the socket to nonblocking.
There's a small possibility that when writing, the output buffer of the
socket could fill up. This would result in print() blocking [just as
reading could block] until the remote end reads sufficient data for the
local end to send more of its buffer. To prevent print from blocking, I
would suggest selecting on your sockets for writing, and keep your own
buffer, as necessary.
Lastly, you should remove your client socket from the IO::Select object
when you disconnect -- otherwise, you're wasting memory, and possibly
slowing your program down [I dunno by how much].
So here's how I would write *my* version of your code:
my $sock = ...;
my ($r,$w) = (IO::Socket->new($sock), IO::Socket->new);
my (%in, %out);
while( my ($rr, $ww) = IO::Socket->select($r,$w) ) {
foreach my $rrr ( @$rr ) {
if( $rrr == $sock ) {
my $client = $sock->accept
or warn("accept: $!"), $r->remove($sock), next;
$out{STDERR} .= "Got new connection! ($client)\n"
$out{$client} = "Hi, how are you?\n";
$w->add($client, "STDERR");
$r->add($client);
next;
}
sysread( $rrr, $in{$rrr}, POSIX::BUFSIZ, length $in{$rrr} );
or $r->remove($rrr), delete $in{$rrr}, next;
my $add_to_w = 0;
while( $in{$rrr} =~ s,^.*?<command>(.*?)</command>,,s ) {
$out{$rrr} .= ">$1<\n";
$add_to_w = 1;
}
$w->add($rrr) if $add_to_w;
}
foreach my $www (@$ww) {
unless( my $cnt = syswrite( $www, $out{$www} ) ) {
delete $out{$www}; $w->remove($www);
next;
}
substr( $out{$www}, 0, $cnt, "" );
if( !length $out{$www} && !$ready{$www} ) {
$w->remove($www), delete $out{$www};
}
}
}
You might notice that I don't disconnect at any point... That's ok,
since perl keeps refcounts of it's objects. When all references to a
socket handle are removed, the socket is closed.
NB: This code is untested.
--
Klein bottle for rent - inquire within.
------------------------------
Date: 16 Nov 2001 02:03:49 -0800
From: thefairey@hotmail.com (Simon)
Subject: no FETCH in IO::String
Message-Id: <ba3a18e0.0111160203.34b4c449@posting.google.com>
I'm using the IO::String module and when I try and use Data::Dumper
and use Dumper() to output the class object it complains there is no
FETCH method in IO::String. I'm wondering what the best way round this
is? I'm also wondering why like some of the other IO based modules
(FileHandle etc.) it hasn't inherited a FETCH method but I tried to
track down where FileHandle inherited it from and got toatlly lost!!
Any suggestions welcome.
Si
------------------------------
Date: Fri, 16 Nov 2001 10:03:12 -0000
From: "simonmcc" <simonmcc@nortelnetworks.com>
Subject: Re: OT: URL To Text (url2txt)
Message-Id: <9t2o6g$15q8k8$1@ID-101235.news.dfncis.de>
"Anand Ramamurthy" <anand_ramamurthy@yahoo.com> wrote in message
news:761041e6.0111151235.71391844@posting.google.com...
=> I am looking for a script for converting an URL to TEXT,
=> which takes care of tables also.
=>
=> Many have suggested using lynx. I am not sure if lynx is
=> available for HP-UX, SunOS, Windows (NT & 2000). Please let
=> me know if I can get download lynx for above platform. If yes,
=> from which site.
heard of google? http://www.google.com/search?q=lynx+&btnG=Google+Search
yeilds <http://lynx.browser.org/> as a first result.
http://lynx.browser.org/ then states :
Lynx 2.8.3 runs on Un*x, VMS, Windows 95/98/NT but not 3.1 or 3.11, on DOS
(386 or higher) and OS/2 EMX
also see http://hpux.connect.org.uk/hppd/hpux/Networking/WWW/lynx-2.8.3/ for
HP-UX binaries
=> Does anyne have a PERL script which does url2txt (handling
=> tables also).
again from google (google is your friend - remember that)
http://cgi.w3.org/cgi-bin/html2txt
also search google for html2text
HTH
=> Thank you.
That's alright. Just be careful about asking non-perl questions here. Most
people don't like that.
------------------------------
Date: Fri, 16 Nov 2001 05:48:52 GMT
From: "1" <kfisher@nyc.rr.com>
Subject: Personal ad script?
Message-Id: <8N1J7.88877$XA5.15352122@typhoon.nyc.rr.com>
Hello. I am looking for a personal ad script to power a free matchmaking
website, of a quality approaching match.com.
I have found a few scripts on the web for free, and many for a fee (Such as
e-personals). I have not found a good free one yet, and though I really
don't mind paying for a script, I am not sure which ones are good. (I have a
very small budget, so I would much prefer a free one or a try and buy.)
Does anyone have or know where I can find a good, customizable
personals/classified ad script? Php or Perl are what I need.
I will be back to this board to check, but if you would e-mail me a reply, I
would really appreciate it.
Thanks,
Ken
kenneth@fisher-net.com
------------------------------
Date: 16 Nov 2001 07:28:52 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Pretty-printing CSS
Message-Id: <9t2f7k$nip$2@bob.news.rcn.net>
David Wall <darkon@one.net> wrote:
> Actually I think I should just write a parser for CSS (unless there's
> already one), but I'm still slacking off on learning more about parsers.
CSS::SAC.
------------------------------
Date: Fri, 16 Nov 2001 06:48:19 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: Regular Expression Madness
Message-Id: <slrn9v9glt.3il.bernard.el-hagin@gdndev25.lido-tech>
On 15 Nov 2001 10:37:19 -0800, Malcolm Dew-Jones <yf110@vtn1.victoria.tc.ca>
wrote:
> Bernard El-Hagin (bernard.el-hagin@lido-tech.net) wrote:
>: On Thu, 15 Nov 2001 10:03:51 GMT, Tyler Cruz <tylercruz@home.com> wrote:
>: > I'm a Perl newbie so I'm not sure about this, but other than switching to
>: >=~, doesn't the RegEx parameters have to be in parethethis?:
>: >
>: > $text =~ s/<%(.*?)%>//sg;
>
>
>: The parentheses are only necessary if you want to use what ".*?"
>: captures. It will be stored in the special $1 variable if the
>: ()s are present. Read more in perlre.
>
> Not entirely true, the ()'s are also important for logical grouping.
Entirely true in the example above. The OP asked about a specific
regex and I gave a specific response. I then pointed the OP at
the documentation using the words "read more" to suggest there's
more to read. When a beginner asks a specific question you shouldn't
complicate matters. Just give a specific answer and a pointer to more
information.
Cheers,
Bernard
------------------------------
Date: Fri, 16 Nov 2001 06:23:11 GMT
From: "Sean Hamilton" <sh@planetquake.com>
Subject: relinquishing group permissions
Message-Id: <jh2J7.9081$c4.1481599@news0.telusplanet.net>
Greetings,
I have a script running setuid. As soon as the user authenticates, I'm
calling getpwnam and setting $<, $(, $>, and $) accordingly. However, the
script still maintains the group permissions of its caller (Apache), and I
think it may be possible for it to change its id back up to root or
whatever.
Anybody have any pointers here?
TiA,
sh
------------------------------
Date: 16 Nov 2001 00:51:43 -0500
From: Joe Schaefer <joe+usenet@sunstarsys.com>
Subject: Re: self-printing perl program
Message-Id: <m33d3f2rgw.fsf@mumonkan.sunstarsys.com>
c-blair@uiuc.edu (Charles Blair) writes:
> I'm sure this must be an FAQ, but I couldn't find it in admittedly
> cursory searching. What is the shortest perl program that will
> print itself?
google(perl quine) turned up lots of interesting stuff.
> I suspect something can be done with eval, but haven't figured out
> what.
Here's one way:
% perl -wle 'eval for q{print qq/eval for q{$_}/}'
eval for q{print qq/eval for q{$_}/}
%
--
Joe Schaefer "The worst thing you can do to a man is to tell him he can have
what he wants."
--Mark Twain
------------------------------
Date: Fri, 16 Nov 2001 08:56:33 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Sorting observation
Message-Id: <3bf4d4c0.2009$1e2@news.op.net>
Keywords: Brunswick, bater, chromatograph, turnpike
C's 'qsort' function and Perl's built-in 'sort' operator require a
three-way comparator function, which compares two items and returns
negative if they are in the right order, positive if they are in the
wrong order, and zero if it doesn't matter. But this isn't the only
possible interface to a generic sort operator.
Other languages, such as J, SML, and Common Lisp, have sort operators
that use a two-way comparator. The comparison function simply says
whether one item precedes another. To give the idea of how this might
work, here's a sample implementation in Perl:
# quicksort
sub qsort ($@) {
my $cmp = shift;
if (@_ == 0) { return () }
else {
local $b = shift;
my ($small, $large);
local $a;
for $a (@_) {
push @{&$cmp ? $small : $large}, $a;
}
return (qsort($cmp, @$small), $b, qsort($cmp, @$large));
}
}
Then you no longer need Perl's special three-way '<=>' and 'cmp'
operators, because you just use the regular '<' and 'lt' operators
instead:
print qsort sub { $a < $b }, 1,4,2,8,5,7;
# result is 1,2,4,5,7,8
@words = qw(one four two eight five seven);
print qsort sub { $a lt $b }, @words; # ascending order
# result is eight,five,four,one,seven,two
Similarly, if you want a reversed sort, you do not need to use the
slightly strange trick of exchanging $a and $b. If you know that
trick, you can still use it, but it's more perspicacious to just use
the opposite comparison operator. For ascending order, use < so that
each element is < the next one;; for
descending order use > so that each element is > the next one:
print qsort sub { $a > $b }, 1,4,2,8,5,7; # 8,7,5,4,2,1
print qsort sub { $a gt $b }, @words; # two,seven...five,eight
The reason I bring this up is I've just discovered the following trick:
print qsort sub { $a == $b }, 1,4,2,8,5,4,7,1,4,2,8,5,3,7;
With the qsort implementation above, the output is 11444228855773.
Items in appear in the same order in the input as in the output, (for
example, the first 4 precedes the first 8 in the input, so the 4s
precede the 8s in the output) but with equal items grouped. There is
no analog of this with three-way comparators.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Fri, 16 Nov 2001 06:54:08 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: specific array items, loop iterator
Message-Id: <slrn9v9h0r.3il.bernard.el-hagin@gdndev25.lido-tech>
On Thu, 15 Nov 2001 16:12:19 +0000, Paul Boardman <peb@bms.umist.ac.uk> wrote:
> Bernard El-Hagin wrote:
>>
>> On Thu, 15 Nov 2001 15:18:58 +0100, Philip Newton
>> <pne-news-20011115@newton.digitalspace.net> wrote:
>> > On Thu, 15 Nov 2001 09:13:38 +0000 (UTC), Bernard El-Hagin
>> ><bernard.el-hagin@lido-tech.net> wrote:
>> >
>> >> On Thu, 15 Nov 2001 09:06:23 GMT, Sean Hamilton <sh@planetquake.com> wrote:
>> >> > 1. Is there a good way to extract only certain items from an array? For
>> >> > example, if I only want the inode of a file, I can use something like
>> >> >
>> >> > my $inode = (stat ($file))[1];
>> >> >
>> >> > However, what if I want the first and ninth item? Must I create a bunch of
>> >> > dummy variables?
>> >>
>> >> (stat($file))[1,9];
>> >
>> > That gives you the second and tenth item.
>>
>> $[ = 1;
>
> I would have thought that you would be one of the first people to hand
> out a scolding for the response you've just given, Bernard ;-)
Hehe, maybe I should have added a smiley. Anyway, Philip quite correctly
pointed out my mistake so what was I to do other than desperately try to
validate my erroneous answer using a deprecated special variable? Wouldn't
anybody? ;-)
Cheers,
Bernard
------------------------------
Date: 16 Nov 2001 00:40:04 -0800
From: nobull@mail.com
Subject: Re: specific array items, loop iterator
Message-Id: <4dafc536.0111160040.6e7458b5@posting.google.com>
"Sean Hamilton" <sh@planetquake.com> wrote in message news:<jALI7.5092$c4.957093@news0.telusplanet.net>...
> 2. I seem to recall reading somewhere about some variable which indicates
> the current index in a foreach (@array) loop. Which variable is this?
I seem to recall reading that thread too.
I think, however, it was in the context of a proposed addition for a
future version of Perl.
------------------------------
Date: Fri, 16 Nov 2001 08:45:52 +0000 (UTC)
From: Bernard El-Hagin <bernard.el-hagin@lido-tech.net>
Subject: Re: specific array items, loop iterator
Message-Id: <slrn9v9ni7.3il.bernard.el-hagin@gdndev25.lido-tech>
On 16 Nov 2001 00:40:04 -0800, nobull@mail.com <nobull@mail.com> wrote:
> "Sean Hamilton" <sh@planetquake.com> wrote in message news:<jALI7.5092$c4.957093@news0.telusplanet.net>...
>
>> 2. I seem to recall reading somewhere about some variable which indicates
>> the current index in a foreach (@array) loop. Which variable is this?
>
> I seem to recall reading that thread too.
>
> I think, however, it was in the context of a proposed addition for a
> future version of Perl.
I can confirm that. Anno suggested using $#.
Cheers,
Bernard
------------------------------
Date: Fri, 16 Nov 2001 02:59:38 -0500
From: brian d foy <comdog@panix.com>
Subject: Re: URL To Text (url2txt)
Message-Id: <comdog-CA93AE.02593816112001@news.panix.com>
In article <761041e6.0111151235.71391844@posting.google.com>,
anand_ramamurthy@yahoo.com (Anand Ramamurthy) wrote:
> Does anyne have a PERL script which does url2txt (handling
> tables also).
perhaps you mean html2txt, since URLs are a different beast.
--
brian d foy <comdog@panix.com> - Perl services for hire
CGI Meta FAQ - http://www.perl.org/CGI_MetaFAQ.html
Troubleshooting CGI scripts - http://www.perl.org/troubleshooting_CGI.html
------------------------------
Date: 17 Nov 2001 01:55:32 GMT
From: "Mr. Chow Wing Siu" <wschow@Comp.HKBU.Edu.HK>
Subject: win32::netadmin problem
Message-Id: <9t4g2k$le0$1@net44p.hkbu.edu.hk>
Hi,
I would like to know if win32::* in latest activeperl can help
to change the home dir (connect drive to H:), Full name of user,
or more options.
Thanks.
--
Johnson Chow
------------------------------
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 2142
***************************************