[17815] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5235 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jan 4 18:16:36 2001

Date: Thu, 4 Jan 2001 15:16:09 -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: <978650169-v9-i5235@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Thu, 4 Jan 2001     Volume: 9 Number: 5235

Today's topics:
        newbie OO in Perl <ducateg@info.bt.co.uk>
    Re: newbie OO in Perl <ducateg@info.bt.co.uk>
    Re: newbie OO in Perl <jasonh_/dev/null_autonomy.com>
    Re: newbie OO in Perl <ducateg@info.bt.co.uk>
    Re: newbie OO in Perl (Eric Bohlman)
    Re: newbie OO in Perl <bart.lateur@skynet.be>
    Re: newbie OO in Perl <ducateg@info.bt.co.uk>
    Re: newbie OO in Perl <ducateg@info.bt.co.uk>
        newbie perl/xchat question <theobvioushattonr@aug.com>
        NEWBIE Q: DEC2HEX?? HOW <angenent@kabelfoon.nl>
    Re: NEWBIE Q: DEC2HEX?? HOW <angenent@kabelfoon.nl>
    Re: NEWBIE Q: DEC2HEX?? HOW (Colin Watson)
    Re: Newbie with Session Key and META questions <markymoon@zenbydesign.com>
        NEWBIE: Perl and Expect.pm <Achim.Finger@t-online.de>
        Newby scrpt help <mark@marknw.com>
    Re: Newby scrpt help <tony_curtis32@yahoo.com>
    Re: Newby scrpt help (Garry Williams)
    Re: Newby scrpt help <mark@marknw.com>
    Re: Newby scrpt help <uri@sysarch.com>
    Re: Newby scrpt help <fozz@iodynamics.com>
    Re: Newby scrpt help <nawft@mindspring.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 3 Jan 2001 12:12:09 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: newbie OO in Perl
Message-Id: <92v53r$3bc$1@pheidippides.axion.bt.co.uk>

Hi,

My aim has been to turn a perl script into a Object Oriented module (pm
file).

That has worked and it is nice (apart from the referencing dereferencing
business which is
permanently confusing me), nice slOOw. What used to take 2 minutes at
most is most taking nearly 40 minutes :(

I had a look at the bit that is slowing down the process, it turned out to
be a hash population (or a hash in a hash to be precise). Is there anything
I can do to speed it up a little bit like saving some space in memory in
advance?

Hints that are common practices are most welcome...


--
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry Ducatel
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<






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

Date: Wed, 3 Jan 2001 13:52:14 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: Re: newbie OO in Perl
Message-Id: <92vavi$5n7$1@pheidippides.axion.bt.co.uk>

Sorry, I figured the code would not be necessary, but I may be wrong, here
is the code:

every loop @pop_array contains words from a text.

- %dbkeywords is a hash that holds hashes (my hash in a hash)

- $keyword is the keyword being processed(did I tell you this is part of a
indexer?) , the goal is to store this keyword as the key of the %dbkeywords
hash, the first value is the name of the file in which the word was found,
and the final value is a weight that reflect the importance of this word (if
you did not know, this is the structure of an inverted file and the weight
is called the IDF weight, i.e. I am writing a local search engine...)

- $pop_index is the name of the file in which the word was found at the time
of the iteration

- $skippy iteratively gets the number of times the word occurs in the text.



++++a for loop that populates @pop_array+++++

++ the code that populates...

for $keyword (@pop_array)
    {
       next if($dbkeywords{$keyword}{$pop_index});

       $skippy=0;
       for $buffer (@pop_array)
      {
         if ($keyword eq $buffer)
        {
          ++$skippy;
        }
      }
      $dbkeywords{$keyword}{$pop_index}=$skippy;
    }

++++ end of that for loop that populates @pop_array ++++

This code happens to be fast if you run it as a .pl file embeded into a
normal script. Only, I put it into a .pm file that I put in the Perl
directory somewhere. The code is changed because it is a module that can
generate objects. I have a script that create an object of this class. The
piece of code I put here is meant to run during the initialisation process.
All the scripts have the same package declaration (which is different from
the name of the module).

To summarise, I had a fast script, I turned the code into a module, this
module is OO. I have a script that instanciates an object and the
initialisation bit that I have pasted above is slOOw. It has made my work
preaty much useless.

Any thoughts on this are welcome,
--
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry Ducatel
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
"Géry" <ducateg@info.bt.co.uk> wrote in message
news:92v53r$3bc$1@pheidippides.axion.bt.co.uk...
> Hi,
>
> My aim has been to turn a perl script into a Object Oriented module (pm
> file).
>
> That has worked and it is nice (apart from the referencing dereferencing
> business which is
> permanently confusing me), nice slOOw. What used to take 2 minutes at
> most is most taking nearly 40 minutes :(
>
> I had a look at the bit that is slowing down the process, it turned out to
> be a hash population (or a hash in a hash to be precise). Is there
anything
> I can do to speed it up a little bit like saving some space in memory in
> advance?
>
> Hints that are common practices are most welcome...
>
>
> --
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> Géry Ducatel
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
>
>
>




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

Date: Wed, 3 Jan 2001 13:14:10 -0000
From: "Jason Holland" <jasonh_/dev/null_autonomy.com>
Subject: Re: newbie OO in Perl
Message-Id: <3a53260e_2@nnrp1.news.uk.psi.net>

That sounds like a pretty bad performance hit, what exactly are you doing
that would cause such a massive slowdown?

J.


"Géry" <ducateg@info.bt.co.uk> wrote in message
news:92v53r$3bc$1@pheidippides.axion.bt.co.uk...
> Hi,
>
> My aim has been to turn a perl script into a Object Oriented module (pm
> file).
>
> That has worked and it is nice (apart from the referencing dereferencing
> business which is
> permanently confusing me), nice slOOw. What used to take 2 minutes at
> most is most taking nearly 40 minutes :(
>
> I had a look at the bit that is slowing down the process, it turned out to
> be a hash population (or a hash in a hash to be precise). Is there
anything
> I can do to speed it up a little bit like saving some space in memory in
> advance?
>
> Hints that are common practices are most welcome...
>
>
> --
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> Géry Ducatel
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
>
>
>




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

Date: Wed, 3 Jan 2001 14:12:58 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: Re: newbie OO in Perl
Message-Id: <92vc6e$6g4$1@pheidippides.axion.bt.co.uk>

er, problem solved, please stop worrying about it, I had a bug bigger than
an elephant, that was all...

--
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry Ducatel

"Géry" <ducateg@info.bt.co.uk> wrote in message
news:92v53r$3bc$1@pheidippides.axion.bt.co.uk...
> Hi,
>
> My aim has been to turn a perl script into a Object Oriented module (pm
> file).
>
> That has worked and it is nice (apart from the referencing dereferencing
> business which is
> permanently confusing me), nice slOOw. What used to take 2 minutes at
> most is most taking nearly 40 minutes :(
>
> I had a look at the bit that is slowing down the process, it turned out to
> be a hash population (or a hash in a hash to be precise). Is there
anything
> I can do to speed it up a little bit like saving some space in memory in
> advance?
>
> Hints that are common practices are most welcome...
>
>
> --
> >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> Géry Ducatel
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>
>
>
>




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

Date: 3 Jan 2001 14:44:24 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: newbie OO in Perl
Message-Id: <92vds8$9rm$1@bob.news.rcn.net>

"Géry" <ducateg@info.bt.co.uk> wrote:

You mentioned that the extreme slowdown you experienced was due to an
overlooked bug, but there's another source of slowness...

> for $keyword (@pop_array)
>     {
>        next if($dbkeywords{$keyword}{$pop_index});

>        $skippy=0;
>        for $buffer (@pop_array)
>       {
>          if ($keyword eq $buffer)
>         {
>           ++$skippy;
>         }
>       }
>       $dbkeywords{$keyword}{$pop_index}=$skippy;
>     }

This code has what we call quadratic behavior: its runtime is proportional
to the square of the number of elements in @pop_array.  Double the number
of keywords and it will take four times as long.  If I understand the aim
of your code correctly (set $dbkeywords{$keyword}{$pop_index} to the
number of times $keyword occurs in @pop_array), then you can get linear
behavior (runtime proportional to the number of elements, so that doubling
the number of keywords takes only twice as long) very easily by replacing 
all that code with:

++$dbkeywords{$keyword}{$pop_index} for $keyword (@pop_array);



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

Date: Wed, 03 Jan 2001 16:25:25 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: newbie OO in Perl
Message-Id: <okk65t8m8f15t6pcim7v0kskj3mfm3o662@4ax.com>

Géry wrote:

>er, problem solved, please stop worrying about it, I had a bug bigger than
>an elephant, that was all...

So how much is the performance hit now? Just for the record...

-- 
	Bart.


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

Date: Wed, 3 Jan 2001 17:36:12 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: Re: newbie OO in Perl
Message-Id: <92vo3f$av2$1@pheidippides.axion.bt.co.uk>

That appears to be very right. I am most grateful and impressed   :o

I had problem populating this hash structure and I came up with the fix you
have optimised. As usual I overcomplicated my code. It is nice to see some
people can actualy understand it...

All the best


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry Ducatel
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

"Eric Bohlman" <ebohlman@omsdev.com> wrote in message
news:92vds8$9rm$1@bob.news.rcn.net...
> "Géry" <ducateg@info.bt.co.uk> wrote:
>
> You mentioned that the extreme slowdown you experienced was due to an
> overlooked bug, but there's another source of slowness...
>
> > for $keyword (@pop_array)
> >     {
> >        next if($dbkeywords{$keyword}{$pop_index});
>
> >        $skippy=0;
> >        for $buffer (@pop_array)
> >       {
> >          if ($keyword eq $buffer)
> >         {
> >           ++$skippy;
> >         }
> >       }
> >       $dbkeywords{$keyword}{$pop_index}=$skippy;
> >     }
>
> This code has what we call quadratic behavior: its runtime is proportional
> to the square of the number of elements in @pop_array.  Double the number
> of keywords and it will take four times as long.  If I understand the aim
> of your code correctly (set $dbkeywords{$keyword}{$pop_index} to the
> number of times $keyword occurs in @pop_array), then you can get linear
> behavior (runtime proportional to the number of elements, so that doubling
> the number of keywords takes only twice as long) very easily by replacing
> all that code with:
>
> ++$dbkeywords{$keyword}{$pop_index} for $keyword (@pop_array);
>




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

Date: Wed, 3 Jan 2001 17:38:08 -0000
From: "Géry" <ducateg@info.bt.co.uk>
Subject: Re: newbie OO in Perl
Message-Id: <92vo73$avf$1@pheidippides.axion.bt.co.uk>

It is difficult to measure exactly, it is probably a few seconds slower, but
nothing really worrying, the system is largely scalable...


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Géry Ducatel
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

"Bart Lateur" <bart.lateur@skynet.be> wrote in message
news:okk65t8m8f15t6pcim7v0kskj3mfm3o662@4ax.com...
> Géry wrote:
>
> >er, problem solved, please stop worrying about it, I had a bug bigger
than
> >an elephant, that was all...
>
> So how much is the performance hit now? Just for the record...
>
> --
> Bart.




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

Date: Sun, 31 Dec 2000 09:41:59 -0500
From: Rick <theobvioushattonr@aug.com>
Subject: newbie perl/xchat question
Message-Id: <3A4F45B7.5AC1DF1F@aug.com>

I am VERY new to perl. In fact, Im trying to wrtie my first script - one
for xchat.

I would like to send colored ascii to an irc channel (it is acceptable
on this channel). Here is what I have:

#!/bin/perl 

IRC::register("kilroy2","0.1","",""); 
IRC::add_command_handler("kilroy2", "kilroy2");

sub kilroy2 { 

    IRC::print "   \n";
    IRC::print "         ,,,\n";
    IRC::print "        (o o)\n";     
    IRC::print "----oOO--( )--OOo----\n";
    IRC::print "   \n"; 

   return 1; 
} 

The xchat color code I would like to use is: %C7. How can I do this? I
would also like to embed xchat variables into other scripts. Would the
same technique work?

Any and all help appreciated.

-- 
 
Rick
 
* To email me remove theobvious from my address *


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

Date: Sun, 31 Dec 2000 14:44:55 -0800
From: "GOGAR" <angenent@kabelfoon.nl>
Subject: NEWBIE Q: DEC2HEX?? HOW
Message-Id: <92nd5k$tke$1@news.kabelfoon.nl>

Hmm a lil problem i got at the moment is how to Convert integers to
hexadecimals..  lol, i'm sure it's not that hard.
but i can't seem to find anything about it.
before i start making a gigantic sub i thought i'd post a message in here.

i'm currently trying to make a compression sub with perl.. i just translated
some C code i found to perl

the sub outputs normal characters  and inegers that represent a string,  the
integers should be converted to bit strings of usually14 bits long.     and
then the whole thing has to be reformatted back into 8 bit characters

so i came up with a line like this to generate the bit string...

eval("print join('', unpack('B".$BITS."', \"\\x".$code."\"))");

by this line it is expected that $code is a hexadecimal..

isn't there another way to do this?    and if not, a way to convert decimals
to hexadecimals will probabaly do the trick for me.

thanx in advance,
Chris




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

Date: Sun, 31 Dec 2000 15:22:05 -0800
From: "GOGAR" <angenent@kabelfoon.nl>
Subject: Re: NEWBIE Q: DEC2HEX?? HOW
Message-Id: <92nfbf$12n4$1@news.kabelfoon.nl>

WHOOHOO LMFAO

i just found 2 subs i made  a year ago or something.. numberconv() and
numberbackconv()
it converts an integer to anything..   can also be used as encryption or
just for fun lol

sub numberconv() {
      my($dec,$chars,$minchars)=@_;
$hexnumberz="";
while($dec>0) {
$hexsubstr=$dec % length($chars);
$dec=($dec-$hexsubstr)/length($chars);
$hexnumberz2=substr($chars,$hexsubstr,1);
$hexnumberz="$hexnumberz2$hexnumberz"};
while($minchars>length($hexnumberz)){$hexnumberz=substr($chars,0,1)."$hexnum
berz";}
return $hexnumberz}


sub numberbackconv() {
      my($word,$chars)=@_;
$word=reverse($word);
$word=~ tr/[a-z]/[A-Z]/;
$chars=~ tr/[a-z]/[A-Z]/;
@letters=split(/ */,$word);
$bungholio=length($chars);
$cornholio=1;
$dec=0;
foreach $letter (@letters){
$dec=$dec+($cornholio*index($chars,$letter));
$cornholio=$cornholio*$bungholio;
}
return $dec}


hence the use of superb var names

here are some examples of numberconv()  be careful not to enjoy yourself too
much here

numberconv(255,"01",8);
would return 11111111 i hope

numberconv(255,"0123456789ABCDEF",2);
would return FF i hope


"GOGAR" <angenent@kabelfoon.nl> wrote in message
news:92nd5k$tke$1@news.kabelfoon.nl...
> Hmm a lil problem i got at the moment is how to Convert integers to
> hexadecimals..  lol, i'm sure it's not that hard.
> but i can't seem to find anything about it.
> before i start making a gigantic sub i thought i'd post a message in here.
>
> i'm currently trying to make a compression sub with perl.. i just
translated
> some C code i found to perl
>
> the sub outputs normal characters  and inegers that represent a string,
the
> integers should be converted to bit strings of usually14 bits long.
and
> then the whole thing has to be reformatted back into 8 bit characters
>
> so i came up with a line like this to generate the bit string...
>
> eval("print join('', unpack('B".$BITS."', \"\\x".$code."\"))");
>
> by this line it is expected that $code is a hexadecimal..
>
> isn't there another way to do this?    and if not, a way to convert
decimals
> to hexadecimals will probabaly do the trick for me.
>
> thanx in advance,
> Chris
>
>




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

Date: 31 Dec 2000 14:58:54 GMT
From: cjw44@flatline.org.uk (Colin Watson)
Subject: Re: NEWBIE Q: DEC2HEX?? HOW
Message-Id: <92nhje$jus$1@riva.ucam.org>

GOGAR <angenent@kabelfoon.nl> wrote:
>Subject: Re: NEWBIE Q: DEC2HEX?? HOW

Please don't shout. :)

>Hmm a lil problem i got at the moment is how to Convert integers to
>hexadecimals..  lol, i'm sure it's not that hard.
>but i can't seem to find anything about it.

  perldoc -f sprintf

 ... is really quite a lot better than the messy functions you posted in
your reply to yourself.

-- 
Colin Watson                                     [cjw44@flatline.org.uk]
Linux: the choice of a GNU generation.


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

Date: Fri, 29 Dec 2000 19:10:03 -0800
From: MarkyMoon <markymoon@zenbydesign.com>
Subject: Re: Newbie with Session Key and META questions
Message-Id: <3A4D520A.35B63467@zenbydesign.com>

Rafael,

Thanks for taking time to answer my questions.  I really wasn't sure if  I had
a workable idea or not.
Mark Smith

Rafael Garcia-Suarez wrote:

-- 

That pesky MarkyMoon guy


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

Date: Wed, 03 Jan 2001 20:39:26 +0100
From: Achim Finger <Achim.Finger@t-online.de>
Subject: NEWBIE: Perl and Expect.pm
Message-Id: <3A537FEE.D807CBD4@t-online.de>

Hello perl users,

I have got problems using the Expect.pm with perl:

Here is the code:

--------------------------------------------------------------
#!/usr/bin/perl
use Expect;

$host = $ENV{'HOSTNAME'};
$username = $ENV{'USER'};

($ftp = Expect->spawn("ftp $hostname")) || die "Couldn't spawn ftp, $!";

unless ($ftp->expect(30,"Name ($hostname:$username): ")) {
  die "Never got username prompt on $hostname, ".$ftp->exp_error()."\n";
}
--------------------------------------------------------------

if I try to run this script it produces the follwoing output:

Expect: Could not assign a pty at
/usr/lib/perl5/site_perl/5.005/Expect.pm line 60.


this happens on a PIII with
Linux 2.2.13
perl version 5.005_03
Expect.pm 1.07


Any ideas
thanx in advance
Achim Finger


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

Date: Sat, 30 Dec 2000 11:29:56 -0800
From: "Mark@MarkNW.com" <mark@marknw.com>
Subject: Newby scrpt help
Message-Id: <mHq36.11495$XW4.469824@nntp1.onemain.com>

After scanning the FAQ and reading all relative posts, I can't seem to find
an answer (perhaps I am simply to inexperienced to recognize it?) to my
problem. While the issue is not strictly PERL, the code is, therefore my
post here; if it's inappropriate please redirect me.

The following code is part of a larger script, but file writes and testing
(as best I know how) verify it to this point. I believe it's here the
misconfiguration error is generated.

The #WRITE TO USERS section writes to .group as expected, but somewhere
after that... error. No new password entry, no redirect, and when I suffix
'die' with "<HTML>...</HTML>" I get nothing. I've checked the permissions
826 times (there always the same- R/W enabled), tried commenting out
questionable lines, checked perl man (is there an unencrypted version of
this for newbys?) on crypt... The only other thing I can think to mention is
that I'm on a remote server (TELEPORT) running PERL 5+.

Oh yeah, the question. Is this a problem with my code? If so, what? If not,
what else might be the problem?

#WRITE TO USERS
  open(GRP, ">>dir/otherdir/.group") || die;
  flock(GRP, 2);
  print GRP "my-users: $NewUser\n";
  flock(GRP,8);
  close (GRP);

# CRYPT PASSWORD
  $NewPass =~ /(.*)/;
  $utPass = $1;
  $Cryptwd = crypt($utPass, $ST);

# WRITE TO PASS FILE
  open(PWD, ">>dir/otherdir/.password") || die;
  flock(PWD, 2);
  print PWD "$NewUser: $Cryptwd\n";
  flock(PWD,8);
  close (PWD);

# REDIRECT TO MEMBER WELCOME
  $redirect= "http://www.marknw.com/dir/otherdir/newmem.html;
  print("location:$redirect\n\n");
exit (0);






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

Date: 30 Dec 2000 13:47:18 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: Newby scrpt help
Message-Id: <87n1ddpu7t.fsf@limey.hpcc.uh.edu>

>> On Sat, 30 Dec 2000 11:29:56 -0800,
>> "Mark@MarkNW.com" <mark@marknw.com> said:

> # REDIRECT TO MEMBER WELCOME
> $redirect= "http://www.marknw.com/dir/otherdir/newmem.html;

This doesn't even compile.  PLEASE post real code, not stuff you've
retyped in and/or changed from the original.

    #!/path/to/perl -wT
    use strict;

Perl wants to help you, let it! :-)

> print("location:$redirect\n\n");

perldoc CGI -- see the redirect() method.

As for the other problems, see

    http://www.boutell.com/openfaq/cgi/

esp. ID:8

You also want the "die"s to be more informative.  Which file couldn't
be opened?  What does $! have in it?  (i.e. why didn't the system open
it?).

Don't forget

    use CGI::Carp qw(fatalsToBrowser);

to get the die messages displayed in the browser.

hth
t
-- 
Eih bennek, eih blavek.


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

Date: Sat, 30 Dec 2000 21:12:31 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: Newby scrpt help
Message-Id: <3ds36.53$zp4.5091@eagle.america.net>

On Sat, 30 Dec 2000 11:29:56 -0800, Mark@MarkNW.com <mark@marknw.com>
wrote:

>#WRITE TO USERS
>  open(GRP, ">>dir/otherdir/.group") || die;
>  flock(GRP, 2);
>  print GRP "my-users: $NewUser\n";
>  flock(GRP,8);
>  close (GRP);

I feel the need to add to Tony Curtis' excellent follow-up even though
I might not be looking at Real Code.  

Your code has a race condition.  Maybe.  This from the perlfunc manual
page on flock() may save you: 

    To avoid the possibility of miscoordination, Perl now flushes
    FILEHANDLE before locking or unlocking it.        ^^^

Between the time that you free the lock and the time that the close is
executed, there is a window of opportunity for a waiting process to
obtain an exclusive lock *before* your changes are actually written to
the file.  The close() will cause the buffers to be flushed *after*
freeing the lock.  (Unless your perl saved you.)  

Always close a file that you flock'ed to free the lock.  That insures
an atomic operation no matter what version you're using.  

Speaking of close(), what happens if there's no space left on the
device you're writing to?  Almost always the only way to know that is
to check the result of closing an output file.  

Also, use the symbolic constants available in the Fcntl module instead
of magic numbers.  

    use warnings;
    use strict;
    use Fcntl ':flock'; # import LOCK_* constants

    open(GRP, ">> $file") || die "can't open $file: $!";
    flock(GRP, LOCK_EX)   || die "can't lock $file: $!";
    print GRP, "stuff\n";
    # Check close in case "no space on device..." error
    close(GRP)            || die "close $file: $!";

Lot's of help is awaiting in the perlopentut, perlfaq5 ("How can I
lock a file?") and perlfunc (flock) manual pages.  

-- 
Garry Williams


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

Date: Sat, 30 Dec 2000 14:42:31 -0800
From: "Mark@MarkNW.com" <mark@marknw.com>
Subject: Re: Newby scrpt help
Message-Id: <Xvt36.11532$XW4.512514@nntp1.onemain.com>

Sorry, I was only trying to keep the post pertinent and on a PERL topic. the
script runs without error to the point which I started the previous post.
But if it helps, I'm willing to post the whole thing. The only change in the
original was the path names (which I kept relative).

Even with the additional debugging help the error is cryptic (fitting I
suppose): Execution of adminpgThree.pl aborted due to compilation errors
when run from the browser, from the command line I get a hefty list of
errors, ALL are 'requires specific package name'. It nice to know how to
direct errors to my browser though... thanks!

Thanks to Gary's response I'm checking on the FLOCK, but as I'm the only
user at this point, I doubt it's anything specific to multiple calls or
instances.

#!/usr/bin/perl -wT
use strict;
use CGI::Carp qw(fatalsToBrowser);
require("cgi-lib.pl");
&ReadParse;
use Env;

  $Per[0] = $in{CC1};
  $Per[1] = $in{CC2};
  $Per[2] = $in{CC3};
  $Per[3] = $in{CC4};
  $Per[4] = $in{EXMO};
  $Per[5] = $in{EXYR};
  $Per[6] = $in{CCHOLD};
  $Per[7] = $in{CARD};
  $ID =      $in{PROFILE};
  $NewUser= $in{USER};
  $NewPass= $in{PASS1};
  $NewPass2= $in{PASS2};
  $Retry= $in{RETRY};

 for ($i=0; $i<8; $i++) {
    chomp($Per[$i]);
  }

chomp($ID);
chomp($NewUser);
chomp($NewPass);
chomp($NewPass2);

# BAD REFERRER
  unless($ENV{'SCRIPT_FILENAME'} eq '/homepath/dir/adminpgThree.pl') {
    # LOG ILLEGAL

    # REDIRECT
    $redirect= "http://www.marknw.com/dir/dir1/badrefer.html;
    print("location:$redirect\n\n");
  }

#VERIFY/FILTER INPUT
#FILTER ILLEGAL CHARACTERS
  $NewPass2=~ tr/A-Za-z0-9_/X/c;

#MATCH PASSWORDS
  if ($NewPass ne $NewPass2) {
    $redirect= "http://www.marknw.com/dir/dir1/regfail.html;
    print("location:$redirect\n\n");
  }

  if ($Retry ne "YES") {
    $CCno = $Per[0] . "-" . $Per[1] . "-" . $Per[2] . "-" . $Per[3];
    $CCexp = $Per[4] . "/" . $Per[5];
# OPEN PAGE ONE
    open(PAGE1,"records/" . $ID . ".tmp") || die;
    $P1 = <PAGE1>;
    chomp($P1);
    close (PAGE1);

# ADD CC DATA TO TEMP
    $member='';
    $member= $CCno . "\t" . $CCexp . "\t" . $Per[7] . "\t" . $Per[6];
    $member=~ tr/A-Za-z0-9\._,\ \@\-\#\%\t/A-Za-z0-9\._,\ \@\-\#\%\t/cd;
    $member= $P1 . $member;

#WRITE MEMBER TO TEMP
  open(TEMP,">records/" . $ID . ".tmp");
  print TEMP "$member\n";
  close (TEMP);
}

#CHECK FOR DUP USER
  open(MEMBER, 'secure/scripts/data/members.txt') || die;
    flock(MEMBER,2);
    $MemLine= <MEMBER>;
    chomp($MemLine);
    while ($MemLine) {
      (@MemList)=split /\t/, $MemLine;
      if ($NewUser eq $MemList[33]) {
#YES
        flock(MEMBER,8);
        close (MEMBER);
#OUTPUT RETRY
print "Content-type: text/html\n\n";
print <<EOF;
  <HTML>
    <HEAD><TITLE>XX</TITLE>
    </HEAD>
    <BODY>
      USER NAME USED, USE ANOTHER
    </BODY>
  </HTML>
EOF
        exit (0);
      }
    $MemLine= <MEMBER>;
    chomp($MemLine);
    }
  flock(MEMBER,8);
  close (MEMBER);

#NO DUPLICATE
#RETRIVE USER DATA
#OPEN TEMP

### OPEN TEMP
    open(PG, "records/" . $ID . ".tmp") || die ;
    $P1 = <PG>;
    chomp($P1);

($TI,$FN,$MI,$LN,$B1,$A1,$A2,$CY,$ST,$ZP,$P1,$E1,$P2,$E2,$FX,$EM,$LC,$HR,$PD
,$GB,$GA,$GD,$GC,$GS,$GZ) = split/\t/,$P1;
    close (PG);

  #WRITE TO MEMBERS
#format $NewMember
  $NewMember = $TI . "\t" . $FN . "\t" . $MI . "\t" . $LN . "\t" . $B1 .
"\t" . $A1 . "\t" . $A2 . "\t" . $CY . "\t" . $ST . "\t" . $ZP . "\t" . $P1
 . "\t" . $E1 . "\t" . $P2 . "\t" . $E2 . "\t" . $FX . "\t" . $EM . "\t" .
"B" . "\t" . "B" . "\t" . "B" . "\t" . $CCno . "\t" . $Per[7] . "\t" .
$CCexp. "\t" . "B" . "\t" . $Per[6] . "\t" . $LC . "\t" . $HR . "\t" . $PD .
"\t" . $GB . "\t" . $GA . "\t" . $GD . "\t" . $GC . "\t" . $GS . "\t" . $GZ
 . "\t" . $NewUser;
  open(MEM, ">>secure/scripts/data/members.txt") || die ;
  flock(MEM,2);
  print MEM "$NewMember\n";
  flock(MEM,8);
  close (MEM);

# DELETE TEMP
#  unlink "records/" . $ID . ".tmp";

#WRITE TO USERS
  open(GRP, ">>secure/otherdir/.htgroup") || die;
  flock(GRP, 2);
  print GRP "my-users: $NewUser\n";
  flock(GRP,8);
  close (GRP);

# CRYPT PASSWORD
  $NewPass =~ /(.*)/;
  $utPass = $1;
  $Cryptwd = crypt($utPass, $ST);

# WRITE TO PASS FILE
  open(PWD, ">>secure/otherdir/.htpasswd") || die
"<HTML><HEAD><TITLE>XX</TITLE></HEAD><BODY>ER:$!</BODY></HTML>";
  flock(PWD, 2);
  print PWD "$NewUser: $Cryptwd\n";
  flock(PWD,8);
  close (PWD);

# REDIRECT TO MEMBER WELCOME
  $redirect= "http://www.marknw.com/dir/dir1/newmem.html;
  print("location:$redirect\n\n");
exit (0);







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

Date: Sat, 30 Dec 2000 23:25:25 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Newby scrpt help
Message-Id: <x7elypa3vh.fsf@home.sysarch.com>

>>>>> "Mc" == Mark@MarkNW com <mark@marknw.com> writes:

  Mc> #!/usr/bin/perl -wT
  Mc> use strict;


  Mc> use CGI::Carp qw(fatalsToBrowser);
  Mc> require("cgi-lib.pl");

huh? you have access to CGI.pm (i would assume since you have CGI::Carp)
and you still use cgi-lib? and CGI.pm has a cgi-lib compatibility mode.

  Mc> &ReadParse;
  Mc> use Env;

  Mc>   $Per[0] = $in{CC1};
  Mc>   $Per[1] = $in{CC2};
  Mc>   $Per[2] = $in{CC3};
  Mc>   $Per[3] = $in{CC4};
  Mc>   $Per[4] = $in{EXMO};
  Mc>   $Per[5] = $in{EXYR};
  Mc>   $Per[6] = $in{CCHOLD};
  Mc>   $Per[7] = $in{CARD};

clean that up some.

	@Per = @in{ qw( CC1 CC2 CC3 CC4 EXMO EXYR CHOLD CARD ) } ;

  Mc>  for ($i=0; $i<8; $i++) {
  Mc>     chomp($Per[$i]);
  Mc>   }

chomp works on lists or arrays:

	chomp @Per ;

also in common perl style don't use capital letters in normal variable
names. especially studly caps like you use below.

  Mc> chomp($ID);
  Mc> chomp($NewUser);
  Mc> chomp($NewPass);
  Mc> chomp($NewPass2);

and why are you chomping all those cgi params? you should really trim
leading and trailing white space

  Mc> # BAD REFERRER

comments in all upper case are hard to read.

  Mc>   unless($ENV{'SCRIPT_FILENAME'} eq '/homepath/dir/adminpgThree.pl') {
  Mc>     # LOG ILLEGAL

  Mc>     # REDIRECT
  Mc>     $redirect= "http://www.marknw.com/dir/dir1/badrefer.html;
  Mc>     print("location:$redirect\n\n");

since this is the end of the cgi execution (you don't do anything after a
redirect), do an exit() here.

  Mc>   }

  Mc> #MATCH PASSWORDS
  Mc>   if ($NewPass ne $NewPass2) {
  Mc>     $redirect= "http://www.marknw.com/dir/dir1/regfail.html;
  Mc>     print("location:$redirect\n\n");

again, put an exit here.

  Mc>   }

  Mc>   if ($Retry ne "YES") {
  Mc>     $CCno = $Per[0] . "-" . $Per[1] . "-" . $Per[2] . "-" . $Per[3];
  Mc>     $CCexp = $Per[4] . "/" . $Per[5];

join or interpolate. much cleaner

	$CCno = join( '-', @Per[0 .. 3] ) ;
	$CCexp = "$Per[4]/$Per[5]" ;

  Mc> # OPEN PAGE ONE
  Mc>     open(PAGE1,"records/" . $ID . ".tmp") || die;

interpolate. using . all the time is hard to read.
so why did you fail here? put $! in your die message along with the file
name

	my $file = "records/$ID.tmp" ;
	open(PAGE1, $file) || die "can't open $file $!" ;

  Mc> # ADD CC DATA TO TEMP
  Mc>     $member='';
  Mc>     $member= $CCno . "\t" . $CCexp . "\t" . $Per[7] . "\t" . $Per[6];

interpolate or join

  Mc>     $member=~ tr/A-Za-z0-9\._,\ \@\-\#\%\t/A-Za-z0-9\._,\ \@\-\#\%\t/cd;

no need to \ most regular chars in tr. also since the replacement list is the
same as the translate (left side) list of char, you can eliminate the
right side.

	$member=~ tr/A-Za-z0-9._, @\-#%\t//cd;

  Mc>     $member= $P1 . $member;

interpolate. 

  Mc> #WRITE MEMBER TO TEMP
  Mc>   open(TEMP,">records/" . $ID . ".tmp");

always check the result of open


  Mc> #CHECK FOR DUP USER
  Mc>   open(MEMBER, 'secure/scripts/data/members.txt') || die;
  Mc>     flock(MEMBER,2);

don't hardwarie flock opcode. use the Fctnl module for the symbolic
names. they are not the same on all systems.

  Mc>     $MemLine= <MEMBER>;
  Mc>     chomp($MemLine);
  Mc>     while ($MemLine) {
  Mc>       (@MemList)=split /\t/, $MemLine;

no need for () around @MemList. arrays provide list context already.

  Mc>     open(PG, "records/" . $ID . ".tmp") || die ;

use a proper die message (as i did above)


  Mc> ($TI,$FN,$MI,$LN,$B1,$A1,$A2,$CY,$ST,$ZP,$P1,$E1,$P2,$E2,$FX,$EM,$LC,$HR,$PD
  Mc> ,$GB,$GA,$GD,$GC,$GS,$GZ) = split/\t/,$P1;

that is UGLY! use a hash or an array or something other than that many vars.


  Mc>   $NewMember = $TI . "\t" . $FN . "\t" . $MI . "\t" . $LN . "\t" . $B1 .
  Mc> "\t" . $A1 . "\t" . $A2 . "\t" . $CY . "\t" . $ST . "\t" . $ZP . "\t" . $P1
  Mc> . "\t" . $E1 . "\t" . $P2 . "\t" . $E2 . "\t" . $FX . "\t" . $EM . "\t" .
  Mc> "B" . "\t" . "B" . "\t" . "B" . "\t" . $CCno . "\t" . $Per[7] . "\t" .
  Mc> $CCexp. "\t" . "B" . "\t" . $Per[6] . "\t" . $LC . "\t" . $HR . "\t" . $PD .
  Mc> "\t" . $GB . "\t" . $GA . "\t" . $GD . "\t" . $GC . "\t" . $GS . "\t" . $GZ
  Mc> . "\t" . $NewUser;

JOIN!! that is unreadable and very prone to bugs. since you never seem
to do anything with each element, why not use an array?

  Mc>   open(MEM, ">>secure/scripts/data/members.txt") || die ;

die message

  Mc>   flock(MEM,2);

flock opcodes

  Mc> #WRITE TO USERS
  Mc>   open(GRP, ">>secure/otherdir/.htgroup") || die;

again die message.


  Mc>   flock(GRP, 2);

think about doing a single flock for the entire script. this code
doesn't do much and flocking each time is more work than you need to
do. 

  Mc>   open(PWD, ">>secure/otherdir/.htpasswd") || die

die message

  Mc>   $redirect= "http://www.marknw.com/dir/dir1/newmem.html;
  Mc>   print("location:$redirect\n\n");
  Mc> exit (0);

here you exit after a redirect but you don't even need it. the script
will exit here anyway.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Sat, 30 Dec 2000 19:08:04 -0700
From: "Doran Barton" <fozz@iodynamics.com>
Subject: Re: Newby scrpt help
Message-Id: <92m49q$arb$1@news.xmission.com>

In article <mHq36.11495$XW4.469824@nntp1.onemain.com>, "Mark@MarkNW.com"
<mark@marknw.com> wrote:

> The #WRITE TO USERS section writes to .group as expected, but somewhere
> after that... error. No new password entry, no redirect, and when I
> suffix
> 'die' with "<HTML>...</HTML>" I get nothing. I've checked the
> permissions
> 826 times (there always the same- R/W enabled), tried commenting out
> questionable lines, checked perl man (is there an unencrypted version of
> this for newbys?) on crypt... The only other thing I can think to
> mention is that I'm on a remote server (TELEPORT) running PERL 5+.
> 
> Oh yeah, the question. Is this a problem with my code? If so, what? If
> not, what else might be the problem?
> 
> #WRITE TO USERS
>   open(GRP, ">>dir/otherdir/.group") || die; flock(GRP, 2); print GRP
>   "my-users: $NewUser\n"; flock(GRP,8); close (GRP);
> 
> # CRYPT PASSWORD
>   $NewPass =~ /(.*)/;
>   $utPass = $1;
>   $Cryptwd = crypt($utPass, $ST);
> 
> # WRITE TO PASS FILE
>   open(PWD, ">>dir/otherdir/.password") || die; flock(PWD, 2); print PWD
>   "$NewUser: $Cryptwd\n"; flock(PWD,8); close (PWD);
> 
> # REDIRECT TO MEMBER WELCOME
>   $redirect= "http://www.marknw.com/dir/otherdir/newmem.html;
>   print("location:$redirect\n\n");
> exit (0);

First of all, the 'die' function in Perl writes it's message to STDERR,
not to STDOUT. So, assuming you're using this as a CGI script (it sure
likes part of one), you should find your die() text in the server
error log file. 

Instead of calling die (or in addition to it), you should use print() to
send some interesting information to the HTTP client.

Now, as for .htaccess-style password file management, I would like to say
that you are probably trying to hard in rolling your own solution here.
I know, I did that too. There are two very good CPAN modules for managing
basic HTTP authentication password/group files.

One of these modules is HTTPD::User_Manage. The other is
Apache::Htpasswd. If you use one of these, you can save yourself time and
effort.

Also, please consider using the CGI.pm module (also available via CPAN)
for generating HTML and CGI/HTTP headers (e.g. Location: header). 

-=Fozz

-- 
------------------------------------------------------------------------------
fozz@iodynamics.com -- Doran L. Barton - Chief Super Hero, Iodynamics LLC
    "A good messenger expects to get shot." -- Larry Wall


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

Date: Sat, 30 Dec 2000 22:00:20 -0700
From: Nawft <nawft@mindspring.com>
Subject: Re: Newby scrpt help
Message-Id: <3A4EBD64.7058760E@mindspring.com>

"Mark@MarkNW.com" wrote:
> 
> After scanning the FAQ and reading all relative posts, I can't seem to find
> an answer (perhaps I am simply to inexperienced to recognize it?) to my
> problem. While the issue is not strictly PERL, the code is, therefore my
> post here; if it's inappropriate please redirect me.
> 
> The following code is part of a larger script, but file writes and testing
> (as best I know how) verify it to this point. I believe it's here the
> misconfiguration error is generated.
> 

> 
> # REDIRECT TO MEMBER WELCOME
>   $redirect= "http://www.marknw.com/dir/otherdir/newmem.html;
>   print("location:$redirect\n\n");
> exit (0);
Perhaps I'm wrong here, but shouldn't there be a closing quote on the
$redirect= line?


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

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 5235
**************************************


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