[22366] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4587 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 19 18:11:08 2003

Date: Wed, 19 Feb 2003 15:10:15 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 19 Feb 2003     Volume: 10 Number: 4587

Today's topics:
    Re: looping through arrays (Anno Siegel)
    Re: Need Help with Pattern Matching <marc.beyer@gmx.li>
        Paging DBI result sets (was: Re: Returning the field li <jeff@vpservices.com>
        Perl, Large Memory, and autofork <mthunter@students.uiuc.edu>
    Re: Perl, Large Memory, and autofork (Anno Siegel)
    Re: Perl, Large Memory, and autofork <mthunter@students.uiuc.edu>
    Re: Perl, Large Memory, and autofork (Anno Siegel)
    Re: Perl, Large Memory, and autofork <abigail@abigail.nl>
    Re: pipe() + fork() + exit() == problem? (Anno Siegel)
    Re: pipe() + fork() + exit() == problem? <gordan@_NOSPAM_bobich.net>
        Religious question: commenting <mr@sandman.net>
    Re: Religious question: commenting <tassilo.parseval@post.rwth-aachen.de>
    Re: Religious question: commenting <REMOVEsdnCAPS@comcast.net>
    Re: Religious question: commenting <mr@sandman.net>
    Re: Religious question: commenting <mr@sandman.net>
    Re: Religious question: commenting (Tad McClellan)
    Re: Religious question: commenting <mr@sandman.net>
    Re: Religious question: commenting <tassilo.parseval@post.rwth-aachen.de>
    Re: Religious question: commenting <tassilo.parseval@post.rwth-aachen.de>
        Simple regex question... (Sam)
    Re: Simple regex question... <mpapec@yahoo.com>
    Re: Simple regex question... <mpapec@yahoo.com>
    Re: Simple regex question... <bob@fourtheye.REMOVEME.org>
    Re: Simple regex question... <REMOVEsdnCAPS@comcast.net>
    Re: Unsuccesful "Print" (Jay Tilton)
    Re: use DBI; <abigail@abigail.nl>
    Re: Variables from 'require' not initialized <j.j.konkle-parker@larc.nasa.gov>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 19 Feb 2003 19:10:11 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: looping through arrays
Message-Id: <b30kqj$hp7$1@mamenchi.zrz.TU-Berlin.DE>

roach_01 <roach01@cfl.rr.com> wrote in comp.lang.perl.misc:
> Hello!
> 
> I worling on a script that parses log files and finds start and end times
> for
> servers.
> 
> My problem is when more than one occurances happen for the same server.
> What happens is my script only catches the first occurance.
> 
> ...
> foreach $b (keys %backup) {
>     $id = $backup{$b};
>     @1=grep/Node: $b)  Elapsed/ .. /time:/g,@log;
      ^^
Ugh.  The variable @1 is a system variable and better left alone.  The
system doesn't use it, but it might some day. (It only exists because
its cousins, the regex variables $1, $2, etc exist.)  Use a normal
variable name.

> }
> 
> &parse_times if @1;

Don't call subs with a leading ampersand, unless you know what you're
doing.  Write "parse_times()" instead.

> 
> }
> 
> 
> sub parse_times {
>   @end=map { "@{[ (split)[1]]}" }$1[0] ;
>   @elapse=map { "@{[ (split)[-1] ]}" }$1[1] ;
> 
> .....
> }

The code in parse_times is horrible.  The first line does exactly the
same as

    @end = ( split ' ', $1[0])[1];

which is a bad replacement for

    (undef, $end) = split ' ', $1[0];

The second line is similar.  map() does nothing useful there, nor does
the artful interpolation of a function call into a string.

> Now if @log has more than one instance of the server like this:
> 
> 02/05/03 12:38:34 Node: SERVER1) Elapsed
> processing time: 00:56:36
> 02/05/03 16:38:03 Node: SERVER1) Elapsed
> processing time: 00:45:07
> 02/05/03 20:35:18 Node: SERVER1) Elapsed
> processing time: 00:22:48

> It would just pull the first.
> Can anyone point me in the right direction on
> looping through the array to find more than one of the same in the
> array?

There is essential information missing.  With the data you provided,
there are several trivial ways to do what you want.  The question is,
how can these lines be mixed with other log data?  Are there similar lines
for other servers?  Other, unrelated, lines?  Can something go between
the "node:" line and the corresponding "processing time:" line?  If
so, how do you know which ones correspond?  If not, why does your code
try to provide for it (the use of "..")?

I'm afraid, that code of yours is beyond repair.  You are using a number
of rather advanced constructs, but show no sign of understanding what
they do.

Start again.  Build your program under "strict" and "warnings", and
use only those parts of the language you understand.  (You may have
to learn about some parts you don't understand yet.)  Also, before
coding, specify precisely what kind of data the program shall have
to deal with.  Good luck.

Anno


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

Date: Wed, 19 Feb 2003 21:54:03 +0000
From: Marc <marc.beyer@gmx.li>
Subject: Re: Need Help with Pattern Matching
Message-Id: <b30ub8$1grhqp$1@ID-123680.news.dfncis.de>


> Thanks Marc, but I asked for it to be sorted by height! (ascending)

Ack. Mea culpa, that's what you get for doing stuff at three in the morning 
;-)

This will do what your split does:

sub make_sort_1 {
    ($a =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d\.\d+)/)[0]
        <=>
        ($b =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d\.\d+)/)[0];
}

Which produces the output

01.02.1983 100 Blaro Van Daronimo 4.4
12.01.1963 100 John Smith 5.11
16.03.1977 400 Johnny Barrosh 5.4
09.01.1987 200 Steve McHooter 5.5
27.02.1955 200 Jimmy Dolan Andrews 5.8

However, I assume what you actually want to have is this:

01.02.1983 100 Blaro Van Daronimo 4.4
16.03.1977 400 Johnny Barrosh 5.4
09.01.1987 200 Steve McHooter 5.5
27.02.1955 200 Jimmy Dolan Andrews 5.8
12.01.1963 100 John Smith 5.11

which can be done via

my ($aa,$ab,$ba,$bb);
sub make_sort_2 {
    ($aa,$ab)= $a =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d)\.(\d+)/;
    ($ba,$bb)= $b =~ m/[0-9.]*\s\d*\s[a-zA-Z ]*\s\w*\s(\d)\.(\d+)/;
    $aa==$ba ? $ab <=> $bb : $aa <=> $ba;
}

As for the things you mentioned in the conversation with Benjamin, once 
you've got the sorted array you can do

my @split;
foreach (@sorted) {
    @split = $_ =~ m/([0-9.]*)\s(\d*)\s([a-zA-Z ]*\s\w*)\s(\d\.\d+)/;
    # do stuff with @split
}

to individually access the columns of data. There's probably a way to do 
this within the make_sort subroutine but I can't think of one right now.

> Incidently, when I tried your idea as well as the following lines:
> use strict;
> use warnings;
> 
> I was getting "use of uninitialized value in string comparison" and the
> data wasn't sorted as expected.

Hmm, my version works fine here, for the space-delimited data that is. If 
you feed it data delimited by the | you will get these warnings cause the 
regex doesn't match and the comparison operator doesn't have anything to 
work on.

Have fun,

Marc


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

Date: Wed, 19 Feb 2003 13:42:09 -0800
From: Jeff Zucker <jeff@vpservices.com>
Subject: Paging DBI result sets (was: Re: Returning the field list from DBI)
Message-Id: <3E53FA31.5040808@vpservices.com>

[snip of good discussion by all]


Tim Bunce's talk at Perl 3.0 covers many of these same issues about paging, see his slideshow at:


    http://dbi.perl.org/doc/conferences/

See also a summary of a discussion Tim had on the subject at:


   http://www.carumba.com/talk/perl/multiview.shtml

-- 
Jeff



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

Date: Wed, 19 Feb 2003 19:06:45 GMT
From: Mike Hunter <mthunter@students.uiuc.edu>
Subject: Perl, Large Memory, and autofork
Message-Id: <slrnb57ljt.eu7.mthunter@ux5.cso.uiuc.edu>

Hi all,

Please forgive me if this is nonsense.  I seem to remember having read
somewhere that perl "auto-forks" to gain access to more memory.  I've
googled a bit, but haven't found any information on that.  Is there any
upper limit to how much memory perl can access aside from that of the 
address space?  I'm asking because my program occasionally runs out of
memory.

The program shoves obscene amounts of data into a hash (7 byte binary
key, listref to 7 scalar/numeric values)...is there anything I should be
considering in trying to keep down memory usage?  I've done my best to
make variables fall out of scope asap.

Thanks,

Mike


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

Date: 19 Feb 2003 19:19:21 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Perl, Large Memory, and autofork
Message-Id: <b30lbp$hp7$2@mamenchi.zrz.TU-Berlin.DE>

Mike Hunter  <mthunter@uiuc.edu> wrote in comp.lang.perl.misc:
> Hi all,
> 
> Please forgive me if this is nonsense.  I seem to remember having read
> somewhere that perl "auto-forks" to gain access to more memory.  I've
> googled a bit, but haven't found any information on that.

It is nonsense.

>                                                           Is there any
> upper limit to how much memory perl can access aside from that of the 
> address space?  I'm asking because my program occasionally runs out of
> memory.

No such limits.  If your machine can address it, Perl can.

> The program shoves obscene amounts of data into a hash (7 byte binary
> key, listref to 7 scalar/numeric values)...is there anything I should be
> considering in trying to keep down memory usage?  I've done my best to
> make variables fall out of scope asap.

A pack/unpack scheme storing the data in a compact string instead of a
listref comes to mind.

Anno


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

Date: Wed, 19 Feb 2003 19:35:28 GMT
From: Mike Hunter <mthunter@students.uiuc.edu>
Subject: Re: Perl, Large Memory, and autofork
Message-Id: <slrnb57n9p.1ol.mthunter@ux5.cso.uiuc.edu>

On 19 Feb 2003 19:19:21 GMT, Anno Siegel wrote:

> > The program shoves obscene amounts of data into a hash (7 byte binary
> > key, listref to 7 scalar/numeric values)...is there anything I should be
> > considering in trying to keep down memory usage?  I've done my best to
> > make variables fall out of scope asap.
>  
>  A pack/unpack scheme storing the data in a compact string instead of a
>  listref comes to mind.

Thanks, that's a good idea.

So, does a listref to a list of 7 scalars actually take up 8*4 bytes in
"pointers", or is it just an extra 4 bytes?  Sorry if there's good
documentation on this, but it seems like such topics are usually
addressed by saying "don't worry about how perl does this" :)

Mike


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

Date: 19 Feb 2003 19:55:50 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Perl, Large Memory, and autofork
Message-Id: <b30ng6$jm6$1@mamenchi.zrz.TU-Berlin.DE>

Mike Hunter  <mthunter@uiuc.edu> wrote in comp.lang.perl.misc:
> On 19 Feb 2003 19:19:21 GMT, Anno Siegel wrote:
> 
> > > The program shoves obscene amounts of data into a hash (7 byte binary
> > > key, listref to 7 scalar/numeric values)...is there anything I should be
> > > considering in trying to keep down memory usage?  I've done my best to
> > > make variables fall out of scope asap.
> >  
> >  A pack/unpack scheme storing the data in a compact string instead of a
> >  listref comes to mind.
> 
> Thanks, that's a good idea.
> 
> So, does a listref to a list of 7 scalars actually take up 8*4 bytes in
> "pointers", or is it just an extra 4 bytes?  Sorry if there's good
> documentation on this, but it seems like such topics are usually
> addressed by saying "don't worry about how perl does this" :)

No, there isn't much documentation about the amount of storage Perl
data structures take.  There are good descriptions of what they contain
and what each part is for, but what a particular compiler with particular
settings on a particular machine will make of it is anybody's guess.

Common sense says that compact strings can be more economic than Perl's
arrangement of SVs and AVs (which are really also SVs), and experience
bears it out.  How much you gain (and what you lose) must be seen.

You can (probably) test it with made-up data strings of the right
length, so you'd only have to estimate the length, not write and
debug a pair of packer/unpacker to see what comes of it.

Then there's PDL...

Anno


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

Date: 19 Feb 2003 20:05:21 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Perl, Large Memory, and autofork
Message-Id: <slrnb57os1.1ea.abigail@alexandra.abigail.nl>

Mike Hunter (mthunter@students.uiuc.edu) wrote on MMMCDLIX September
MCMXCIII in <URL:news:slrnb57n9p.1ol.mthunter@ux5.cso.uiuc.edu>:
{}  On 19 Feb 2003 19:19:21 GMT, Anno Siegel wrote:
{}  
{} > > The program shoves obscene amounts of data into a hash (7 byte binary
{} > > key, listref to 7 scalar/numeric values)...is there anything I should be
{} > > considering in trying to keep down memory usage?  I've done my best to
{} > > make variables fall out of scope asap.
{} >  
{} >  A pack/unpack scheme storing the data in a compact string instead of a
{} >  listref comes to mind.
{}  
{}  Thanks, that's a good idea.
{}  
{}  So, does a listref to a list of 7 scalars actually take up 8*4 bytes in
{}  "pointers", or is it just an extra 4 bytes?  Sorry if there's good
{}  documentation on this, but it seems like such topics are usually
{}  addressed by saying "don't worry about how perl does this" :)


It's actually much more than that:

    $ perl -MDevel::Size -wle 'print Devel::Size::total_size [1 .. 7]'
    228


That's 228 bytes for a reference to a list containing 7 scalars.



Abigail
-- 
perl5.004 -wMMath::BigInt -e'$^V=Math::BigInt->new(qq]$^F$^W783$[$%9889$^F47]
 .qq]$|88768$^W596577669$%$^W5$^F3364$[$^W$^F$|838747$[8889739$%$|$^F673$%$^W]
 .qq]98$^F76777$=56]);$^U=substr($]=>$|=>5)*(q.25..($^W=@^V))=>do{print+chr$^V
%$^U;$^V/=$^U}while$^V!=$^W'


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

Date: 19 Feb 2003 22:06:21 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: pipe() + fork() + exit() == problem?
Message-Id: <b30v4t$pee$1@mamenchi.zrz.TU-Berlin.DE>

 <gordan@_NOSPAM_bobich.net> wrote in comp.lang.perl.misc:
> Hi!
> 
> Got a small problem. I'm trying to write an application that regulates the 
> number of forked children according to it's outstanding workload (within 
> limits, obviously).
> 
> I use pipes to communicate the process queue between the queue process and 
> the worker processes. There is one pipe for writing to the queue, and one 
> pipe for the forked worker threads to read from. This method works very 
> nicely, and I'd like to keep it.
> 
> But, the problems start when it's time for some of the worker  
> processes to die out. As soon as they exit, the pipe they use to read 
> from queue (and probably the one they use to write to the queue, but I 
> haven't checked that yet) seems to flush and re-set. All of it's contents 
> are gone, and now my queued jobs are lost.

[big snip]

> Finally, I've thought about mapping a pair of scalars into a piece of 
> shared memory and using them as a queue. This seems really daft because it 
> kind of re-invents the pipe in a different, less efficient way. OTOH, it 
> could be useful because the size of the "pipe" is now not limited and 
> explicitly set by the file system. Unfortunately, expect it to be quite 
> CPU intensive with the copy-edit-restore required when working with shared 
> memory in Perl...

If you're considering System V IPC, you'd be better off with a message
queue.  If I understand your procedure, one process is feeding one
end of the queue while multiple workers pluck off requests ad lib.
That's one of the applications message queues were designed for.

A socket-based solution would be more portable.  I'm awaiting Benjamin's
suggestions with interest :)

Anno


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

Date: Wed, 19 Feb 2003 22:45:31 +0000
From: Gordan <gordan@_NOSPAM_bobich.net>
Subject: Re: pipe() + fork() + exit() == problem?
Message-Id: <1045694731.217908@auth2.dns.griffin.net.uk>

Anno Siegel wrote:

>  <gordan@_NOSPAM_bobich.net> wrote in comp.lang.perl.misc:
>> Hi!
>> 
>> Got a small problem. I'm trying to write an application that
>> regulates the number of forked children according to it's outstanding
>> workload (within limits, obviously).
>> 
>> I use pipes to communicate the process queue between the queue
>> process and the worker processes. There is one pipe for writing to
>> the queue, and one pipe for the forked worker threads to read from.
>> This method works very nicely, and I'd like to keep it.
>> 
>> But, the problems start when it's time for some of the worker
>> processes to die out. As soon as they exit, the pipe they use to read
>> from queue (and probably the one they use to write to the queue, but
>> I haven't checked that yet) seems to flush and re-set. All of it's
>> contents are gone, and now my queued jobs are lost.
> 
> [big snip]
> 
>> Finally, I've thought about mapping a pair of scalars into a piece of
>> shared memory and using them as a queue. This seems really daft
>> because it kind of re-invents the pipe in a different, less efficient
>> way. OTOH, it could be useful because the size of the "pipe" is now
>> not limited and explicitly set by the file system. Unfortunately,
>> expect it to be quite CPU intensive with the copy-edit-restore
>> required when working with shared memory in Perl...
> 
> If you're considering System V IPC, you'd be better off with a message
> queue.  If I understand your procedure, one process is feeding one
> end of the queue while multiple workers pluck off requests ad lib.

Pretty much. These workers also push new jobs they identify into a pipe
to the queue process. The reason for this is that the queue can quite
easily exceed the pipe size, in which case the whole lot deadlocks when
everything has blocked, waiting for something else.

> That's one of the applications message queues were designed for.
> 
> A socket-based solution would be more portable.

Well, that's arguable. It would only be portable to systems that support
UNIX sockets.

The problem with switching to using sockets is that is that I am not
totally sure that a sockets based solution would not suffer the same
problem that I'm having with pipes (i.e. flush on exit()/fork() of any
child).

> I'm awaiting Benjamin's suggestions with interest :)

I am, too. :-)

It was his suggestions on one of my earlier apps that made me see the
light of using pipes (elegant, fast, simple). Unfortunately, now it
comes back to bite me on a different program...

Thanks.

Gordan


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

Date: Wed, 19 Feb 2003 21:26:43 +0100
From: Sandman <mr@sandman.net>
Subject: Religious question: commenting
Message-Id: <mr-F55FF2.21264319022003@news.fu-berlin.de>

How do you comment code? What do you feel is the Right Way To Do It?
Let's say I have a function for something in my script. I would do it like this:

    #############################################################
    # Create hidden fields for the given keys
    # Usage: &hidden("id,key,passwd");
    # Requires: &parse()
    #############################################################

    sub hiddens {
        if (%in eq undef){
            # first, we won't do anything if &parse() hasn't been run
            # yet, since it won't work.
            print STDERR "You can't run hiddens() without running parse()\n";
            exit;
        }
        @fields = split ",", $_[0];         # read comma-delimited list
        foreach (@fields){
            if ($in{$_} ne ""){             # only print it if exists
                print qq{<input type="hidden" name="$_" value="$in{$_}" />";
                push @hiddenfields, $_;     # Save for later
            }
        }
    }

These are the three kind of commenting I am doing. First I do I big block of 
comments associated with the function (or script for that matter, I just choose 
a sub in this case). Then I have comments directly associated with a while(), 
foreach() or if() that I put before any commands in those cases and the last 
type are small comments associated with individual one-liners, and is mostly 
made for clarification so no one (or me) can say "Eh, why is he doing that?" 
later. I try to indent these comments to the same level if they appaer near 
each others.

With the above example, how would you comment? Would you do it basically the 
same or do you have some other über-way of doing it?

-- 
Sandman[.net]


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

Date: 19 Feb 2003 20:47:35 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Religious question: commenting
Message-Id: <b30qh7$t87$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Sandman:

> How do you comment code? What do you feel is the Right Way To Do It?
> Let's say I have a function for something in my script. I would do it like this:
> 
>     #############################################################
>     # Create hidden fields for the given keys
>     # Usage: &hidden("id,key,passwd");
>     # Requires: &parse()
>     #############################################################
> 
>     sub hiddens {
>         if (%in eq undef){

Not comma-related, but: The above is not quite right. If you want to
test for definedness, use defined():
    
    if (defined %in) {

But are you sure that this is what you want? defined() on aggregates is
strange since it tests whether there has ever been memory allocated for
this variable. You probably want:

    if (%in) {  # anything in the hash?
    
>             # first, we won't do anything if &parse() hasn't been run
>             # yet, since it won't work.
>             print STDERR "You can't run hiddens() without running parse()\n";
>             exit;

I think this comment makes no sense given the print() that follows. It's
redundant. If code is self-commenting, don't comment it further.

>         }
>         @fields = split ",", $_[0];         # read comma-delimited list

Same here. Also, split() takes a regex as first parameter. It doesn't
change anything here, but it will change if you use "." as split's first
argument. Therefore:

    @fields = split /,/, $_[0];
    
>         foreach (@fields){

You don't need the @fields at all. Simply:

    foreach (split /,/, $_[0]) {

>             if ($in{$_} ne ""){             # only print it if exists

Here's a discrapency between the code and the comment. You don't check
whether it exists, you check whether it is equal to the empty string.
Not at all the same thing. To make your code match the comment:

    if (exists $in{ $_ }) {

>                 print qq{<input type="hidden" name="$_" value="$in{$_}" />";
>                 push @hiddenfields, $_;     # Save for later
>             }
>         }
>     }
> 
> These are the three kind of commenting I am doing. First I do I big
> block of comments associated with the function (or script for that
> matter, I just choose a sub in this case). Then I have comments
> directly associated with a while(), foreach() or if() that I put
> before any commands in those cases and the last type are small
> comments associated with individual one-liners, and is mostly made for
> clarification so no one (or me) can say "Eh, why is he doing that?"
> later. I try to indent these comments to the same level if they appaer
> near each others.

Don't be too dogmatic on it. Not every while-loop needs a comment. Use
comments to explain why you are doing something and not what you are
doing. The latter is already in the code so don't rehash it in comments.
Apart from that, you are concerned with good commenting. This is not a
bad thing.

> With the above example, how would you comment? Would you do it basically the 
> same or do you have some other über-way of doing it?

I'd probably use less comments. But that's a matter of a) personal taste
and b) complexity of the code. Yours isn't that complex. But I do have
code that does so many things that I added a lot of comments.

Anyway, when you think about comments, don't forget something equally
essential. Use strictures and warnings in your code. You don't seem to
have put them in. So let your scripts start thusly:

    use warnings; # perl >= 5.6.0, else the -w switch in the shebang
    use strict;

This is actually a requirement layed out in the posting guidelines of
this group. If you neither use strictures nor warnings, be prepared fo
some harsh comments. So look them up with 'perldoc strict' and 'perldoc
warnings'.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: Wed, 19 Feb 2003 15:38:33 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Religious question: commenting
Message-Id: <Xns9327A93BFF1BEsdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

Sandman <mr@sandman.net> wrote in
news:mr-F55FF2.21264319022003@news.fu-berlin.de: 

> How do you comment code? What do you feel is the Right Way To Do
It?
> Let's say I have a function for something in my script. I would do
it
> like this: 
> 
>     #############################################################
>     # Create hidden fields for the given keys
>     # Usage: &hidden("id,key,passwd");
>     # Requires: &parse()
>     #############################################################

I used to use a format like this:

    #--->     hidden($in, $key, $passwd);
    # 
    # Create hidden fields for the given keys.
    #
    # Input:
    #    $in     - describe this input
    #    $key    - describe this input
    #    $passwd - describe this input
    #
    # Output:
    #    ...describe parameters which are modified...
    #
    # Returns:
    #    ...describe return value...
    #
    # Side effects:
    #    ...you get the idea...
    #
    # Change history:
    #  2003/02/19  EJR  First version
    #
    sub hidden


Now I use pod:

    =item pairwise

     @c = pairwise {code} @a, @b;

    Applies C<{code}> to each pair of elements of C<@a>, C<@b> in
turn.
    Returns a list of the results of that evaluation.  The pairs are
    assigned to C<$a> and C<$b> before invoking the code.

    Examples:

        @a = (1, 2, 3); @b = (2, 4, 6);
        @c = pairwise {$a + $b} @a, @b;   # returns (3, 6, 9)
        @d = pairwise {$a * $b} @a, @b;   # returns (2, 8, 18)

    =cut

    #--->     @c = pairwise {code} @a, @b;
    #
    # Change History:
    #     06/27/2002  EJR  First version
    sub pairwise (&\@\@)

By the way (this is a pet peeve of mine), don't use & when calling
functions.  :-)

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE+U/k+Y96i4h5M0egRAjE5AJ0Sium/iFxnUiiGqpL/3mZOtwcTWQCeKfmT
uFFQ4d5CwCrOjUEmGACFbOU=
=5F80
-----END PGP SIGNATURE-----


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

Date: Wed, 19 Feb 2003 22:42:48 +0100
From: Sandman <mr@sandman.net>
Subject: Re: Religious question: commenting
Message-Id: <mr-6F8D93.22424819022003@news.fu-berlin.de>

In article <b30qh7$t87$1@nets3.rz.RWTH-Aachen.DE>,
 "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote:

> > With the above example, how would you comment? Would you do it basically 
> > the same or do you have some other über-way of doing it?
> 
> I'd probably use less comments. But that's a matter of a) personal taste and 
> b) complexity of the code. Yours isn't that complex. But I do have code that 
> does so many things that I added a lot of comments.

I was hoping for a reply concerning -style- of commenting rather than amount of 
commenting (even though amount could be considered a style). I snipped all the 
parts of your post that had nothing to do with commenting.

But you're right of course, the more complex the code is, the more comments 
should it require. But I tend to comment more than less, just for the purpose 
of the next guy, since I know what a total pain it is to try to take apart 
someone elses perlscript to try to find out what it is he's doing.

-- 
Sandman[.net]


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

Date: Wed, 19 Feb 2003 22:54:37 +0100
From: Sandman <mr@sandman.net>
Subject: Re: Religious question: commenting
Message-Id: <mr-A8392D.22543719022003@news.fu-berlin.de>

In article <Xns9327A93BFF1BEsdn.comcast@216.166.71.239>,
 "Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote:

> Now I use pod:
> 
>     =item pairwise
> 
>      @c = pairwise {code} @a, @b;
> 
>     Applies C<{code}> to each pair of elements of C<@a>, C<@b> in
> turn.
>     Returns a list of the results of that evaluation.  The pairs are
>     assigned to C<$a> and C<$b> before invoking the code.
> 
>     Examples:
> 
>         @a = (1, 2, 3); @b = (2, 4, 6);
>         @c = pairwise {$a + $b} @a, @b;   # returns (3, 6, 9)
>         @d = pairwise {$a * $b} @a, @b;   # returns (2, 8, 18)
> 
>     =cut
> 
>     #--->     @c = pairwise {code} @a, @b;
>     #
>     # Change History:
>     #     06/27/2002  EJR  First version
>     sub pairwise (&\@\@)

Ok, I think I've looked into that before. The reason to do this is what? You 
can read the comments in the file with an external reader, right+ Can't 
remember if I choose not to do it because my editor of choice didn't highlight 
stuff correctly when I did so, or if I never got a hang of it.

-- 
Sandman[.net]


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

Date: Wed, 19 Feb 2003 16:08:30 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Religious question: commenting
Message-Id: <slrnb5802u.1vi.tadmc@magna.augustmail.com>

Sandman <mr@sandman.net> wrote:

> What do you feel is the Right Way To Do It?


My way.  :-)


>     # Usage: &hidden("id,key,passwd");


That should be:

   # Usage: hidden("id,key,passwd");

unless you know what the difference is (see perlsub.doc).


>         if (%in eq undef){


That does not do what you think it does.

What were you hoping to accomplish there?


>             # first, we won't do anything if &parse() hasn't been run
>             # yet, since it won't work.
>             print STDERR "You can't run hiddens() without running parse()\n";


Restating in comments what is already stated in code is redundant (ie. bad).


>         @fields = split ",", $_[0];         # read comma-delimited list
>         foreach (@fields){


Why the temporary variable?

You can loop over the list returned from split() just as
well as the list stored in @fields.


>             if ($in{$_} ne ""){             # only print it if exists


That doesn't do what your comment says it does, so no comment
would be better in this case.

   if ( length $in{$_} ) { # only print if there is something to print
or
   if ( exists $in{$_} ) { # only print it if exists

The second one is another redundant comment now...


> These are the three kind of commenting I am doing. First I do I big block of 
> comments associated with the function (or script for that matter, I just choose 
> a sub in this case). 


Consider using embedded POD for those instead.


> made for clarification so no one (or me) can say "Eh, why is he doing that?" 


_Now_ you're talking.

Comments should not generally say _what_ is being done, the
code already says that.

Comments should say _why_ something is being done.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Wed, 19 Feb 2003 23:14:05 +0100
From: Sandman <mr@sandman.net>
Subject: Re: Religious question: commenting
Message-Id: <mr-528C40.23140519022003@news.fu-berlin.de>

In article <slrnb5802u.1vi.tadmc@magna.augustmail.com>,
 tadmc@augustmail.com (Tad McClellan) wrote:

> > made for clarification so no one (or me) can say "Eh, why is he doing 
> > that?" 
> 
> Comments should not generally say _what_ is being done, the
> code already says that.
> 
> Comments should say _why_ something is being done.

Good point.

-- 
Sandman[.net]


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

Date: 19 Feb 2003 22:57:02 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Religious question: commenting
Message-Id: <b3123u$8ce$1@nets3.rz.RWTH-Aachen.DE>

Also sprach Sandman:

> In article <Xns9327A93BFF1BEsdn.comcast@216.166.71.239>,
>  "Eric J. Roode" <REMOVEsdnCAPS@comcast.net> wrote:
> 
>> Now I use pod:
>> 
>>     =item pairwise
>> 
>>      @c = pairwise {code} @a, @b;
>> 
>>     Applies C<{code}> to each pair of elements of C<@a>, C<@b> in
>> turn.
>>     Returns a list of the results of that evaluation.  The pairs are
>>     assigned to C<$a> and C<$b> before invoking the code.
>> 
>>     Examples:
>> 
>>         @a = (1, 2, 3); @b = (2, 4, 6);
>>         @c = pairwise {$a + $b} @a, @b;   # returns (3, 6, 9)
>>         @d = pairwise {$a * $b} @a, @b;   # returns (2, 8, 18)
>> 
>>     =cut

[...]

> Ok, I think I've looked into that before. The reason to do this is
> what? You can read the comments in the file with an external reader,
> right+ Can't remember if I choose not to do it because my editor of
> choice didn't highlight stuff correctly when I did so, or if I never
> got a hang of it.

Then change your editor. Anyway, using POD (see 'perldoc perlpod') is
actually a good idea for the per-function comment. Modules available
from CPAN are in fact documented that way. It would allow to easily dump
your subroutines into a separate module and just do:

    perldoc /path/to/Module.pm

to see a description of the provided functions. Unlike ordinary
comments, POD also allows for some simple mark-up like high-lighting,
verbatim code-paragraphs etc. that is nicely rendered by perldoc and can
easily be converted into some other format such as HTML.

It's also less to type because you no longer need the '#' prepended to
each line.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 19 Feb 2003 22:57:03 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de>
Subject: Re: Religious question: commenting
Message-Id: <b3123v$8ce$2@nets3.rz.RWTH-Aachen.DE>

Also sprach Sandman:

> In article <b30qh7$t87$1@nets3.rz.RWTH-Aachen.DE>,
>  "Tassilo v. Parseval" <tassilo.parseval@post.rwth-aachen.de> wrote:
> 
>> > With the above example, how would you comment? Would you do it basically 
>> > the same or do you have some other über-way of doing it?
>> 
>> I'd probably use less comments. But that's a matter of a) personal taste and 
>> b) complexity of the code. Yours isn't that complex. But I do have code that 
>> does so many things that I added a lot of comments.
> 
> I was hoping for a reply concerning -style- of commenting rather than
> amount of commenting (even though amount could be considered a style).
> I snipped all the parts of your post that had nothing to do with
> commenting.

But I hope you read them nonetheless. It has nothing to do with vanity
on my side. I also got some confirmation in what I wrote by Tad's
posting. Save for the wording, he seemed to point out the very same
things as I did. Better try to understand these parts. They were
probably more important than stylistic issues with comments.

> But you're right of course, the more complex the code is, the more comments 
> should it require. But I tend to comment more than less, just for the purpose 
> of the next guy, since I know what a total pain it is to try to take apart 
> someone elses perlscript to try to find out what it is he's doing.

It's not the amount, it's the content. Redundant comments do more harm
than benefit since they distract the reader. Use comments to let them
stand out and point the reader of your code to the essential bits. As
said before: the 'why' and not the 'what' should be explained.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


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

Date: 19 Feb 2003 12:31:00 -0800
From: s_vakareskov@hotmail.com (Sam)
Subject: Simple regex question...
Message-Id: <e52c9016.0302191231.4a63728a@posting.google.com>

Hi there,
I have what should be a simple question about a regex that I'm playing
with.

Here is my regesx:
/onmouseout=(.)*;/
This should match all occurences of 'onmouseover='.  And it does. But
some for some reason it also matches other stuff around it.  For
example, this whole string was retunred as a match:

'onmouseout=mouseOut(); onmouseover=mouseOverLink(1);>special fares &

Any ideas?  
Thank you in advance.
Regards,
-Sam


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

Date: Wed, 19 Feb 2003 21:48:30 +0100
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: Simple regex question...
Message-Id: <d8r75v8m79efmt3qfinb0b965l0fjn5r1n@4ax.com>

X-Ftn-To: Sam 

s_vakareskov@hotmail.com (Sam) wrote:
>Hi there,
>I have what should be a simple question about a regex that I'm playing
>with.
>
>Here is my regesx:
>/onmouseout=(.)*;/

your match is greedy, if you want to stop matching after first ; use ?
/onmouseout=(.*?);/



-- 
Matija


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

Date: Wed, 19 Feb 2003 21:53:27 +0100
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: Simple regex question...
Message-Id: <khr75vsd59sle9gvo7f92h8j5vev6icouo@4ax.com>

X-Ftn-To: Sam 

s_vakareskov@hotmail.com (Sam) wrote:
>I have what should be a simple question about a regex that I'm playing
>with.
>
>Here is my regesx:
>/onmouseout=(.)*;/
>This should match all occurences of 'onmouseover='.  And it does. But
>some for some reason it also matches other stuff around it.  For
>example, this whole string was retunred as a match:
>
>'onmouseout=mouseOut(); onmouseover=mouseOverLink(1);>special fares &

please ignore last post. :)
is above line complete result from your match?



-- 
Matija


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

Date: Wed, 19 Feb 2003 20:56:57 +0000
From: "Bob Wilkinson" <bob@fourtheye.REMOVEME.org>
To: s_vakareskov@hotmail.com (Sam)
Subject: Re: Simple regex question...
Message-Id: <pan.2003.02.19.20.56.55.763099@fourtheye.REMOVEME.org>

On Wed, 19 Feb 2003 12:31:00 -0800, Sam wrote:

> Hi there,
> I have what should be a simple question about a regex that I'm playing
> with.
> 
> Here is my regesx:
> /onmouseout=(.)*;/
> This should match all occurences of 'onmouseover='.  

No, it shouldn't. It should match all strings which contain 
onmouseout= and any characters (if any) which follow it - the 
last character being available via $1.

> And it does. But
> some for some reason it also matches other stuff around it.  For
> example, this whole string was retunred as a match:
> 
> 'onmouseout=mouseOut(); onmouseover=mouseOverLink(1);>special fares &

Your regexp matches onmouseout= and any characters (if any) which 
follow. I'm not surprised that the above matches. 

What do you want to match?

> 
> Any ideas?  
> Thank you in advance.
> Regards,
> -Sam

Bob


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

Date: Wed, 19 Feb 2003 15:48:06 -0600
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Simple regex question...
Message-Id: <Xns9327AADAC9184sdn.comcast@216.166.71.239>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

s_vakareskov@hotmail.com (Sam) wrote in
news:e52c9016.0302191231.4a63728a@posting.google.com:

> Hi there,
> I have what should be a simple question about a regex that I'm
playing
> with.
> 
> Here is my regesx:
> /onmouseout=(.)*;/
> This should match all occurences of 'onmouseover='.  And it does.
But
> some for some reason it also matches other stuff around it.  For
> example, this whole string was retunred as a match:
> 
> 'onmouseout=mouseOut(); onmouseover=mouseOverLink(1);>special fares
&
> 
> Any ideas?  


Because . matches ; in addition to (nearly) everything else.  I
expect that what you want is "onmouseout=" followed by any number of
characters except semicolons (or right-angle brackets, eh?):

    /onmouseout=([^;>]*)/

But that's not foolproof either, consider the line

    <tag onmouseout=SomeJS("a;b;c:"); onmouseover=...>

There's no easy way around that; consider using one of the
HTML-parsing modules such as HTML::TokeParser or HTML::Parser.

- -- 
Eric
print scalar reverse sort qw p ekca lre reh 
ts uJ p, $/.r, map $_.$", qw e p h tona e;

-----BEGIN xxx SIGNATURE-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.5.13

iD8DBQE+U/t3Y96i4h5M0egRAkrDAJ9y1zbzYqKjxBKhgBJ+gFshRCE8BgCgthLM
4uS4Z8lSElsBv4R2jGMd4wU=
=1BQW
-----END PGP SIGNATURE-----


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

Date: Wed, 19 Feb 2003 22:30:50 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Unsuccesful "Print"
Message-Id: <3e5404b0.58703149@news.erols.com>

"Mikey" <PleaseDontThrowSpam@Me.com> wrote:

: Global symbol "$opt_a" requires explicit package name at ./perlfile
: Global symbol "$opt_h" requires explicit package name at ./perlfile
: 
: It's getting worse, 

Worse?

I'd take a program that shrieks and dies over one that silently does
the Wrong Thing any day.



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

Date: 19 Feb 2003 19:52:59 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: use DBI;
Message-Id: <slrnb57o4r.1ea.abigail@alexandra.abigail.nl>

Helgi Briem (helgi@decode.is) wrote on MMMCDLIX September MCMXCIII in
<URL:news:3e535c5c.1031961211@news.cis.dfn.de>:
;;  On 18 Feb 2003 18:13:58 GMT, Abigail <abigail@abigail.nl>
;;  wrote:
;;  
;; >Helgi Briem (helgi@decode.is) wrote on MMMCDLVIII September MCMXCIII in
;; ><URL:news:3e523a9b.957778142@news.cis.dfn.de>:
;; >))  
;; >))  That last exit is completely unnecessary.  Where does
;; >))  it come from?  I keep seeing this sort of thing in
;; >))  cargo cult code.  
;; >))  
;; >)) >exit(0);
;; >
;; >That has nothing to do with 'cargo cult'. Cargo cult programming is
;; >about using constructs you don't understand, but which seem vital
;; >to your program.
;;  
;;  I always see this in old Perl4 CGI programs that people
;;  have downloaded from the internet somewhere and are trying 
;;  to fix.  I doubt that many of them understand
;;  the code at all.  Most haven't even looked at it.
;;  
;;  When I first encountered Perl, I was shown it by a C/C++
;;  programmer.  He put exit(0) all over the place. For a 
;;  while, I did too.  I have always assumed that it was a C-ism
;;  but I don't speak C so I can't be sure.
;;  
;; >I doubt the 'exit(0)' was put there without knowing what 
;; >the exit() does. 
;;  
;;  I am pretty sure most of the people who use it in do it
;;  just because it was in the script they downloaded or
;;  copied.  I almost certain most of them haven't got a
;;  clue what it's there for.
;;  
;; >Nor does it mysteriously change the behaviour of the program.
;;  
;;  No, it just does absolutely nothing that wouldn't have 
;;  happened anyway.
;;  
;; >Just because it isn't "necessary" doesn't make an exit() at the end
;; >of the program cargo cult, just like using parens where not required,
;; >or an $_ where not required make "cargo cult".
;;  
;;  I though the definition of "cargo cult" was using something
;;  in a program because you saw it somewhere else without
;;  knowing why you are using it.

Yes, I said that, and you quoted that. What is your point? Or are you just
assuming that anyone who uses 'exit(0)' doesn't know what that does? I
find that a very bold assumption. Do you have any data to back this up?

;;  Do you use exit(0) a lot?  And if so, what for?

What is 'a lot'? I sometimes use 'exit' at a point where it would
reach the end of the file anyway. And sometimes, even an 'exit 0'.
Sometimes I don't. I do the same in shell scripts. Why? Just because
something I feel like doing so.

I don't see anything wrong with that. Why are you all over that, eager
to slap it with a label 'Cargo Cult'?


Abigail
-- 
perl -wle 'sub _ "Just another Perl Hacker"; print prototype \&_'


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

Date: Wed, 19 Feb 2003 14:21:40 -0500
From: Joel Konkle-Parker <j.j.konkle-parker@larc.nasa.gov>
Subject: Re: Variables from 'require' not initialized
Message-Id: <3E53D944.9090402@larc.nasa.gov>

> The fact that lexically scoped variables are _lexically_ _scoped_.
> 
> When you declare a variable with my() the scope of the declaration is
> from the end of the current statment to the end of the current lexical
> scope.  i.e. until the first unmatched '}' in the _current_ source
> file.
> 
> If you process another file with require() or do() then that file is
> processed in a fresh lexical scope.
>  
> If you want a file included with requre to alter variable in the
> calling environment you need to be using package scoped variables.
> 
> 

Ok, here's my first attempt at using package variables:

Script:
----------
#!/bin/perl -wT

push (@INC, ".");

use strict;
use Getopt::Long;
use Math::Trig;

my ($inputfile, $filename, $aoa);
$ENV{PATH} = "/bin:/usr/local/bin:/ump/fldmd/home/joeljkp/pmarc/exe";


#####################
# Argument handling
#####################


GetOptions ("filename|f=s" => \$inputfile);

if ($inputfile =~ /^([-\w.]+)$/) {
   $inputfile = $1;
   print "starting require\n";
   require $inputfile;
   print "done require\n";
}
else {
   die "Invalid characters in filename\n";
}
print "out if\n";

my $span = $parameters::span;
my $rawfilename = $parameters::rawfilename;
my $outboard_chord = $parameters::outboard_chord;
my $grid_type = $parameters::grid_type;
my $velocity = $parameters::velocity;
my $aoa_degrees = $parameters::aoa_degrees;

print $parameters::span;

 ...
et c.
 ...
----------

Input:
----------
print "top input\n";

package parameters;
my ($rawfilename, $span, $outboard_chord, $grid_type, $velocity, 
$aoa_degrees);

$rawfilename = 'temp';  # Filename for script output files (no 
extension, no periods)
$span = 10.0;           # Wingspan (inboard chord is 1.0000)
$outboard_chord = 0.5;  # Outboard chord length (inboard chord is 1.0000)
$grid_type = 'ss';      # Grid type (ss = structured surface, sv = 
structured volume, uv = unstructured volume)
$velocity = 100.0;      # Velocity (m/s)
$aoa_degrees = 0.0;     # Angle of attack (degrees)

print "bottom input\n";
1;
----------

Output:
----------
Name "parameters::rawfilename" used only once: possible typo at 
 ./airfoil.pl line 58.
Name "parameters::aoa_degrees" used only once: possible typo at 
 ./airfoil.pl line 62.
Name "parameters::velocity" used only once: possible typo at 
 ./airfoil.pl line 61.
Name "parameters::grid_type" used only once: possible typo at 
 ./airfoil.pl line 60.
Name "parameters::outboard_chord" used only once: possible typo at 
 ./airfoil.pl line 59.
starting require
top input
bottom input
done require
out if
Use of uninitialized value at ./airfoil.pl line 64.
----------

So what am I missing this time?



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

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


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