[6719] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 344 Volume: 8

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Apr 22 14:25:51 1997

Date: Tue, 22 Apr 97 11:00:37 -0700
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 22 Apr 1997     Volume: 8 Number: 344

Today's topics:
     Re: [Q] Chat server in perl <rootbeer@teleport.com>
     Re: A Perl modules confusion... <rootbeer@teleport.com>
     Back References (Stefano Barbato)
     bug or feature ("my" semantics)? (York Werres)
     Re: CGI to call java app & exit (Kyzer)
     Re: Creating New Files (Kyzer)
     Re: Crypt() Question (Kyzer)
     Re: Delete List Element <a.aitken@unl.ac.uk>
     Re: HELP:  Redirection with Perl... (Kyzer)
     Re: How can i round up numbers? <rootbeer@teleport.com>
     Re: How to expand a variable in a string? <rootbeer@teleport.com>
     Re: HTML Parser anywhere ? <lglb@doc.ic.ac.uk>
     New WebTechniques and UnixReview columns on-line <merlyn@stonehenge.com>
     Odd array sizing information <nospam!dcaugh@nortel.ca>
     Re: Odd array sizing information <eryq@enteract.com>
     Re: Perl as its own metalanguage? <eryq@enteract.com>
     Re: PERL BUG: ([]) x 3 does not work correctly (Brian Dellert)
     Re: PERL BUG: ([]) x 3 does not work correctly (Brian Dellert)
     Perl compiler <jstuardo@aclaris.cl>
     Re: perl mkdir produces weird permissions <rootbeer@teleport.com>
     Re: Perl return code <rootbeer@teleport.com>
     Re: Perl/Cgi script <rootbeer@teleport.com>
     Re: print <<THIS_PART; won't work (Stefano Barbato)
     Re: Reply to Ousterhout's reply (was Re: Ousterhout and (James Cribb)
     Re: safe shelling - how to do system calls safely <rootbeer@teleport.com>
     Re: Setting value of environment variable QUERY_STRING (Kyzer)
     Re: sorting hashes on values (Andrew M. Langmead)
     Re: Variable interpolation (substitution) <a.aitken@unl.ac.uk>
     Re: Who will win?  Borland or Microsoft or Programmers? (Kyzer)
     writing to text file from Perl ... <mark@backlash.net>
     Re: writing to text file from Perl ... (Bob)
     Digest Administrivia (Last modified: 8 Mar 97) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 22 Apr 1997 08:23:30 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Elton Wells <wells@cs.utk.edu>
Subject: Re: [Q] Chat server in perl
Message-Id: <Pine.GSO.3.96.970422082251.11131F-100000@kelly.teleport.com>

On Sun, 20 Apr 1997, Elton Wells wrote:

> The problem that I am running into is that
> when the server forks the child doesn't know about the other
> clients.  Is there some way that the server can communicate with
> the children 

Do the methods in perlipc help? Good luck!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 08:15:44 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: "D. Emilio Grimaldo T." <grimaldo@panama.iaehv.nl>
Subject: Re: A Perl modules confusion...
Message-Id: <Pine.GSO.3.96.970422081154.11131E-100000@kelly.teleport.com>

On Sat, 19 Apr 1997, D. Emilio Grimaldo T. wrote:

>    I am converting a 4.x package to a 5.003 version, however I
> am getting the impression that this 'module' stuff is more a
> patch to a language than a natural extension, in particular I
> cannot explain why it works sometimes but it doesn't in others....

Sometimes it's broken, sometimes it's not. :-)

> As an example I have a module Cim.pm and a test script module,
> but when I try to execute it complains that the subroutine
> main::tellMe is not known! how can that be, it is exported
> in Cim.pm so it should be seen as part of main. 

>     # ********* GLOBAL FUNCTIONS *********
>     @EXPORT = qw( &tellMe );
>     # ********* GLOBAL VARIABLES *********
>     use vars qw($ConfFile $ResrcFile $RelativePath);
>     @EXPORT = qw($ConfFile $ResrcFile $RelativePath);

Here's one problem. @EXPORT is a regular array; there's nothing magical
about it. If you want to export more than one set of things, you'll need
to get all of them into @EXPORT, perhaps like this. 

     # ********* GLOBAL FUNCTIONS *********
     @EXPORT = qw( &tellMe );
     # ********* GLOBAL VARIABLES *********
     use vars qw($ConfFile $ResrcFile $RelativePath);
     push @EXPORT, qw($ConfFile $ResrcFile $RelativePath);

Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 14:19:22 GMT
From: mc4650@mclink.it (Stefano Barbato)
Subject: Back References
Message-Id: <5jihjk$ab5@news1.mclink.it>

I would like to print from a HTML file all html tags needed to create a Form. 
Then I would like to use a unique regular expression to do this work:
 
undef($/)'; #  all file
$_ = <HTMLFILE>;
/(<form.*?>).*?(<input.*?>).*?(<\/form>)/i
print $1, $2, $3;

This work but only for one <INPUT> field...to print all present input field I 
tried to use di expr:

/(<form.*?>).*?(<input.*?>.*?)*.*?(<\/form>)/i
print $1, ?????????

- How can I refer to the 3rd, 4th, 5th, etc. <input> field (if any, of course) 
  to print them out?
- I need to include (<input.*?>.*?)* to match all input field but what I need  
 is only the complete INPUT tag not text between INPUTs field (.*?). How I 
include nested ()s like  ((<input.*?>).*?)* to back reference only important 
chars.

 I now that my english is terrible...;-( 

---------------------------------------
  Stefano Barbato -  mc4650@mclink.it   
Flying Toolbars for Netscape Navigator:
  http://www.webcom.com/flyingtb


------------------------------

Date: Mon, 21 Apr 1997 07:02:57 GMT
From: werres@informatik.uni-bremen.de (York Werres)
Subject: bug or feature ("my" semantics)?
Message-Id: <335b0ba3.9128698@news.informatik.uni-bremen.de>

Hi,

is this a bug or a feature ("my" semantics in 5.002):

sub strange { 
  my $x = 1;
  sub { my $y = 2; sub { print $x, $y; } } 
}
&{ &{ &strange } }; # prints 2

sub very_strange {
  my $x = 1;
  sub { my $y = 2; my $x = $x; sub { print $x, $y; } }
}#                 ^^^^^^^^^^^
&{ &{ &very_strange } }; # now it prints 1

sub extremely_strange {
  my $x = 1;
  sub { my $y = 2; my $z = $x; sub { print $z, $y; } }
}#                 ^^^^^^^^^^^
&{ &{ &extremely_strange } }; # prints nothing at all!

sub workaround {
  my $x = 1;
  sub { 
    my $y = 2;
      &{+sub{my($x,$y)=@_;     # <-- this line for workaround
    sub { print $x, $y; }
      }}($x,$y);               # <-- and this line, too
  }
}
&{ &{ &workaround } }; # prints 12 (ok)

Will 5.004 still require this workaround? (It puts the free
variables into the same lexical layer.)

Note that this isn't esoteric nonsense. Just take the SCHEME-line

(define a (let a ((b 1)(c 2)) (write b) (write c) (lambda () (a b c))))

It translates to:

$a = &{ my $a; $a = sub { my ($b, $c) = @_;
        print $b, $c;
        &{+sub{my($a,$b,$c)=@_; # <--- same workaround
          sub { &$a($b, $c) }
        }}($a,$b,$c)            # <--- is required here
     } } (1, 2); # prints 12

I have one more question on this matter: In a book about a one-hump
dromedary I found on p.120 the lovely promise of making @_ "lexically
scoped someday". Of what use would that be? Wouldn't @_ be hidden by
inner subs anyway?

Oh, and a fourth question: Why is the '+' necessary in the workaround?

21.04.97 -- York.
-- 
werres@informatik.uni-bremen.de (PGP key available)
http://www.informatik.uni-bremen.de/~werres


------------------------------

Date: 22 Apr 1997 17:30:46 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: CGI to call java app & exit
Message-Id: <5jisk6$mb7@info.abdn.ac.uk>

Robert J Migliore of migliore+@andrew.cmu.edu wrote in comp.lang.perl.misc:
: Can someone suggest a way to call `java JavaServer` and return control to the
: browser?

`java JavaServer &`;  # :o)

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
Yesh I shee Felixsh, perhapsh I can be of shume ashishtanshe.


------------------------------

Date: 22 Apr 1997 17:13:20 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: Creating New Files
Message-Id: <5jirjg$mb7@info.abdn.ac.uk>

Kevin A. Miller of kmiller@mail.cheta.net wrote in comp.lang.perl.misc:
: 	I am trying to create a script that opens a single .dat file, which
: consists of array elements
: (some_name,some_address,phone_number,CATEGORY), 'Four fields seperated
: by commas'.  I then want the program to take whatever element is in the
: CATEGORY part and create a new .dat file with each CATEGORY as the name
: of the new file, then print the whole array in this new file.

#!/bin/perl
#sift.pl
while(<>){chop;($a,$b,$c,$d)=split(/,/);open(A,">>$d")||exit;print A;close(A);}

now run it like this: sift.pl <names.dat
It may throw up an error message but that's OK.

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
BONUS: Present this .sig at Tesco for a 15% discount.


------------------------------

Date: 22 Apr 1997 16:59:34 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: Crypt() Question
Message-Id: <5jiqpm$mb7@info.abdn.ac.uk>

Frank Fisher of frank@primemail.com wrote in comp.lang.perl.misc:
: Why does the crypt() function only use the first 8 characters?  Is
Because it's designed to encrypt passwords, are traditionally 6-8 characters
long. At least, they are on my machine :)

>From the manpage:
     crypt() is the password encryption function.  It is based on 
     a  one-way  encryption  algorithm  with  variations intended 
     (among other things) to frustrate use of hardware  implemen- 
     tations of a key search. 

     key is the input string to encrypt, for instance,  a  user's 
     typed  password.   Only the first eight characters are used; 
     the rest are ignored.  salt is a two-character string chosen 
     from  the  set [a-zA-Z0-9./]; this string is used to perturb 
     the hashing algorithm in one of 4096 different  ways,  after 
     which  the  input  string  is  used as the key to repeatedly 
     encrypt a constant string.  The returned value points to the 
     encrypted  input  string.   The  first two characters of the 
     return value are the salt itself. 


: there any way to encrypt more than 8 characters?
$encrypt_me="I'm a string and I'm OK.";

open(FISH,"|crypt fish >/tmp/fish") || die;
print FISH $encrypt_me;
close(FISH);
$encryped_data=`cat </tmp/fish`;

open(FISH,"|crypt fish >/tmp/fish") || die;
print FISH $encrypted_data;
close(FISH);
$decrypted_data=`cat </tmp/fish`;

print $decrypted_data;

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
BEAU PEEP: This "Cow-man" comic of yours is riduiculous!
DENNIS: S'not!
BEAU: How is this "half-man, half-cow" supposed to catch criminals?
DENNIS: HE'S GOT LASER-BEAM UDDERS!



------------------------------

Date: Tue, 22 Apr 1997 17:01:37 +0100
From: Alastair Aitken <a.aitken@unl.ac.uk>
Subject: Re: Delete List Element
Message-Id: <335CE0E0.29D8@unl.ac.uk>

christop@ozinter.co.jp wrote:
> 
> It took a while but my next question for the wise has appeared:
> 
> If I have (an) %array that looks like this:
> 
> row1=>[1,2,3,4],
> row2=>[5,6,7,8]

Note that this method of hash initialisation automatically quotes the
left hand operand.

> and I want to delete row2; do I say
> 
> delete($array{'row1'})

um .. nooo ..

you use:

delete($array{'row2'})	#	if you wanna delete row2 - not row1

Alastair.


------------------------------

Date: 22 Apr 1997 16:37:39 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: HELP:  Redirection with Perl...
Message-Id: <5jipgj$mb7@info.abdn.ac.uk>

Mike Stok of mike@stok.co.uk wrote in comp.lang.perl.misc:
: In article <5j4e88$9v3@usenet85.supernews.com>,
: No Mass Spam E-Mail! <zippidy@do.dah.com> wrote:
: >Also, Iam haveing some trouble with printing " chars.

: Maybe you want to say something like
:   print '<META HTTP-EQUIV="REFRESH" CONTENT="http://www.digispec.com/loadthis.htm">';

You could also say:
$foo="the pike";
  print <<"EOFISH";
My email address is <a href="mailto:me\@home.net">me\@home.net</a>
I charge $5 for unsolicited mail. My favourite fish is $foo.
EOFISH

Line orientated quoting is pretty handy for CGIs generating nicely marked-up
web pages instead of just text.

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
DC.W f+++ s+++ df h++++ Csilv a+ $+++ m d--- WL++ Fr---- Bslobber


------------------------------

Date: Tue, 22 Apr 1997 08:07:22 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: SIDHU <sidhu1@home.com>
Subject: Re: How can i round up numbers?
Message-Id: <Pine.GSO.3.96.970422080239.11131D-100000@kelly.teleport.com>

On 18 Apr 1997, SIDHU wrote:

> Subject: How can i round up numbers?

<Country music twang> Got my 'lectronic spurs on / Goin' to the digital
ro-day-o / Gonna round up some numbers / Yippie-eye-ay-o!  :-)

> How can i round up a number if i, for example divide 22 by 7 i.e (22/7)
> the answer it gives me is 3.145652328 I just need to round it off to two
> decimal places.  

You probably want sprintf, which will round off to a specific number of
decimal places. If that won't do it, let me know. Hope this helps! 

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 08:52:33 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Petr Prikryl <prikryl@dcse.fee.vutbr.cz>
Subject: Re: How to expand a variable in a string?
Message-Id: <Pine.GSO.3.96.970422084814.11131L-100000@kelly.teleport.com>

On 21 Apr 1997, Petr Prikryl wrote:

> How can I expand the string while replacing the
> substring "$variable" by the content of the $variable?

    $string =~ s!\$(\w+)! '$' . $1 !gee;

Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 09:15:53 +0100
From: Leon Brocard <lglb@doc.ic.ac.uk>
Subject: Re: HTML Parser anywhere ?
Message-Id: <335C73B9.41C67EA6@doc.ic.ac.uk>

Mike Stok wrote:

> I have an HTML::Parser module installed on my system:
> 
> NAME
>   HTML::Parser - SGML parser class

Hmm, this is actually a rather good module, but I seem
to remember that the $p->asText (or whatever) didn't work
properly for tables - it didn't end table rows or table datas
with </TR> or </TD>, which kind of confuses any HTML parser.

Any idea why / how this is? Leon.

-- 
Leon Brocard.................................................PERL / JAVA
lglb@doc.ic.ac.uk..........<URL:http://www-students.doc.ic.ac.uk/~lglb/>

 ... Oh, it's you.


------------------------------

Date: 22 Apr 1997 09:14:21 -0700
From: Randal Schwartz <merlyn@stonehenge.com>
Subject: New WebTechniques and UnixReview columns on-line
Message-Id: <8cbu77avc2.fsf@gadget.cscaper.com>


I've placed online column 14 in my bi-monthly series appearing in Unix
Review magazine, the second part of the two-parter on a gentle
introduction to objects.

I've also placed online columns 12 and 13 in my monthly series
appearing in WebTechniques magazine.

Column 12 talks about making searchable web pages, and gives a sample
search engine for small amounts of text.  The search engine is
actually online, searching the source files for all the WebTechniques
examples. :-) Column 13 shows how I make my "non-visitor" counter on
my home page, along with being a quick introduction to creating
graphics with the GD module.

Many people have asked for the WebTechniques programs to be available
for separate download (because they can't cut-n-paste from their
browser, I guess), so I've also extracted those separately.

These columns are available at:

        http://www.stonehenge.com/merlyn/UnixReview/
	http://www.stonehenge.com/merlyn/WebTechniques/

Please remember that these items are not mine to give away -- I
republish them in a very limited fashion with the express permission
of the copyright owner, Miller-Freeman, Inc.  So, don't mirror them or
steal from them.  Thanks.  And buy the magazines, or at least write
them a thank-you note.

As always, if you like what you are reading for free, consider buying
Unix Review or WebTechniques or a Llama or Camel book.  You could also
make a donation to my legal defense fund (pointers on the webpage).

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@ora.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



------------------------------

Date: Tue, 22 Apr 1997 12:20:40 -0400
From: Dan Caugherty <nospam!dcaugh@nortel.ca>
Subject: Odd array sizing information
Message-Id: <335CE558.57C2@nortel.ca>

TWIMC --

Hi!  I ran across what I believe to be a Perl bug.
Given the following snippet:
# --------------------------------------------
# For sake of argument, assume $#some_array == 25
@new_array = ();
print $#some_array, "   ";
for($i = 0; $i < $#some_array; $i++)
{
    push @new_array, $i;  
}
print $#new_array, "\n";
# ---------------------------------------------

I get:

25   24

I would expect to get:

25   25

Is this a bug?  If so, has it been fixed?  If not,
what Perl nuance am I missing here?

TIA,
--  Dan C.
+------------------dcaugh@pas_de_spammer!.nortel.ca------------------+
| Dan Caugherty         | All opinions herein are MINE.  Not yours,  |
| Nortel RTP, NC        | theirs, his, hers, or its.  So there.      |
| Pursuant to US Code, Title 47, Chapter 5, Subchapter II, '227,     |
| ANY AND ALL UNSOLICITED COMMERCIAL E-MAIL is subject to a download |
| and archival fee in the amount of $100 US. E-mailing this address  |
| denotes acceptance of these terms.  (And I can use the money.)     |
+------------------dcaugh@pas_de_spammer!.nortel.ca------------------+


------------------------------

Date: Tue, 22 Apr 1997 12:08:38 -0500
From: Eryq <eryq@enteract.com>
To: Dan Caugherty <nospam!dcaugh@nortel.ca>
Subject: Re: Odd array sizing information
Message-Id: <335CF096.4ED7484A@enteract.com>


$#x is *not* the number of elements of @x; it's the 0-based index
of the last element.  In general,

	$#x == (int(@x) - 1)

So say:

	for ($i = 0; $i <= $#some_array; $i++)

or:

	for ($i = 0; $i < int(@some_array); $i++)

but if you just want to copy arrays (as you seem to in
your example), say:

	@new_array = @some_array;
 
HTH,
-- 
  ___  _ _ _   _  ___ _   Eryq (eryq@enteract.com)
 / _ \| '_| | | |/ _ ' /  Hughes STX, NASA/Goddard Space Flight Cntr.
|  __/| | | |_| | |_| |   http://www.enteract.com/~eryq
 \___||_|  \__, |\__, |___/\  Visit STREETWISE, Chicago's newspaper by/
           |___/    |______/ of the homeless: http://www.streetwise.org


------------------------------

Date: Tue, 22 Apr 1997 11:09:25 -0500
From: Eryq <eryq@enteract.com>
To: Malcolm Beattie <mbeattie@sable.ox.ac.uk>
Subject: Re: Perl as its own metalanguage?
Message-Id: <335CE2B5.1532C541@enteract.com>


Malcolm Beattie wrote:
> 
> In article <335BA4C2.6DF60FCD@enteract.com>, Eryq  <eryq@enteract.com> wrote:
> >As for a Perl program examining itself: apart from looking at the
> >symbol table, you don't often see this done.  At least, I can't
> >think of any program I've ever seen that looks at its own bytecode.
> 
> I suppose it's really a module rather than a program but the Perl
> compiler makes a program examine itself and look at its own
> bytecode (well, op tree). It just loads up the program, pores over
> its own entrails and dumps them out in some appropriate format.
> The B module provides the necessary XSUBs to examine the internal
> structures of the program; the compiler itself is written in Perl.

I was thinking of the more general case (somehow a compiler seems a special 
case to me).   In forward-chaining expert systems, or any systems capable 
of "learning" new facts/rules and modifying their own knowledge bases, you 
can see this "introspective behavior" quite frequently (e.g., "hmmm... I see a 
pattern emerging... do I have a rule for this case? no? well, let me create one...")
Although perhaps we're leaving the Prolog world here, and entering the CYC domain...
  
Regards,
-- 
  ___  _ _ _   _  ___ _   Eryq (eryq@enteract.com)
 / _ \| '_| | | |/ _ ' /  Hughes STX, NASA/Goddard Space Flight Cntr.
|  __/| | | |_| | |_| |   http://www.enteract.com/~eryq
 \___||_|  \__, |\__, |___/\  Visit STREETWISE, Chicago's newspaper by/
           |___/    |______/ of the homeless: http://www.streetwise.org


------------------------------

Date: Tue, 22 Apr 1997 17:00:01 GMT
From: xpress@pobox.com (Brian Dellert)
Subject: Re: PERL BUG: ([]) x 3 does not work correctly
Message-Id: <335ce7ca.7094656@news.real-time.com>

On 18 Apr 1997 05:41:00 -0700, Randal Schwartz <merlyn@stonehenge.com>
wrote:

>A slightly tacky solution, but probably rather efficient:
>
>	@chain = map [], @pieces;
>
>Map is your friend.

Thanks everybody for your help. I took the map solution and tried it
out with Benchmark to see what worked the best in the real world.
Suprisingly, my original solution was far and away the slowest! The
map solution was much, much faster.

Notice that I tested two alternative "map" solutions. One creates a
@chain with the same number of elements as @pieces. The second takes
the scalar $pieces, creates a temporary array on the fly, then maps
this into @chain as before. Even with this extra step, it is *still*
faster than the foreach solution! In fact, it's just as fast as the
other map solution (!?!?!)

Here's the results:

--begin--

> try.pl
10 loops with 50000 pieces:
---------------------------------
Foreach: 15 secs (15.13 usr  0.61 sys = 15.74 cpu)
Map:     10 secs (10.48 usr  0.18 sys = 10.66 cpu)
Map_2:   11 secs (10.47 usr  0.04 sys = 10.51 cpu)


50000 loops with 10 pieces:
---------------------------------
Foreach: 18 secs (16.64 usr  0.00 sys = 16.64 cpu)
Map:     11 secs (10.83 usr  0.00 sys = 10.83 cpu)
Map_2:   11 secs (10.68 usr  0.00 sys = 10.68 cpu)

--end--

Here the results, tested in a different order:

--begin--

> try.pl
10 loops with 50000 pieces:
---------------------------------
Map:     12 secs (10.65 usr  0.70 sys = 11.35 cpu)
Foreach: 17 secs (16.90 usr  0.06 sys = 16.96 cpu)
Map_2:   11 secs (10.58 usr  0.02 sys = 10.60 cpu)


50000 loops with 10 pieces:
---------------------------------
Map_2:   10 secs ( 9.89 usr  0.00 sys =  9.89 cpu)
Map:      9 secs ( 9.63 usr  0.00 sys =  9.63 cpu)
Foreach: 19 secs (17.60 usr  0.00 sys = 17.60 cpu)

--end--

Here's my code.

--begin--
#!/usr/bin/perl -w

use Benchmark;
use strict;
use vars qw(@Pieces $Pieces $Count);

my $Foreach = <<'EndHere';
	my($i, @chain);
	for($i=0; $i<@Pieces; $i++) {
		push @chain, [];
	}
EndHere

my $Map =   'my @chain = map [], @Pieces';
my $Map_2 = 'my @chain = map [], (0) x $Pieces';

$Pieces = 50000;
@Pieces = (0) x $Pieces;
$Count  = 10;

print "$Count loops with $Pieces pieces:\n";
print "---------------------------------\n";
print "Map:     ", timestr(timeit($Count, $Map)), "\n";
print "Foreach: ", timestr(timeit($Count, $Foreach)), "\n";
print "Map_2:   ", timestr(timeit($Count, $Map_2)), "\n";

print "\n\n";

$Pieces = 10;
@Pieces = (0) x $Pieces;
$Count  = 50000;

print "$Count loops with $Pieces pieces:\n";
print "---------------------------------\n";
print "Map_2:   ", timestr(timeit($Count, $Map_2)), "\n";
print "Map:     ", timestr(timeit($Count, $Map)), "\n";
print "Foreach: ", timestr(timeit($Count, $Foreach)), "\n";

--end--

Thanks again for everyone's help. I mistakenly assumed using map to
step through @pieces would slow the process down, but the numbers
proved me wrong.

Sincerely,
Brian Dellert



------------------------------

Date: Tue, 22 Apr 1997 17:23:01 GMT
From: xpress@pobox.com (Brian Dellert)
Subject: Re: PERL BUG: ([]) x 3 does not work correctly
Message-Id: <335cf234.9760827@news.real-time.com>

Ooops, I forgot about one solution that involves a while loop. It is
*much* slower than all the rest -- even significantly slower than the
foreach loop! Any ideas why?

Here's the new results (I had to cut back the loop to 25,000
iterations or 25,000 pieces to avoid an out of memory error with the
While test):

---begin---

> try.pl
10 loops with 25000 pieces:
---------------------------------
Map:      5 secs ( 5.19 usr  0.42 sys =  5.61 cpu)
Foreach:  8 secs ( 7.54 usr  0.03 sys =  7.57 cpu)
Map_2:    5 secs ( 5.23 usr  0.03 sys =  5.26 cpu)
While:   10 secs ( 6.15 usr  3.24 sys =  9.39 cpu)


25000 loops with 10 pieces:
---------------------------------
Map_2:    5 secs ( 5.03 usr  0.00 sys =  5.03 cpu)
While:   17 secs ( 7.54 usr  3.43 sys = 10.97 cpu)
Map:      5 secs ( 4.87 usr  0.00 sys =  4.87 cpu)
Foreach:  9 secs ( 7.82 usr  0.00 sys =  7.82 cpu)

---end---



Here's the new code:

---begin---
#!/usr/bin/perl -w

use Benchmark;
use strict;
use vars qw(@Pieces $Pieces $Count);

my $Foreach = <<'EndHere';
	my($i, @chain);
	for($i=0; $i<@Pieces; $i++) {
		push @chain, [];
	}
EndHere

my $Map   =   'my @chain = map [], @Pieces';
my $Map_2 = 'my @chain = map [], (0) x $Pieces';
my $While = 'my $pieces = $Pieces; push(@chain, []) while $pieces--;';

$Pieces = 25000;
@Pieces = (0) x $Pieces;
$Count  = 10;

print "$Count loops with $Pieces pieces:\n";
print "---------------------------------\n";
print "Map:     ", timestr(timeit($Count, $Map)), "\n";
print "Foreach: ", timestr(timeit($Count, $Foreach)), "\n";
print "Map_2:   ", timestr(timeit($Count, $Map_2)), "\n";
print "While:   ", timestr(timeit($Count, $While)), "\n";

print "\n\n";

$Pieces = 10;
@Pieces = (0) x $Pieces;
$Count  = 25000;

print "$Count loops with $Pieces pieces:\n";
print "---------------------------------\n";
print "Map_2:   ", timestr(timeit($Count, $Map_2)), "\n";
print "While:   ", timestr(timeit($Count, $While)), "\n";
print "Map:     ", timestr(timeit($Count, $Map)), "\n";
print "Foreach: ", timestr(timeit($Count, $Foreach)), "\n";

---end---



------------------------------

Date: Tue, 22 Apr 1997 12:12:25 -0300
From: "Jaime Stuardo" <jstuardo@aclaris.cl>
Subject: Perl compiler
Message-Id: <5jibpb$3oe$4@news.rdc.cl>

Hi all

Is there a Shareware Perl compiler?

Thanks
Jaime
--
-----------------------------------------
Jaime A. Stuardo B.
Ing. Civil Electrsnica
Universidad Ticnica F. Santa Marma
Valparamso, CHILE

mailto:jstuardo@aclaris.cl
mailto:jstuardo@geocities.com
mailto:jstuardo@lucas.elo.utfsm.cl
http://www.geocities.com/CollegePark/3485
-----------------------------------------





------------------------------

Date: Tue, 22 Apr 1997 08:25:28 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Ken Anderson <anderson@necsys.gsfc.nasa.gov>
Subject: Re: perl mkdir produces weird permissions
Message-Id: <Pine.GSO.3.96.970422082502.11131H-100000@kelly.teleport.com>

On 21 Apr 1997, Ken Anderson wrote:

> i was trying to use the perl mkdir function and
> am winding up with weird permissions on the directory.

I think you want the oct() function. Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 08:24:40 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Walter Boev <walter.boev@nrc.ca>
Subject: Re: Perl return code
Message-Id: <Pine.GSO.3.96.970422082355.11131G-100000@kelly.teleport.com>

On 21 Apr 1997, Walter Boev wrote:

> How does one trap a perl script return code.
> The perl script is called from a Korn shell script. When
> the perl script finishes it returns an exit code (e.g. exit 7).
> The Korn shell script then continues to run. However depending
> on the exit status returned by the perl script the Korn shell
> script can branch.

Isn't this covered in the Korn shell's docs? It's certainly not in the
Perl docs because it's not a Perl question. :-)  Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 08:30:25 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: BBurns@Cbclegal.com
Subject: Re: Perl/Cgi script
Message-Id: <Pine.GSO.3.96.970422082547.11131I-100000@kelly.teleport.com>

On Mon, 21 Apr 1997 BBurns@Cbclegal.com wrote:

> Why does my webserver eventually slow down since the addition of CGI
> scripting (mainly multiple counter programs)???  

It's probably busy executing them. :-)

You might be able to improve the counter programs to make them run faster. 
One way to speed things up would be to remove the counters altogether,
since they're like the car counters you sometimes see at the side of the
freeway. When was the last time you saw somebody stop to look at one of
those? :-) 

Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: Tue, 22 Apr 1997 13:33:27 GMT
From: mc4650@mclink.it (Stefano Barbato)
Subject: Re: print <<THIS_PART; won't work
Message-Id: <5jietf$89h@news1.mclink.it>

In article <adelton.861637131@aisa.fi.muni.cz>, adelton@fi.muni.cz (Honza Pazdziora) wrote:
>ledheadx@mindspring.com (Ken Ledbetter) writes:
>
>> I'm trying to do something like this
>> 
>> print <<STOP;
>> 
>>    <html><body>
>>    HI
>>    </body></html>
>> STOP
>> 
>> but it won't work, any help?
I think that You don't have to include the ";" after STOP and be careful to 
don't indent "STOP" (no tab or spaces first).

example:
  
----------------------------
     print <<STOP
        first line
        sec line
STOP
;
----------------------------------

  bye
  Stefano Barbato


------------------------------

Date: 18 Apr 1997 04:21:55 GMT
From: jamesc@bain.oz.au (James Cribb)
Subject: Re: Reply to Ousterhout's reply (was Re: Ousterhout and Tcl ...)
Message-Id: <5j6st3$h06$1@plath.bain.oz.au>

[Newgroups trimmed]


Chris Bitmead uid(x22068) (Chris.Bitmead@alcatel.com.au) wrote:
|
| While Lisp does not store numbers as strings,
| I never know or care that this is the case.

This will also be true for Tcl 8, I believe.  Lists too.

| Why do you want to fit everything in a string??
| It buys you NOTHING.

On the contrary, it makes much easier to pass data across
architecture, process, and programming languages boundaries.
Just what you need in a glue language.

--
James Cribb (james.cribb@aus.deuba.com)
Information Technology, Deutsche Morgan Grenfell Australia Limited
Phone: +61 2 9258 1375    Fax: +61 2 9258 1004
My opinions are not necessarily those of my employer.  Pity.


------------------------------

Date: Tue, 22 Apr 1997 08:01:06 -0700
From: Tom Phoenix <rootbeer@teleport.com>
To: Michael Friendly <friendly@hotspur.psych.yorku.ca>
Subject: Re: safe shelling - how to do system calls safely
Message-Id: <Pine.GSO.3.96.970422075432.11131C-100000@kelly.teleport.com>

On 18 Apr 1997, Michael Friendly wrote:

> The 'unsafe' form does work:
> 
> 	system ("$SAS $sasopts -sysin $sasfile");
> 
> but lots of variations of this do not:
> 	system "$SAS", $sasopts, $sasfile;
> 	system $SAS, $sasopts, $sasfile;
> 	system $SAS, $sasopts, '-sysin',  $sasfile;

So, what are $SAS, $sasopts, and $sasfile? If any of them contains a
space, you're probably not doing what you think. For example,

        system '/bin/echo', 'foo; rm -rf *'

will not treat the semicolon or asterisk as special, since there's no
shell involved. And aren't you glad you didn't combine those two into one
argument? :-)  Hope this helps!

-- Tom Phoenix        http://www.teleport.com/~rootbeer/
rootbeer@teleport.com   PGP  Skribu al mi per Esperanto!
Randal Schwartz Case:     http://www.lightlink.com/fors/



------------------------------

Date: 22 Apr 1997 17:23:06 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: Setting value of environment variable QUERY_STRING
Message-Id: <5jis5q$mb7@info.abdn.ac.uk>

THIAM YEO of n1835173@sparrow.qut.edu.au wrote in comp.lang.perl.misc:

: Hi there!

:   IS it possible for me to set the value of QUERY_STRING from a Perl
: script such that another script will be able to pick up that value?

$ENV{'QUERY_STRING'} = "Something of interest";

:   And, how do I pass a value from a Perl script to another Perl script?

#!/bin/perl
#runme1.pl
open(POLOYP,"|perl runme2.pl") || die;
print POLOYP "Thanks for the fish\n";
close(POLOYP);

#!/bin/perl
#runme2.pl
while(<>){print;}

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
"Slightly less safe than joining the mile high club bareback with a junkie
whore in a plane flying over a war zone with three engines on fire, a pissed
up pilot and Carlos the Jackal sitting RIGHT BEHIND YOU" - MK3 advert.


------------------------------

Date: Tue, 22 Apr 1997 16:41:11 GMT
From: aml@world.std.com (Andrew M. Langmead)
Subject: Re: sorting hashes on values
Message-Id: <E91t0n.E86@world.std.com>

[a copy of this article as mailed to the author of the referenced article.]

Hans Suijkerbuijk <Hans.Suijkerbuijk@SD.BE> writes:

>I get an undefined number of hashes as results of a query. (each hash is
>a record) In each hash i have three keys: code, title and a score. I
>want to collect the hashes (in a hash or in an array?) and then sort the
>collection of hashes according to the (integer) value of the key score
>before displaying the collection. How do I do this (i experimented a lot
>with sort-stuff, but nothing worked)?

What is being returned is a list of references. Each of these
references points to a hash with duplicately labeled keys. sort()
takes a list as an argument, the sort-block will alias the scalars $a
and $b to each element in the list. 

So in this block, you need to dereference $a and $b and access the
data they refer to. compare this data, and tell sort() how to order
the references it is given. sort() will return copies of these
references (pointing to the same hashes. Don't change the data by the
pointers in one hash without expecting changes in both.) in the
specified order.

for $record (sort { $a->{score} <=> $b->{score} } 
                          i_return_a_list_of_refs_to_hashes()) {
  print "$record->{code} $record->{title} $record->{score}\n";
}

Maybe this should be mentioned in the
FAQ. <http://www.perl.com/CPAN/docs/FAQs/> It does have a "How do I
sort an array by (anything)?", but the answer doesn't give any
examples where "(anything)" is a reference to an array or hash.
-- 
Andrew Langmead


------------------------------

Date: Tue, 22 Apr 1997 16:14:18 +0100
From: Alastair Aitken <a.aitken@unl.ac.uk>
Subject: Re: Variable interpolation (substitution)
Message-Id: <335CD5CA.D9B@unl.ac.uk>


John Adamson wrote:
> 
> Could use some advice on variable interpolation (as Wall uses
> the term) in a conditional statement.

Larry or Max?
 
> Consider this simple statement:
>   for ($i=1;$i<=10;$i++){print "$i\n";}

ok, considering ...
 
> Is it possible to use:
>   for ($arg1;$arg2;$arg3){print "$i\n";}
> where $arg1-3 are defined elsewhere?

> Seems like it oughta be simple but haven't been able to get
> it to work.  Thoughts will be appreciated and - if this is an
> incredibly dumb question - well, heck, I'm sorry.  TIA ..

Sorry?  Sorry is it!! GRRR!

:-) 

ahem ...

That doesn't look like a conditional statement but a for loop. However
 ...

#! /usr/local/bin/perl

require 5.003;
use Strict;

my ($arg1, $arg2, $arg3);

$arg1 = '$i=1';
$arg2 = '$i<=10';
$arg3 = '$i++;
for (eval"$arg1";eval"$arg2";eval"$arg3") {
    print "$i\n";
}

works fine.  I used Strict and declared args 1-3 as local variables. 
You need to thnk about where
they will be valued and how they will be passed to the routine that
includes the for loop.

Alastair.


------------------------------

Date: 22 Apr 1997 17:14:31 GMT
From: junkmail@sysa.abdn.ac.uk (Kyzer)
Subject: Re: Who will win?  Borland or Microsoft or Programmers?
Message-Id: <5jirln$mb7@info.abdn.ac.uk>

Mark Bracey of mbracey@interaccess.com wrote in comp.lang.perl.misc:
: hbaecker@island.net wrote:

: > Borland Delphi 2 is incredibly difficult to use. 
: You are kidding...right?

Difficult as in "now, should I put the button to the left, or to the right?"

--
Stuart 'Kyzer' Caie - Kyzer/CSG |undergraduate of Aberdeen University |100%
http://www.abdn.ac.uk/~u13sac   |My opinions aren't those of Aberdeen |Amiga -
kyzer@4u.net kyzer@hotmail.com  |University or AUCC, thankfully.***** |always!
BEAU PEEP: This "Cow-man" comic of yours is riduiculous!
DENNIS: S'not!
BEAU: How is this "half-man, half-cow" supposed to catch criminals?
DENNIS: HE'S GOT LASER-BEAM UDDERS!



------------------------------

Date: Tue, 22 Apr 1997 11:07:43 -0500
From: Mark Smith <mark@backlash.net>
Subject: writing to text file from Perl ...
Message-Id: <335CE24F.611F@backlash.net>

I am trying to write to a text file through my perl script, however, I
want to post to the top of the files rather than the bottom without
having to write the entire file in an array and re-writing it.  By
re-writing the file over and over again, it seems like I am losing data
because it cuts the very bottom of the file off and I sometimes get
fifteen million squares in the middle of my file.  The whole procedure
is just unstable.  When I had the script writing to the bottom of the
file, I did not have any of these problems, however, I need the text
files to read in chronological order (top to bottom, as opposed to
bottom to top) because, well, that is what my boss said.  Please help! 
thanks!!!

Lina
lina@ty.com


------------------------------

Date: Tue, 22 Apr 1997 17:56:25 GMT
From: xxbbell@voicenet.com (Bob)
Subject: Re: writing to text file from Perl ...
Message-Id: <5jiufg$bfc$2@news2.voicenet.com>

Mark Smith <mark@backlash.net> wrote:
>I am trying to write to a text file through my perl script, however, I
>want to post to the top of the files rather than the bottom without
>having to write the entire file in an array and re-writing it.  By
>re-writing the file over and over again, it seems like I am losing data
>because it cuts the very bottom of the file off and I sometimes get
>fifteen million squares in the middle of my file.  The whole procedure
>is just unstable.  When I had the script writing to the bottom of the
>file, I did not have any of these problems, however, I need the text
>files to read in chronological order (top to bottom, as opposed to
>bottom to top) because, well, that is what my boss said.  Please help! 
>thanks!!!
>
>Lina
>lina@ty.com

	I'm a newbie to Perl, but you could do what I did.  Recently I
wrote a perl script to create MOTDs, with a log, and I wanted the most
recent at the top.  What I wound up doing is appending to the file.
The files are then in reverse order.  I then read the entries in when
it was time to view the MOTD and just did a reverse.  Adding an MOTD
was therefore fast, and since I was reading the  whole file when
viewing an MOTD, the only extra bit was a "reverse" of an array after
the array was already in memory, which didn't seem to be bad at all.
Not sure if this will apply to you, or if there is a better way (>+?).

          - Bob
          xxbbell@voicenet.comxx
          remove x's to reply



------------------------------

Date: 8 Mar 97 21:33:47 GMT (Last modified)
From: Perl-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 8 Mar 97)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

To submit articles to comp.lang.perl.misc (and this Digest), send your
article to perl-users@ruby.oce.orst.edu.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

The Meta-FAQ, an article containing information about the FAQ, is
available by requesting "send perl-users meta-faq". The real FAQ, as it
appeared last in the newsgroup, can be retrieved with the request "send
perl-users FAQ". Due to their sizes, neither the Meta-FAQ nor the FAQ
are included in the digest.

The "mini-FAQ", which is an updated version of the Meta-FAQ, is
available by requesting "send perl-users mini-faq". It appears twice
weekly in the group, but is not distributed in the digest.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V8 Issue 344
*************************************

home help back first fref pref prev next nref lref last post