[18967] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1162 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jun 20 06:05:36 2001

Date: Wed, 20 Jun 2001 03:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <993031509-v10-i1162@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 20 Jun 2001     Volume: 10 Number: 1162

Today's topics:
        clean install and still perl won't work (Frank)
        END_MULTIPART (Seems very rare) <scott@generator.co.za>
    Re: END_MULTIPART (Seems very rare) (Rafael Garcia-Suarez)
    Re: END_MULTIPART (Seems very rare) <buggs-clpm@splashground.de>
    Re: END_MULTIPART (Seems very rare) <maheshak@synopsys.com>
    Re: END_MULTIPART (Seems very rare) <peb@bms.umist.ac.uk>
    Re: Fetching CGI data as a hash (was DBM in a CGI) <dneill@snet.net>
    Re: Fetching CGI data as a hash (was DBM in a CGI) <amittai@amittai.com>
    Re: Fetching CGI data as a hash (was DBM in a CGI) <amittai@amittai.com>
    Re: help printing floating point <toga@celsiustech.se>
    Re: help printing floating point <toga@celsiustech.se>
    Re: help printing floating point <toga@celsiustech.se>
    Re: help printing floating point <pne-news-20010620@newton.digitalspace.net>
    Re: help printing floating point <pne-news-20010620@newton.digitalspace.net>
    Re: help printing floating point <toga@celsiustech.se>
    Re: Help with Rewriting Application in OO Perl (Chris Fedde)
    Re: Help with Rewriting Application in OO Perl <goldbb2@earthlink.net>
    Re: Help with Rewriting Application in OO Perl (Chris Fedde)
    Re: How do I get the current directory from Windows Exp (Garry)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 20 Jun 2001 02:23:50 -0700
From: tiberianqs@yahoo.com (Frank)
Subject: clean install and still perl won't work
Message-Id: <ceaf9a51.0106200123.41ae8077@posting.google.com>

I am using windows 2000 (clean no other server is installated)
I installed the sambar web server (www.sambar.com)
then installed ActivePerl-5.6.1.626-MSWin32

When i put a HTML doc into the local host. it works
But when i Put a Perl doc  .pl  on the cgi-bin directory
and then try to run it from my Internet Browser. It won't run the .pl program.

what is the problem ?

Please, help//


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

Date: Wed, 20 Jun 2001 09:54:42 +0200
From: "scotth" <scott@generator.co.za>
Subject: END_MULTIPART (Seems very rare)
Message-Id: <3b305726$0$230@hades.is.co.za>

Hi.
I am trying to get a CGI search engine written by someone else to work.
After modifying the first line to the correct path, I get this error on both
Linux and Windows.

Can't find string terminator "END_MULTIPART" anywhere before EOF at
 ./cgi-lib.pl line 114.

This error seems to be rare, as I could only find one reference to it on
Google.
Has anyone had this problem before, if so how do I solve it?

Thanks
Scott
--
Scott Houseman

Software Development
scott@generator.co.za
mobile: +27 82 491 8021

Generator Communications | Be An Island
tel:  +27 21 425 9710
fax: +27 21 425 9713




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

Date: 20 Jun 2001 08:22:46 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: END_MULTIPART (Seems very rare)
Message-Id: <slrn9j0nc8.thk.rgarciasuarez@rafael.kazibao.net>

scotth wrote in comp.lang.perl.misc:
} Hi.
} I am trying to get a CGI search engine written by someone else to work.
} After modifying the first line to the correct path, I get this error on both
} Linux and Windows.
} 
} Can't find string terminator "END_MULTIPART" anywhere before EOF at
} ./cgi-lib.pl line 114.
} 
} This error seems to be rare, as I could only find one reference to it on
} Google.

When looking for info about a perl error message, you should look in the
first place in the perldiag manpage.

It seems that there's somewhere in the code a here-doc construct

 ... <<END_MULTIPART;
 ...
 ...
END_MULTIPART

that was not properly closed.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Wed, 20 Jun 2001 10:37:23 +0200
From: buggs <buggs-clpm@splashground.de>
Subject: Re: END_MULTIPART (Seems very rare)
Message-Id: <9gpn8s$lbr$01$1@news.t-online.com>

scotth wrote:

> Hi.
> I am trying to get a CGI search engine written by someone else to work.
> After modifying the first line to the correct path, I get this error on
> both Linux and Windows.
> 
> Can't find string terminator "END_MULTIPART" anywhere before EOF at
> ./cgi-lib.pl line 114.

you may have something like

print <<"END_MULTIPART";
SOME TEXT OR STUFF
 ...
SOME MORE TEXT OR MORE STUFF
END_MULTIPART


The last END_MULTIPART is probably missing.

Buggs


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

Date: Wed, 20 Jun 2001 10:11:27 +0100
From: Mahesh Asolkar <maheshak@synopsys.com>
Subject: Re: END_MULTIPART (Seems very rare)
Message-Id: <3B3068BF.C9EBD014@synopsys.com>

scotth wrote:
> 
> Hi.
> I am trying to get a CGI search engine written by someone else to work.
> After modifying the first line to the correct path, I get this error on both
> Linux and Windows.

Did you, by any chance, indent the code? 

<following lines... courtesy Buggs>
-----------
print <<"END_MULTIPART";
SOME TEXT OR STUFF
 ...
SOME MORE TEXT OR MORE STUFF
   END_MULTIPART  <<< Watch this out
------------

If the END_MULTIPART that ends the here-doc does not start at the 1st
column, you could get an error.

<rest snipped>

- Mahesh.


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

Date: Wed, 20 Jun 2001 10:15:31 +0100
From: Paul Boardman <peb@bms.umist.ac.uk>
Subject: Re: END_MULTIPART (Seems very rare)
Message-Id: <3B3069B3.2170D80@bms.umist.ac.uk>

scotth wrote:
> 
> Hi.
> I am trying to get a CGI search engine written by someone else to work.
> After modifying the first line to the correct path, I get this error on both
> Linux and Windows.
> 
> Can't find string terminator "END_MULTIPART" anywhere before EOF at
> ./cgi-lib.pl line 114.
> 
> This error seems to be rare, as I could only find one reference to it on
> Google.
> Has anyone had this problem before, if so how do I solve it?


sounds to me like you're not terminating a 'here' document.

look for something like

print <<END_MULTIPART;

followed by a lot of text to be printed.

at the end of this there should be the text END_MULTIPART at the
begining of a line with nothing else (not even any whitespace).

The reason you only found one reference on Google is that the
END_MULTIPART text is just a tag.  You could substitute any text you
like in its place, so long as it matches the terminatig tag.

HTH

Paul


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

Date: Wed, 20 Jun 2001 02:02:38 -0400
From: "Dave Neill" <dneill@snet.net>
Subject: Re: Fetching CGI data as a hash (was DBM in a CGI)
Message-Id: <40XX6.10249$ld7.1647874@typhoon.snet.net>

<snip>

The problem comes up when, after creating a new
> CGI object, I attempt to create a hash out of the names-values pairs
coming
> from the form:
>
> use CGI qw(:cgi);
>
> $query = new CGI;
> %fields = $query->Vars;
>

You want

use CGI qw(:cgi-lib);

$query = new CGI;
%fields = $query->Vars;

Vars needs :cgi-lib imported.  If you want to manipulate the values, Vars
must be called in a scalar context to produce the tied hash - so the last
line would read:

$fields = $query->Vars

<snip>

> BTW, if anybody can suggest a way around this to create a hash from the
> incoming data without having to spell out every single field name and
using
> $value = param("fieldname"), I'd also be grateful.

Posted recently to this group:

            Message 7 in thread
      From: Andras Malatinszky (andras@mortgagestats.com)
      Subject: Re: CGI and printing vars


<snip>
Suppose your parameters are fee, fie, foe and fum.
You can do something like

$cgi->import_names('foo');

This will automagically create the variables
$foo::fee
$foo::fie
$foo::foe
and
$foo::fum

and assign the appropriate values to them. You can obviously work out the
rest.

This, by the way, is described in the CGI.pm documentation. Read it, there
is
lot's of good stuff in there.

>
>
> --
> Amittai Aviram
> Please note new address! --  amittai@amittai.com




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

Date: Wed, 20 Jun 2001 10:18:15 +0200
From: "Amittai Aviram" <amittai@amittai.com>
Subject: Re: Fetching CGI data as a hash (was DBM in a CGI)
Message-Id: <9gpmaq$8fn$1@rznews2.rrze.uni-erlangen.de>

Thank you to everybody who has responded to my query.  I had suspected that
the CGI.pm needs to be updated, but, of course, it's not mine.  I will
mention it to the administrator.  I have indeed read the CGI.pm
documentation in the Perl manual that came with my ActivePerl (for Windows)
package, and hence my thought to use %fields = $query->Vars int he first
place, but I must admit that for me as a novice the article, though
well-written, was cryptic.  It was hard for me to tell whether the example
lines, upon which that short section depends, presented _alternatives_ or a
single code unit.  Again, thanks to all.

Amittai Aviram
amittai@amittai.com

"Dave Neill" <dneill@snet.net> wrote in message
news:40XX6.10249$ld7.1647874@typhoon.snet.net...
> <snip>
>
> The problem comes up when, after creating a new
> > CGI object, I attempt to create a hash out of the names-values pairs
> coming
> > from the form:
> >
> > use CGI qw(:cgi);
> >
> > $query = new CGI;
> > %fields = $query->Vars;
> >
>
> You want
>
> use CGI qw(:cgi-lib);
>
> $query = new CGI;
> %fields = $query->Vars;
>
> Vars needs :cgi-lib imported.  If you want to manipulate the values, Vars
> must be called in a scalar context to produce the tied hash - so the last
> line would read:
>
> $fields = $query->Vars
>
> <snip>
>
> > BTW, if anybody can suggest a way around this to create a hash from the
> > incoming data without having to spell out every single field name and
> using
> > $value = param("fieldname"), I'd also be grateful.
>
> Posted recently to this group:
>
>             Message 7 in thread
>       From: Andras Malatinszky (andras@mortgagestats.com)
>       Subject: Re: CGI and printing vars
>
>
> <snip>
> Suppose your parameters are fee, fie, foe and fum.
> You can do something like
>
> $cgi->import_names('foo');
>
> This will automagically create the variables
> $foo::fee
> $foo::fie
> $foo::foe
> and
> $foo::fum
>
> and assign the appropriate values to them. You can obviously work out the
> rest.
>
> This, by the way, is described in the CGI.pm documentation. Read it, there
> is
> lot's of good stuff in there.
>
> >
> >
> > --
> > Amittai Aviram
> > Please note new address! --  amittai@amittai.com
>
>




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

Date: Wed, 20 Jun 2001 10:58:14 +0200
From: "Amittai Aviram" <amittai@amittai.com>
Subject: Re: Fetching CGI data as a hash (was DBM in a CGI)
Message-Id: <9gpolr$9ju$1@rznews2.rrze.uni-erlangen.de>

Again, thanks to everybody for responding to my query.  As it turns out,
Jason Clifford's solution (the first proposed) is the _only_ one that works
on the system as it is currently set up.  Invoking cgi-lib does not seem to
enable Vars on this system, which suggests, indeed, that the module needs to
be updated.  John Krahn's suggestion (copied below) produces results rather
than an error, but assigns the wrong names and values respectively to the
keys and values -- it assigns the value of the second name to the value of
the first key in the %fields hash.  Respective solutions copied below copy
of my original post.

"Amittai Aviram" <amittai@amittai.com> wrote in message
news:9gnq13$9ma$1@rznews2.rrze.uni-erlangen.de...
> Getting started with Perl, CGI, and Unix and am grateful for help from
this
> group.  After some troubleshooting, I was able to figure out exactly what
is
> causing some scripts of mine to return server error messages when I
attempt
> to run them via Web forms.  The problem comes up when, after creating a
new
> CGI object, I attempt to create a hash out of the names-values pairs
coming
> from the form:
>
> use CGI qw(:cgi);
>
> $query = new CGI;
> %fields = $query->Vars;
>
> Comment the last line out, the script works (without the hash, of course.)
> Uncomment it, I get a server error.  My administator informs me that he is
> running Perl 5 and an Apache server.  I have no idea where to go next.
Any
> suggestions or help would be appreciated.
>
> BTW, if anybody can suggest a way around this to create a hash from the
> incoming data without having to spell out every single field name and
using
> $value = param("fieldname"), I'd also be grateful.
> --
> Amittai Aviram

Jason Clifford's solution (works):

my (@names, %fields);
@names = $query->param;
foreach my $p (@names)
    {
    $fields{$p} = param($p);
    }


John W. Krahn's solution (does not work correctly):

Or more simply:

my %fields;
my @names = $query->param;
@fields{@names} = param(@names);


gnari's solution (see comment below):

upgrade your CGI.pm

   [ ... indeed! ... -- AA]

Dave Neill's solution (see comment below):

use CGI qw(:cgi-lib);

$query = new CGI;
%fields = $query->Vars;

[AA:  invoking cgi-lib does not cause an error, but attempting to create the
%fields hash with it, using Vars, does cause an error.  So this does not
work on the system as currently configured.

Amittai Aviram amittai@amittai.com





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

Date: Wed, 20 Jun 2001 08:28:48 +0200
From: Tobias Gabrielsson <toga@celsiustech.se>
Subject: Re: help printing floating point
Message-Id: <3B3042A0.56CC378@celsiustech.se>



Paul Boardman wrote:

> Tobias Gabrielsson wrote:
> >
> > I would like to print, on a webpage, a variable like 12.1234567 as 12.12
> > only. Can anyone help me? I have tride almost everything, but I cannot
> > make it work. i have tried %.2f, but without any luck.
> >
>
> show us some of your code.
>
> using 'printf("%.2f", $var)' should work fine.
>
> e.g.
>
> $a = 12.124576234;
> printf("%.2f", $a);
>
> prints 12.12.
>
> If you post some of your code we might be able to pin-point the problem.
>
> Paul

My problem is that I would like to print it out from a script on a html-page
using tags.

print "("%.2f", $a)";
if I use printf here, it prints "%.2f12.1234566"

So, my problem is maybe that I don't know how to express it in html??
Any suggestions?
Thanks for helping me!
/Lena



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

Date: Wed, 20 Jun 2001 08:32:45 +0200
From: Tobias Gabrielsson <toga@celsiustech.se>
Subject: Re: help printing floating point
Message-Id: <3B30438D.C520C0E@celsiustech.se>



Mike Wescott wrote:

> Tobias Gabrielsson <toga@celsiustech.se> writes:
>
> > I would like to print, on a webpage, a variable like 12.1234567 as 12.12
> > only. Can anyone help me? I have tride almost everything, but I cannot
> > make it work. i have tried %.2f, but without any luck.
>
> Works for me:
>
> $ perl -e 'printf "%.2f\n", 12.1234567;'
> 12.12
>
> Perhaps you could show us a code fragment that illustrates your
> problem.
> --
>         Mike Wescott
>         wescott@conterra.com

My problem is that I would like to print it out from a script on a html-page
using tags.
I have a line in the script saying:
$Results = sqrt(...);
print "("%.2f", $Results)"; //Here, I want to send the $Results to my
html-page

if I use printf here, it prints "%.2f12.1234566" on the html-page.

So, my problem is maybe that I don't know how to express it in html??
Any suggestions?
Thanks for helping me!
/Lena




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

Date: Wed, 20 Jun 2001 08:33:21 +0200
From: Tobias Gabrielsson <toga@celsiustech.se>
Subject: Re: help printing floating point
Message-Id: <3B3043B1.3EF7F913@celsiustech.se>



Anno Siegel wrote:

> According to Tobias Gabrielsson  <toga@celsiustech.se>:
> > I would like to print, on a webpage, a variable like 12.1234567 as 12.12
> > only. Can anyone help me? I have tride almost everything, but I cannot
> > make it work. i have tried %.2f, but without any luck.
>
> How did you try?  $x = 12.1234567; printf "%.2f\n", $x; does exactly
> that.
>
> Anno

My problem is that I would like to print it out from a script on a html-page
using tags.
I have a line in the script saying:
$Results = sqrt(...);
print "("%.2f", $Results)"; //Here, I want to send the $Results to my
html-page

if I use printf here, it prints "%.2f12.1234566" on the html-page.

So, my problem is maybe that I don't know how to express it in html??
Any suggestions?
Thanks for helping me!
/Lena




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

Date: Wed, 20 Jun 2001 08:50:04 +0200
From: Philip Newton <pne-news-20010620@newton.digitalspace.net>
Subject: Re: help printing floating point
Message-Id: <ach0jto3ijsnr0d5if6e1s3iiuet0kt1u0@4ax.com>

On Wed, 20 Jun 2001 08:32:45 +0200, Tobias Gabrielsson
<toga@celsiustech.se> wrote:

> print "("%.2f", $Results)"; //Here, I want to send the $Results to my
> html-page
> 
> if I use printf here, it prints "%.2f12.1234566" on the html-page.

The quotes appear to be in the wrong positions. But if you use

    printf ("%.2f", $Results);

, it should work. (Note the lack of quotes around the parentheses, and
the change from print to printf.)

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: Wed, 20 Jun 2001 08:50:05 +0200
From: Philip Newton <pne-news-20010620@newton.digitalspace.net>
Subject: Re: help printing floating point
Message-Id: <seh0jtsi501p79c99v2b31udpcn9g1k9e3@4ax.com>

On Wed, 20 Jun 2001 08:28:48 +0200, Tobias Gabrielsson
<toga@celsiustech.se> wrote:

> print "("%.2f", $a)";
> if I use printf here, it prints "%.2f12.1234566"

Use   printf ("%.2f", $a);   instead. And drop those double quotes
around the parentheses.

Cheers,
Philip
-- 
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.


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

Date: Wed, 20 Jun 2001 09:11:13 +0200
From: Tobias Gabrielsson <toga@celsiustech.se>
Subject: Re: help printing floating point
Message-Id: <3B304C91.D07E20D0@celsiustech.se>

Thank you!!! It works, so you made my day!
/Lena

Philip Newton wrote:

> On Wed, 20 Jun 2001 08:28:48 +0200, Tobias Gabrielsson
> <toga@celsiustech.se> wrote:
>
> > print "("%.2f", $a)";
> > if I use printf here, it prints "%.2f12.1234566"
>
> Use   printf ("%.2f", $a);   instead. And drop those double quotes
> around the parentheses.
>
> Cheers,
> Philip
> --
> Philip Newton <nospam.newton@gmx.li>
> That really is my address; no need to remove anything to reply.
> If you're not part of the solution, you're part of the precipitate.



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

Date: Wed, 20 Jun 2001 05:00:29 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Help with Rewriting Application in OO Perl
Message-Id: <N5WX6.162$T3.202376704@news.frii.net>

In article <f024c24d.0106191441.1e968954@posting.google.com>,
E. Moore <emoore@cofs.net> wrote:
>Next Steps
>----------
>First, how much of a performance 'hit' should I expect if I
>'objectify' this code?

A well done object system will causes litle performance hit if any. 

>
>Second, with the 'use' builtin, if I quote only those methods I want
>to use out of common.pm, (e.g. use MyMods::Common qw( login logout) )
>does that improve compile time over simply calling use MyMods::Common?
>

Most OO modules do not export subroutines into the users namespace.
instead Class and instance methods are used. There might be some compile
time hit for using Exporter to enable this behavior but it might be
mitigated by using AutoLoader or SelfLoader.

>Third, I don't quite understand how to create a MyConfig.pm so that
>all the object attributes are available to other scripts?  Along that
>line, I don't see how I access these attributes. I've looked in the
>CPAN and I've gotten more confused.  I rather like the global hashes I
>have now since I don't have to know anything about them, which is the
>point of OO as I understand it.
>

The point of object programming is to allow the classes and instances
to behave in reasonable ways that makes their internal details
irrelevant.  In some ways the Perl object system fails at this but
it can still be very useful.  Two of the most powerful ideas to
grasp in Perl OO are the idea of an AUTOLOAD subroutine that can
be used as a generic attribute get/set subroutine, and the idea
that you are working with a dynamic programming language.  As an
example I'll include a class that I am finding useful for some of
the work that I am doing at the moment. I use it in pipe lines
like the following:

    #!/bin/sh
    # report average xdelay per relay
    grep stat=Sent /var/log/mail.log |
    perl -MS -lne '
	$s=S->parse($_); $d{$s->relay} += $s->xdelay; $c{$s->relay}++;
	END{
	    for (keys %d){
		printf "%6.2f %4d %4d %s\n",
		    $d{$_}/$c{$_}, $c{$_}, $d{$_}, $_}
	}
    ' | sort -n

I do the grep and the sort outside perl because it sucks at those
operations.  I do it in a pipe because unix rocks!

------------------------------------------------------------------------------
package S;
use Carp;
use strict;
use Time::ParseDate;

use vars qw( $AUTOLOAD $DEBUG );

{
    my %Class;

    sub parse {
	my $class = shift;
	$class = ref $class || $class;
	my $line = shift;
	my %field;
	chomp $line;

	$field{'raw'} = $line;
	@field{qw(date host ident pid rest) } = 
	    ($line =~ m/
		(?xo)                         # extended regular expression
		^                             # At the beginning of the line
		(\w+\s+\d+\s+\d+:\d+:\d+) \s+ # match Mon Day HH:MM:SS
		(\S+)                     \s+ # match host string
		([^\[:]+)                     # the ident tag
		(?:\[(\d+)\])?                # the optional PID
		:\s+                          # a colon and some space
		(.+)$                         # the rest of the line
	    /);

	#
	# Deal with wierd Solarisism in some syslog messages
	# eg [ID 801593 mail.info]
	#
	$field{'rest'} =~ s/\[ID\s+(\d+)\s+([\w.]+)\]\s*//;
	@field{qw(ID, facility)} = ($1, $2);

	$field{'ident'} = 'unknown' unless ($field{'ident'});

	my $self = \%field;

	#
	# compute a class name
	#
	$self->{'ident'} =~ m/(\w+)/;
	my $newclass = $class. "::". ucfirst($1);

	#
	# newclass inherits from current class
	#
	$@ = 0;
	eval qq/
	    \@${newclass}::ISA = qw($class);
	/ unless ($Class{$newclass});
	croak ("could not eval for ${newclass}::ISA: $@") if $@;

	$Class{$newclass}++;

	bless $self, $newclass;
	$self->init;

	return $self;
    } 
}

sub now {
    my $self = shift;

    $self->{'now'} = parsedate($self->date) unless ($self->{'now'});
    return $self->{'now'};
} 

sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self)
                or croak "$self is not an object";
    my $code;

    return if $AUTOLOAD =~ /::DESTROY$/;

    my $name = $AUTOLOAD;
    $name =~ s/.*://;

    #
    # Gen the new method
    #
    $code = sub {
        my $self = shift;

        if (@_) {
            return $self->{$name} = shift;
        } else {
            return $self->{$name};
        }
    };

    #
    # stuff the new method into the name space
    #
    {
        no strict 'refs';
        *$AUTOLOAD = $code;
    }

    #
    # Call the new method
    #
    $self->$name(@_);
}

package S::Sendmail;

#
# convert "delay" style values into seconds
#
sub delay2sec {
    my $delay = shift;
    my ($sec, $min, $hr, $day) = reverse (split (/[+:]/, $delay));

    $sec += 60*$min if (defined $min);
    $sec += 3600*$hr if (defined $hr);
    $sec += $day * 3600 *24 if (defined $day);

    return $sec;
}

sub delay {
    return unless defined $_[0]->{'delay'};
    return delay2sec($_[0]->{'delay'});
}

sub xdelay {
    return unless defined $_[0]->{'xdelay'};
    return delay2sec($_[0]->{'xdelay'});
}

#
# make a hash of what we can find in the payload part of
# a sendmail syslog record.
#
sub init {
    my $self = shift;
    my $rest = $self->rest;

    #
    # $1 gets qid $2 gets sequence number
    #
    $rest =~ s/^(\w+)?(?:\[(\d+)\])?:?\s*//;

    ($self->{'qid'}, $self->{'seq'}) = ($1, $2);
    $self->{'rest'} = $rest;

    if ($rest =~ /^to=/) {

        my %e = ($rest =~ m{(\S+)=([^,\n]+)[\n,]?}g);
        @{$self}{keys %e} = values %e;
        $self->{'type'} = 'deliver';

    }elsif ($rest =~ /^from=/) {

        my %e = ($rest =~ m{(\S+)=([^,\n]+)[\n,]?}g);
        @{$self}{keys %e} = values %e;
        $self->{'type'} = 'queue';

    } else {
        $self->{'type'} = 'unknown';
    }

    return $self;
}


1;
-- 
    This space intentionally left blank


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

Date: Wed, 20 Jun 2001 04:35:37 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Help with Rewriting Application in OO Perl
Message-Id: <3B306059.923F27DF@earthlink.net>

Chris Fedde wrote:
[snip]
>     #!/bin/sh
>     # report average xdelay per relay
>     grep stat=Sent /var/log/mail.log |
>     perl -MS -lne '
>         $s=S->parse($_); $d{$s->relay} += $s->xdelay; $c{$s->relay}++;
>         END{
>             for (keys %d){
>                 printf "%6.2f %4d %4d %s\n",
>                     $d{$_}/$c{$_}, $c{$_}, $d{$_}, $_}
>         }
>     ' | sort -n
> 
> I do the grep and the sort outside perl because it sucks at those
> operations.  I do it in a pipe because unix rocks!

While I agree that unix rocks, I disagree that perl sucks at grep and sort.  Try:
perl -MS -ln <<__END__ /var/log/mail.log
	next unless /stat=Sent/;
	$s=S->parse($_); $d{$s->relay} += $s->xdelay; $c{$s->relay}++;
	END {
		print sort { $a <=> $b } map {
			sprintf "%6.2f %4d %4d %s\n",
				$d{$_}/$c{$_}, $c{$_}, $d{$_}, $_}
			} keys %d;
	}
__END__

sh and bash both support here-documents (ie, the <<END thing)

------------------------------------------------------------------------------
> package S;
> use Carp;
> use strict;
> use Time::ParseDate;

use warnings;

>
> use vars qw( $AUTOLOAD $DEBUG );
> 
> {
>     my %Class;
> 
>     sub parse {
>         my $class = shift;
>         $class = ref $class || $class;
>         my $line = shift;
>         my %field;
>         chomp $line;
> 
>         $field{'raw'} = $line;
>         @field{qw(date host ident pid rest) } =
>             ($line =~ m/
>                 (?xo)                         # extended regular expression
>                 ^                             # At the beginning of the line
>                 (\w+\s+\d+\s+\d+:\d+:\d+) \s+ # match Mon Day HH:MM:SS
>                 (\S+)                     \s+ # match host string
>                 ([^\[:]+)                     # the ident tag
>                 (?:\[(\d+)\])?                # the optional PID
>                 :\s+                          # a colon and some space
>                 (.+)$                         # the rest of the line
>             /);

Is there a reason to use (?xo) instead of /x?  There certainly isn't any reason
to be using the /o modifyer here, as there's no interpolation going on.

> 
>         #
>         # Deal with wierd Solarisism in some syslog messages
>         # eg [ID 801593 mail.info]
>         #
>         $field{'rest'} =~ s/\[ID\s+(\d+)\s+([\w.]+)\]\s*//;
>         @field{qw(ID, facility)} = ($1, $2);
> 
>         $field{'ident'} = 'unknown' unless ($field{'ident'});

This is where the ||= operator comes in handy.

> 
>         my $self = \%field;
> 
>         #
>         # compute a class name
>         #
>         $self->{'ident'} =~ m/(\w+)/;

Is there a reason to use the $self ref rather than %fields, here?

>         my $newclass = $class. "::". ucfirst($1);
> 
>         #
>         # newclass inherits from current class
>         #
>         $@ = 0;
>         eval qq/
>             \@${newclass}::ISA = qw($class);
>         / unless ($Class{$newclass});
>         croak ("could not eval for ${newclass}::ISA: $@") if $@;

Ewww.  What's wrong with using a symref here?

	{ no strict refs; @{"${newclass}::ISA"} = ($class); }

Symrefs may be bad, but eval is evil.

> 
>         $Class{$newclass}++;
> 
>         bless $self, $newclass;
>         $self->init;
> 
>         return $self;
>     }
> }
> 
> sub now {
>     my $self = shift;
> 
>     $self->{'now'} = parsedate($self->date) unless ($self->{'now'});
>     return $self->{'now'};
> }

Yet another place for ||=

> 
> sub AUTOLOAD {
>     my $self = shift;
>     my $type = ref($self)
>                 or croak "$self is not an object";

I don't see the point to this... you don't use $type anywhere, and the only
place you use $self is to make a method call at the end, one which would be
better off replaced with goto &$code.  Also, instead of just checking if the
first argument is a ref, why not do something like:
	croak "Argument 0 isn't a " . __PACKAGE__
		unless ref($_[0]) && $_[0]->isa(__PACKAGE__);


>     my $code;
> 
>     return if $AUTOLOAD =~ /::DESTROY$/;
> 
>     my $name = $AUTOLOAD;
>     $name =~ s/.*://;

I would do a bit of checking here...

	my ($pack,$name) = $AUTOLOAD =~ m/(.*)::(.*)/s;
	croak "No element $name exists in " . ref($_[0])
		unless exists $_[0]->{$name};
	croak "Can't AUTOLOAD method for $pack from " . __PACKAGE__
		unless $pack->isa(__PACKAGE__);
	croak "Argument 0 isn't a $pack"
		unless $_[0]->isa($pack);

> 
>     #
>     # Gen the new method
>     #
>     $code = sub {
>         my $self = shift;
> 
>         if (@_) {
>             return $self->{$name} = shift;
>         } else {
>             return $self->{$name};
>         }
>     };
> 
>     #
>     # stuff the new method into the name space
>     #
>     {
>         no strict 'refs';
>         *$AUTOLOAD = $code;
>     }
> 
>     #
>     # Call the new method
>     #
>     $self->$name(@_);

If you hadn't messed with @_, you could simply do:

	goto &$code;

Which has the added benefit of causing the stack to appear as if the AUTOLOADED
sub was called directly, rather than through autoload.  Not that it's important
in *this* case, (since the sub is so simple) but it's a good habit to get into.

> }
> 
> package S::Sendmail;
> 
> #
> # convert "delay" style values into seconds
> #
> sub delay2sec {
>     my $delay = shift;
>     my ($sec, $min, $hr, $day) = reverse (split (/[+:]/, $delay));

What's wrong with:
	my ($day, $hr, $min, $sec) = split /[+:]/, $delay, 4;

> 
>     $sec += 60*$min if (defined $min);
>     $sec += 3600*$hr if (defined $hr);
>     $sec += $day * 3600 *24 if (defined $day);
> 
>     return $sec;
> }
> 
> sub delay {
>     return unless defined $_[0]->{'delay'};
>     return delay2sec($_[0]->{'delay'});
> }
> 
> sub xdelay {
>     return unless defined $_[0]->{'xdelay'};
>     return delay2sec($_[0]->{'xdelay'});
> }
> 
> #
> # make a hash of what we can find in the payload part of
> # a sendmail syslog record.
> #
> sub init {
>     my $self = shift;
>     my $rest = $self->rest;
> 
>     #
>     # $1 gets qid $2 gets sequence number
>     #
>     $rest =~ s/^(\w+)?(?:\[(\d+)\])?:?\s*//;
> 
>     ($self->{'qid'}, $self->{'seq'}) = ($1, $2);
>     $self->{'rest'} = $rest;
> 
>     if ($rest =~ /^to=/) {
> 
>         my %e = ($rest =~ m{(\S+)=([^,\n]+)[\n,]?}g);
>         @{$self}{keys %e} = values %e;
>         $self->{'type'} = 'deliver';
> 
>     }elsif ($rest =~ /^from=/) {
> 
>         my %e = ($rest =~ m{(\S+)=([^,\n]+)[\n,]?}g);
>         @{$self}{keys %e} = values %e;
>         $self->{'type'} = 'queue';
> 
>     } else {
>         $self->{'type'} = 'unknown';
>     }
> 
>     return $self;
> }
> 
> 1;
> --
>     This space intentionally left blank

-- 
The longer a man is wrong, the surer he is that he's right.


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

Date: Wed, 20 Jun 2001 09:33:51 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Help with Rewriting Application in OO Perl
Message-Id: <36_X6.168$T3.209188352@news.frii.net>

In article <3B306059.923F27DF@earthlink.net>,
Benjamin Goldberg  <goldbb2@earthlink.net> wrote:
>Chris Fedde wrote:
>[snip]
>> I do the grep and the sort outside perl because it sucks at those
>> operations.  I do it in a pipe because unix rocks!
>
>While I agree that unix rocks, I disagree that perl sucks at grep and
>sort.  Try:
>perl -MS -ln <<__END__ /var/log/mail.log
>	next unless /stat=Sent/;
>	$s=S->parse($_); $d{$s->relay} += $s->xdelay; $c{$s->relay}++;
>	END {
>		print sort { $a <=> $b } map {
>			sprintf "%6.2f %4d %4d %s\n",
>				$d{$_}/$c{$_}, $c{$_}, $d{$_}, $_}
>			} keys %d;
>	}
>__END__
>

I think that if you actualy benchmark this you will find as I have
that the all perl solution is much slower than the three process
pipe.

>
>use warnings;
>

agreed

>>         @field{qw(date host ident pid rest) } =
>>             ($line =~ m/
>>                 (?xo)                         # extended regular expression
>>                 ^                             # At the beginning of the line
>>                 (\w+\s+\d+\s+\d+:\d+:\d+) \s+ # match Mon Day HH:MM:SS
>>                 (\S+)                     \s+ # match host string
>>                 ([^\[:]+)                     # the ident tag
>>                 (?:\[(\d+)\])?                # the optional PID
>>                 :\s+                          # a colon and some space
>>                 (.+)$                         # the rest of the line
>>             /);
>
>Is there a reason to use (?xo) instead of /x?  There certainly isn't any reason
>to be using the /o modifyer here, as there's no interpolation going on.
>

I use /o much as I might put in the trailing comma in a list.
or a trailing semicolon in a block.  It does no harm and prevents
problems with some kinds of future modifications.

>>         eval qq/
>>             \@${newclass}::ISA = qw($class);
>>         / unless ($Class{$newclass});
>>         croak ("could not eval for ${newclass}::ISA: $@") if $@;
>
>Ewww.  What's wrong with using a symref here?
>
>	{ no strict refs; @{"${newclass}::ISA"} = ($class); }
>
>Symrefs may be bad, but eval is evil.
>

Maybe...  I'll think about it.  Eval is not evil just some uses of it are.

>> 
>>     $self->{'now'} = parsedate($self->date) unless ($self->{'now'});
>>     return $self->{'now'};
>> }
>
>Yet another place for ||=

Yea, I'll have to remember that for this kind of defaulting stuff.

>
>> 
>> sub AUTOLOAD {
>>     my $self = shift;
>>     my $type = ref($self)
>>                 or croak "$self is not an object";
>
>I don't see the point to this... you don't use $type anywhere, and the only
>place you use $self is to make a method call at the end, one which would be
>better off replaced with goto &$code.  Also, instead of just checking if the
>first argument is a ref, why not do something like:
>	croak "Argument 0 isn't a " . __PACKAGE__
>		unless ref($_[0]) && $_[0]->isa(__PACKAGE__);

Differing tastes.  I like to avoid using $_[0].  Go figure. 

>
>
>>     return if $AUTOLOAD =~ /::DESTROY$/;
>> 
>>     my $name = $AUTOLOAD;
>>     $name =~ s/.*://;
>
>I would do a bit of checking here...
>

Maybe so

>
>> 
>>     #
>>     # Call the new method
>>     #
>>     $self->$name(@_);
>
>If you hadn't messed with @_, you could simply do:
>
>	goto &$code;
>
>Which has the added benefit of causing the stack to appear as if the AUTOLOADED
>sub was called directly, rather than through autoload.  Not that it's important
>in *this* case, (since the sub is so simple) but it's a good habit to get into.
>

Maybe so. But I found the stack trace useful in debuging.  Also creating
this dynamic method turns out to be more work than is needed since most of the
time I only call it once for each instance anyway.

>>     my ($sec, $min, $hr, $day) = reverse (split (/[+:]/, $delay));
>
>What's wrong with:
>	my ($day, $hr, $min, $sec) = split /[+:]/, $delay, 4;
>

Perhaps you did not notice that day is sometimes absent from the sendmail
?delay= equates.  Your form would put $hour into $day and leave $sec empty
in most cases.

>-- 
>The longer a man is wrong, the surer he is that he's right.

Thanks for the code walk through. I like some of your ideas.
-- 
    This space intentionally left blank


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

Date: 20 Jun 2001 01:50:15 -0700
From: garry_short@hotmail.com (Garry)
Subject: Re: How do I get the current directory from Windows Explorer?
Message-Id: <bdcefd33.0106200050.27d6a586@posting.google.com>

Triffid Hunter <wjackso2@bigpond.net.au> wrote in message news:<3B2C9104.492F43B1@bigpond.net.au>...
> its in the titlebar... also as the currently selected item in the
> address combobox which would be a child of some kind of MFC toolbar
> object.. good luck!
> 

Unfortunately I'm no programmer, and I'm way out of my depth with that
kind of stuff! Luckily, I've just had a blinding flash of inspiration
- since the triggers are VOB-specific, I can make the VOB name one of
the arguments that are passed - problem sorted!

Thanks everyone for your help - just wish I knew enough to work out
how to follow your suggestions!

Regards,

Garry


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

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


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