[30681] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1926 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 16 03:09:38 2008

Date: Thu, 16 Oct 2008 00:09:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Thu, 16 Oct 2008     Volume: 11 Number: 1926

Today's topics:
    Re: crisis Perl sln@netherlands.com
    Re: crisis Perl <xahlee@gmail.com>
    Re: crisis Perl <cwilbur@chromatico.net>
    Re: crisis Perl <josef.moellers@fujitsu-siemens.com>
    Re: Help: How to process output of a program <tim@burlyhost.com>
        new CPAN modules on Thu Oct 16 2008 (Randal Schwartz)
    Re: Perl script: String comparison Ignoring spaces <cartercc@gmail.com>
    Re: Perl script: String comparison Ignoring spaces <tim@burlyhost.com>
    Re: Perl script: String comparison Ignoring spaces <anfi@onet.eu>
    Re: Text::CSV problem sln@netherlands.com
    Re: Text::CSV problem <tadmc@seesig.invalid>
    Re: Textmining with Lisp <xahlee@gmail.com>
        writing emacs commands with your fav lang <xahlee@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 15 Oct 2008 23:52:19 GMT
From: sln@netherlands.com
Subject: Re: crisis Perl
Message-Id: <4svcf4hak2kq2a5t69a5vsrutm8g68uimc@4ax.com>

On Wed, 15 Oct 2008 06:53:09 -0700 (PDT), cartercc <cartercc@gmail.com> wrote:

>Over the past four years, I've written a fair amount of Perl. Some of
>it has been 'crisis Perl'. This results in scripts that solve a
>problem but are thrown together in a hurry with inefficient, untested,
>and confusing code. When the crisis resolved, I wanted to go back, do
>real testing, and rewrite the script, but have been told on almost
>every occasion to leave it alone. The fact that it seemed to worked
>was good enough, and most of this code found its way into production.
>
>In the past month or so, I've had to look at four scripts I have
>written this way. One script was over two years old, and the newest
>was several months old. Needless to say, dealing with confusing,
>uncommented, and inefficient code is a problem! It would have been
>much easier to clean up the code when it was written than to rewrite
>it months after the fact.
>
>I actually knew better than to not clean up the code, but it was
>easier at the time not to pick a fight with my managers. This isn't an
>excuse, but an explanation.
>
>How do you deal with a manager who tells you to leave a script alone,
>when you know good and well that it's such poorly written code that it
>will be extremely hard to maintain, and perhaps will be buggy as well?
>Getting another job isn't an option, and firing the manager isn't an
>option, either.
>
>CC

First, I just want to tell you, none of the other replies to your
post you should even consider. Not even the one recommending you 
write better code or learn from your mistakes.

You should not mention, nor try to coerce any response from your
manager, concerning the desire to redo something you know you could
do better the second time around, whatsover!

If your manager contacts you about a bug that comes up with your
legacy code, act shocked, don't say anything at all, look at the
code, find the bug and fix it... AND NOTHING ELSE !!!
Don't try to rewrite sections that look shabby or any crap like that.

Fix it, test it, then LET IT GO man! Give the impression to your
manager the code is otherwise perfect.

Remember, if your company does validation, you don't wan't to change
anything unless you have to after its been in production. To do so
would risk getting fired, and I'm sure you don't wan't that.

If your code falls under the umbrella of an overhaul, then by all means,
if you have the time, redo as much as you feel comfortable with.
But remember, it will have to be validated again.

I won't mean jack to your company how much you 'clean up' old code,
they only care about money ....

sln





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

Date: Wed, 15 Oct 2008 18:25:53 -0700 (PDT)
From: "xahlee@gmail.com" <xahlee@gmail.com>
Subject: Re: crisis Perl
Message-Id: <3596c3c3-a755-486f-8d13-67ea399fbc5a@31g2000prz.googlegroups.com>

On Oct 15, 6:53 am, cartercc <carte...@gmail.com> wrote:
> Over the past four years, I've written a fair amount of Perl. Some of
> it has been 'crisis Perl'. This results in scripts that solve a
> problem but are thrown together in a hurry with inefficient, untested,
> and confusing code. When the crisis resolved, I wanted to go back, do
> real testing, and rewrite the script, but have been told on almost
> every occasion to leave it alone. The fact that it seemed to worked
> was good enough, and most of this code found its way into production.
>
> In the past month or so, I've had to look at four scripts I have
> written this way. One script was over two years old, and the newest
> was several months old. Needless to say, dealing with confusing,
> uncommented, and inefficient code is a problem! It would have been
> much easier to clean up the code when it was written than to rewrite
> it months after the fact.
>
> I actually knew better than to not clean up the code, but it was
> easier at the time not to pick a fight with my managers. This isn't an
> excuse, but an explanation.
>
> How do you deal with a manager who tells you to leave a script alone,
> when you know good and well that it's such poorly written code that it
> will be extremely hard to maintain, and perhaps will be buggy as well?
> Getting another job isn't an option, and firing the manager isn't an
> option, either.

in general, that's how real word code works. In my experience, almost
all production code are like that. Live with it.

The time when your desire to rewrite is appreciated, is when things
broke or things need change. When that happens, they will come to you
first, or you may have already moved on.

Maybe one day you'll become a manager, and you'll probably make the
same choice.

  Xah
=E2=88=91 http://xahlee.org/

=E2=98=84


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

Date: Thu, 16 Oct 2008 00:48:01 -0400
From: Charlton Wilbur <cwilbur@chromatico.net>
Subject: Re: crisis Perl
Message-Id: <86wsg9qh6m.fsf@mithril.chromatico.net>

>>>>> "cc" == cartercc  <cartercc@gmail.com> writes:

    cc> How do you deal with a manager who tells you to leave a script
    cc> alone, when you know good and well that it's such poorly written
    cc> code that it will be extremely hard to maintain, and perhaps
    cc> will be buggy as well?  Getting another job isn't an option, and
    cc> firing the manager isn't an option, either.

Educate the manager.  Keeping shoddy code in production is a gamble:
you're gambling that the cost of fixing the code *now* is higher than
the cost of fixing it when it breaks or when there's a crisis.  This is
almost never the case, and a competent manager will realize this.  But
the risk of fixing the code now is that you'll break something that's
working, and that's often the key factor in "if it ain't broke, don't
fix it" decisions.

Put together a test suite that tests the existing code for correct
behavior, after establishing exactly what the desired behavior is.  Be
complete in your tests, and use any available tools to make sure that
the code is all tested.  Then replace the scripts one by one, making
sure that they pass the test suite.  You'll probably find a lot of bugs
this way.

The test suite is an asset to the code base, especially when it's
automated, because you can verify that any code you change doesn't break
anything that was previously broken.  And when you *do* break something
that was previously broken but wasn't tested, you can add to the test
suite.  

And then, the next time you have a crisis, you can add tests
immediately.  Or, when you're doing development at a sane pace, you can
write the tests first.  

Charlton


-- 
Charlton Wilbur
cwilbur@chromatico.net


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

Date: Thu, 16 Oct 2008 08:56:22 +0200
From: Josef Moellers <josef.moellers@fujitsu-siemens.com>
Subject: Re: crisis Perl
Message-Id: <gd6oie$kdg$1@nntp.fujitsu-siemens.com>

Following up on myself, after having given it some more thoughts:

Josef Moellers wrote:
> cartercc wrote:
> 
>> How do you deal with a manager who tells you to leave a script alone,
>> when you know good and well that it's such poorly written code that it
>> will be extremely hard to maintain, and perhaps will be buggy as well?
>> Getting another job isn't an option, and firing the manager isn't an
>> option, either.
> 
> Just give it to him in writing. If you later get into trouble, at least 
> you told him before! Maybe one day he'll listen.

Maybe the best advice, which will not help you in this case, is that you 
should force yourself to write good, well documented code in the first 
place. Make it a habit, even for Q&D programlets: write down what the 
program does, add a comment every time you have to think even for the 
fraction of a second about how to do something, use boilerplates for 
subs, ... the lot.. Depending upon your workload, you'll never get the 
chance to clean it up afterwards, but you'll most likely will have to 
return and fix it.
Writing good, well documented code does not take much longer than 
hacking together bad, sloppy, undocumented code, but the resulting code 
will be of much better quality, as you will have given it a second 
thought when you wrote the documentation (i.e. comments, POD, ...).

I may sound sarcastic, but maybe it's a lesson that you have learned the 
hard way.

I, for one, have.

Josef
-- 
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize (T.  Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html


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

Date: Wed, 15 Oct 2008 11:20:11 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Help: How to process output of a program
Message-Id: <wvqJk.2050$vs7.1229@newsfe04.iad>

xhoster@gmail.com wrote:

> Tim Greer <tim@burlyhost.com> wrote:
>>
>> Also, IPC::Open2 does work on Win32 or am I mistaken?
> 
> Based on my test, I'd say it doesn't work on Win32, or at least not
> well.
> 
> use strict;
> use warnings;
> use IPC::Open2;
> $|=1;
> warn open2 my $in, my $out, q{sort};
> print $out qq{$_\n} foreach (1..100);
> close $out or die;
> print while (<$in>);
> 
> This works on Linux, but hangs on Windows.  It hangs at the while
> (<$in>). Since sort needs to read all input before generating any
> output,
> deadlock should not be an issue.  It also leaves sort.exe hanging
> around.
> 
> (I actually starting using "cat"/"type", using the >& to redirect to
> another filehandle to avoid deadlocks, but I couldn't figure out how
> to
> make Window's "type" read from its stdin.  So I switched to sort. 
> Then
> once using sort, I realized I no longer needed >& so simplified it. 
> None of these steps worked on windows).
> 
> This is perl, v5.8.8 built for MSWin32-x86-multi-thread
> (with 25 registered patches, see perl -V for more detail)
> 

Interesting.  I've honestly not tried this on Win32.  I've been
privileged enough to _not_ have to use Windows for many, many years. 
Maybe I'll install ActiveState's Perl on a Windows laptop and run some
tests (but I probably won't).  I appreciate the follow up.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Thu, 16 Oct 2008 04:42:23 GMT
From: merlyn@stonehenge.com (Randal Schwartz)
Subject: new CPAN modules on Thu Oct 16 2008
Message-Id: <K8tEEn.vBu@zorch.sf-bay.org>

The following modules have recently been added to or updated in the
Comprehensive Perl Archive Network (CPAN).  You can install them using the
instructions in the 'perlmodinstall' page included with your Perl
distribution.

Acme-Magic8Ball-1.2
http://search.cpan.org/~simonw/Acme-Magic8Ball-1.2/
ask the Magic 8 Ball a question 
----
Activator-0.91
http://search.cpan.org/~knassar/Activator-0.91/
Development Framework - Object Oriented framework to ease creation and rapid development of multi-developer distributed mixed environment perl based software projects, especially Catalyst based websit
----
Alien-wxWidgets-0.40
http://search.cpan.org/~mbarbon/Alien-wxWidgets-0.40/
building, finding and using wxWidgets binaries 
----
Apache2-ASP-2.00_01
http://search.cpan.org/~johnd/Apache2-ASP-2.00_01/
ASP for Perl, reloaded. 
----
Apache2-ASP-2.00_02
http://search.cpan.org/~johnd/Apache2-ASP-2.00_02/
ASP for Perl, reloaded. 
----
B-Hooks-OP-Check-0.10
http://search.cpan.org/~flora/B-Hooks-OP-Check-0.10/
Wrap OP check callbacks 
----
BerkeleyDB-Manager-0.07
http://search.cpan.org/~nuffin/BerkeleyDB-Manager-0.07/
General purpose BerkeleyDB wrapper 
----
Catalyst-Runtime-5.7015
http://search.cpan.org/~mramberg/Catalyst-Runtime-5.7015/
Catalyst Runtime version 
----
Continuity-0.996
http://search.cpan.org/~awwaiid/Continuity-0.996/
Abstract away statelessness of HTTP, for stateful Web applications 
----
Continuity-Monitor-0.01
http://search.cpan.org/~awwaiid/Continuity-Monitor-0.01/
monitor and inspect a Continuity server 
----
DBD-Pg-2.11.2
http://search.cpan.org/~turnstep/DBD-Pg-2.11.2/
PostgreSQL database driver for the DBI module 
----
DTS-0.06
http://search.cpan.org/~arfreitas/DTS-0.06/
Perl classes to access Microsoft SQL Server 2000 DTS Packages 
----
Email-Blaster-0.0001_01
http://search.cpan.org/~johnd/Email-Blaster-0.0001_01/
Scalable Mass Email System 
----
ExtUtils-FakeConfig-0.12
http://search.cpan.org/~mbarbon/ExtUtils-FakeConfig-0.12/
override %Config values on-the-fly 
----
Ezmlm-0.08.2
http://search.cpan.org/~sumpfrall/Ezmlm-0.08.2/
Object Methods for Ezmlm Mailing Lists 
----
FCGI-Async-0.15
http://search.cpan.org/~pevans/FCGI-Async-0.15/
Module to allow use of FastCGI asynchronously 
----
File-Fingerprint-0.10_01
http://search.cpan.org/~bdfoy/File-Fingerprint-0.10_01/
This is the description 
----
File-ShareDir-PAR-0.02
http://search.cpan.org/~smueller/File-ShareDir-PAR-0.02/
File::ShareDir with PAR support 
----
Finance-Quote-1.14
http://search.cpan.org/~ecocode/Finance-Quote-1.14/
Get stock and mutual fund quotes from various exchanges 
----
HTML-Tested-0.42
http://search.cpan.org/~bosu/HTML-Tested-0.42/
Provides HTML widgets with the built-in means of testing. 
----
IPC-Run-0.81_01
http://search.cpan.org/~adamk/IPC-Run-0.81_01/
system() and background procs w/ piping, redirs, ptys (Unix, Win32) 
----
IPC-SRLock-0.1.75
http://search.cpan.org/~pjfl/IPC-SRLock-0.1.75/
Set/reset locking semantics to single thread processes 
----
List-Rubyish-0.01
http://search.cpan.org/~yappo/List-Rubyish-0.01/
Array iterator like the Ruby 
----
Math-Integral-Romberg-0.03
http://search.cpan.org/~boesch/Math-Integral-Romberg-0.03/
Scalar numerical integration 
----
Math-Integral-Romberg-0.04
http://search.cpan.org/~boesch/Math-Integral-Romberg-0.04/
Scalar numerical integration 
----
MooseX-Role-Cmd-0.02
http://search.cpan.org/~edenc/MooseX-Role-Cmd-0.02/
Wrap system command binaries the Moose way 
----
MyCPAN-Indexer-1.16_02
http://search.cpan.org/~bdfoy/MyCPAN-Indexer-1.16_02/
Index a Perl distribution 
----
Net-DNS-ToolKit-0.40
http://search.cpan.org/~miker/Net-DNS-ToolKit-0.40/
tools for working with DNS packets 
----
Net-DNS-ToolKit-0.41
http://search.cpan.org/~miker/Net-DNS-ToolKit-0.41/
tools for working with DNS packets 
----
Net-LDAP-Class-0.12
http://search.cpan.org/~karman/Net-LDAP-Class-0.12/
object-relational mapper for Net::LDAP 
----
Net-UPnP-1.41
http://search.cpan.org/~skonno/Net-UPnP-1.41/
Perl extension for UPnP 
----
PDL-Graphics-PLplot-0.37
http://search.cpan.org/~dhunt/PDL-Graphics-PLplot-0.37/
Object-oriented interface from perl/PDL to the PLPLOT plotting library 
----
PDL-Graphics-PLplot-Map-0.10
http://search.cpan.org/~dhunt/PDL-Graphics-PLplot-Map-0.10/
Interface to the GMT coastline database for plotting maps 
----
POE-Component-IRC-Plugin-BaseWrap-0.008
http://search.cpan.org/~zoffix/POE-Component-IRC-Plugin-BaseWrap-0.008/
base class for IRC plugins which need triggers/ban/root control 
----
POE-Component-IRC-Plugin-Fortune-0.0101
http://search.cpan.org/~zoffix/POE-Component-IRC-Plugin-Fortune-0.0101/
fortune cookies plugin for IRC 
----
POE-Component-IRC-Plugin-Magic8Ball-0.0101
http://search.cpan.org/~zoffix/POE-Component-IRC-Plugin-Magic8Ball-0.0101/
Magic 8 Ball plugin for IRC 
----
POE-Component-Server-SimpleSMTP-1.26
http://search.cpan.org/~bingos/POE-Component-Server-SimpleSMTP-1.26/
A simple to use POE SMTP Server. 
----
POE-Component-Server-SimpleSMTP-1.28
http://search.cpan.org/~bingos/POE-Component-Server-SimpleSMTP-1.28/
A simple to use POE SMTP Server. 
----
POE-Component-Server-SimpleSMTP-1.30
http://search.cpan.org/~bingos/POE-Component-Server-SimpleSMTP-1.30/
A simple to use POE SMTP Server. 
----
POE-Component-WWW-Google-PageRank-0.04
http://search.cpan.org/~zoffix/POE-Component-WWW-Google-PageRank-0.04/
A non-blocking wrapper for WWW::Google::PageRank 
----
Parse-Stallion-0.1
http://search.cpan.org/~arthur/Parse-Stallion-0.1/
Backtracking parser with during or after evaluation 
----
Perl-Dist-1.06
http://search.cpan.org/~adamk/Perl-Dist-1.06/
Perl Distribution Creation Toolkit 
----
Rose-HTMLx-Form-Related-0.09
http://search.cpan.org/~karman/Rose-HTMLx-Form-Related-0.09/
RHTMLO forms, living together 
----
String-Util-0-12
http://search.cpan.org/~miko/String-Util-0-12/
----
Test-Aggregate-0.34_09
http://search.cpan.org/~ovid/Test-Aggregate-0.34_09/
Aggregate *.t tests to make them run faster. 
----
Test-Fork-0.02
http://search.cpan.org/~mschwern/Test-Fork-0.02/
test code which forks 
----
Test-Most-0.12
http://search.cpan.org/~ovid/Test-Most-0.12/
Most commonly needed test functions and features. 
----
Test-Simple-0.82
http://search.cpan.org/~mschwern/Test-Simple-0.82/
Basic utilities for writing tests. 
----
Test-Simple-0.84
http://search.cpan.org/~mschwern/Test-Simple-0.84/
Basic utilities for writing tests. 
----
Text-CSV_XS-0.55
http://search.cpan.org/~hmbrand/Text-CSV_XS-0.55/
comma-separated values manipulation routines 
----
WWW-Facebook-API-0.4.14
http://search.cpan.org/~clscott/WWW-Facebook-API-0.4.14/
Facebook API implementation 
----
WWW-Netflix-API-0.04
http://search.cpan.org/~davidrw/WWW-Netflix-API-0.04/
Interface for Netflix's API 
----
XML-Feed-0.20
http://search.cpan.org/~simonw/XML-Feed-0.20/
Syndication feed parser and auto-discovery 
----
XML-Feed-0.21
http://search.cpan.org/~simonw/XML-Feed-0.21/
Syndication feed parser and auto-discovery 
----
checkbot-1.80
http://search.cpan.org/~jjdg/checkbot-1.80/
----
flail-0.2.5
http://search.cpan.org/~attila/flail-0.2.5/
a hacker's mailer in Perl 
----
genpass-0.04
http://search.cpan.org/~xsawyerx/genpass-0.04/
Quickly create secure passwords 
----
genpass-0.05
http://search.cpan.org/~xsawyerx/genpass-0.05/
Quickly create secure passwords 
----
indirect-0.07_01
http://search.cpan.org/~vpit/indirect-0.07_01/
Lexically warn about using the indirect object syntax. 
----
indirect-0.07_02
http://search.cpan.org/~vpit/indirect-0.07_02/
Lexically warn about using the indirect object syntax. 


If you're an author of one of these modules, please submit a detailed
announcement to comp.lang.perl.announce, and we'll pass it along.

This message was generated by a Perl program described in my Linux
Magazine column, which can be found on-line (along with more than
200 other freely available past column articles) at
  http://www.stonehenge.com/merlyn/LinuxMag/col82.html

print "Just another Perl hacker," # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion


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

Date: Wed, 15 Oct 2008 12:05:31 -0700 (PDT)
From: cartercc <cartercc@gmail.com>
Subject: Re: Perl script: String comparison Ignoring spaces
Message-Id: <6f703acb-3e42-4da4-956f-2a68ed0401a0@a70g2000hsh.googlegroups.com>

On Oct 15, 8:43 am, khan <mushtaqk...@gmail.com> wrote:
> I read each line
> of file, split the line in to variables and compare against the
> predefined tokens. Comparison fails if variable in file has some
> spaces around it.
>
> Eg: Line read from file
>     Jhon: jack:hill;
>     spilt(/:/);
>     if("jack" eq "$_[1])
>     #Above comparison fails as $_[1] value is " jack"

You can use the match operator, like this:

if ($_[1] =~ /jack/i) { do_something(); }

CC


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

Date: Wed, 15 Oct 2008 13:15:10 -0700
From: Tim Greer <tim@burlyhost.com>
Subject: Re: Perl script: String comparison Ignoring spaces
Message-Id: <jbsJk.3391$vs7.779@newsfe04.iad>

khan wrote:

> Hi,
> 
> Iam new to Perl Script, am writing a perl script to read a
> configuration file and take some actions accordingly. I read each line
> of file, split the line in to variables and compare against the
> predefined tokens. Comparison fails if variable in file has some
> spaces around it.
> 
> Eg: Line read from file
>     Jhon: jack:hill;
>     spilt(/:/);
>     if("jack" eq "$_[1])
>     #Above comparison fails as $_[1] value is " jack"
> 
> Please let me know a solution to compare string variables ignoring
> spaces around the variables.
> 
> Thanks,
> -Mushtaq Khan

Just strip the leading and trailing white space, or all white space, and
then compare.
-- 
Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
and Custom Hosting.  24/7 support, 30 day guarantee, secure servers.
Industry's most experienced staff! -- Web Hosting With Muscle!


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

Date: Wed, 15 Oct 2008 22:22:26 +0200
From: Andrzej Adam Filip <anfi@onet.eu>
Subject: Re: Perl script: String comparison Ignoring spaces
Message-Id: <xzjztvdv78@kevin.huge.strangled.net>

khan <mushtaqk921@gmail.com> wrote:

> Hi,
>
> Iam new to Perl Script, am writing a perl script to read a
> configuration file and take some actions accordingly. I read each line
> of file, split the line in to variables and compare against the
> predefined tokens. Comparison fails if variable in file has some
> spaces around it.
>
> Eg: Line read from file
>     Jhon: jack:hill;
>     spilt(/:/);
>     if("jack" eq "$_[1])
>     #Above comparison fails as $_[1] value is " jack"
>
> Please let me know a solution to compare string variables ignoring
> spaces around the variables.

You may treat spaces around separator (":") as part of separator.

spilt(/ *: */);

-- 
[pl>en Andrew] Andrzej Adam Filip : anfi@onet.eu : anfi@xl.wp.pl
I wasn't kissing her, I was whispering in her mouth.
  -- Chico Marx


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

Date: Wed, 15 Oct 2008 22:56:51 GMT
From: sln@netherlands.com
Subject: Re: Text::CSV problem
Message-Id: <m9ocf41j9aj0a9p23dsrgemo1fs284lm7l@4ax.com>

On 15 Oct 2008 12:38:31 GMT, Natxo Asenjo <natxete@asenjo.nl.invalid> wrote:

>hi,
>
>I need to check the status of some schedule tasks in a windows server. At
>my $JOB we use nagios, so I thouth, let's write a plugin (I could not
>find anything at the nagiosexchange).
>
>windows 2k3 has a command schtasks. I can dump the status of everything
>like this:
>
>schtasks /query /fo csv /v > file.csv
>
>the /fo switch is for the format and /v switch makes it verbose. this is
>the only way to know if the task has run or not.
>
>The output file looks like this (output truncated):
>
>(1st line)
>"HostName","TaskName","Next Run Time","Status","Logon
>Mode","Last Run Time","Last Result","Creator","Schedule","Task
>To Run","Start In","Comment","Scheduled Task State","Scheduled
>Type","Start Time","Start Date","End Date","Days","Months","Run
>As User","Delete Task If Not Rescheduled","Stop Task
>If Runs X Hours and X Mins","Repeat: Every","Repeat:
>Until: Time","Repeat: Until: Duration","Repeat:
>Stop If Still Running","Idle Time","Power Management"
>
>(2nd line)
>"server","jobname","09:00:00, 16-10-2008","","Interactive
>only","09:00:00, 07-10-2008","0","user","At 09:00
>every day, starting 18-01-2008","C:\Program Files\SQLyog
>Enterprise\sja.exe "afdgroep_progbeh.xml" -l"C:\Documents and
>Settings\user\Application Data\SQLyog\sja.log"
>-s"C:\Documents and Settings\user\Application
>Data\SQLyog\sjasession.xml"","N/A","N/A","Enabled","Daily
>","09:00:00","18-01-2008","N/A","Everyday","N/A","domain\administrator","Disabled","Disabled","Disabled","Disabled","Disabled","Disabled","Disabled","Disabled
>
>(no, I did not write this scheduled job)
>
>using TEXT::CSV I can parse the first line, but it stops with the
>second:
>
>#!perl
>use warnings;
>use strict;
>use Text::CSV;
>
>my $csv_file = "c:/tmp/dump.csv";
>
>open (CSV, "<", $csv_file) or die "$!\n" ;
>
>my $csv_object = Text::CSV->new();
>
>while (<CSV>) {
>    if ($csv_object->parse($_)) {
>        my @columns = $csv_object->fields();
>        print "@columns\n" ;
>    }
>    else {
>        my $error = $csv_object->error_diag();
>        print "oeps: $error\n";
>    }
>}
>
>again sorry, all truncated (very long lines)
>C:\tmp>test.pl 
>HostName TaskName Next Run Time Status Logon Mode Last Run Time Last
>Result Crea
>tor Schedule Task To Run Start In Comment Scheduled Task State Scheduled
>Type St
>art Time Start Date End Date Days Months Run As User Delete Task If Not
>Reschedu
>led Stop Task If Runs X Hours and X Mins Repeat: Every Repeat: Until:
>Time Repea
>t: Until: Duration Repeat: Stop If Still Running Idle Time Power
>Management
>opes:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>oeps:
>
>I think it has to do with the long paths in the task to run field,
>because when I try the same code at another machine with a 'normal'
>(shorter) path to run, I get the desired output.
>
>TIA

You have a shit csv generator !!!
Your csv generator did not escape intended double quotes insind one of the 2nd record fields.
That field is 'Task To Run'. In addition, you are missing field 'Start In'.

This is an indication of corruption or bad generator code.
IMO, because you span lines, there is only one EOR. 
That being the intersection of even double quotes and eol.
Maybe there is a pattern discernable by CSV parsers.
If there is, the easiest test is to try to bring it into Excel.
I don't see one however.

The flaw is that this:

"server","jobname","09:00:00, 16-10-2008","","Interactive
only","09:00:00, 07-10-2008","0","user","At 09:00
every day, starting 18-01-2008","C:\Program Files\SQLyog

contains the intersection of even number of "'s with eol,
but does not equal EOR.

The flaw is really in your csv generator. The even number of records
condition I let remain, and added a " at eol condition to counteract
your shit csv generator. This however, even though it will work in all
valid case, should NOT be the job of the parser, it should be the 
CSV generators job! I don't like adding this condition at all.
It's not a pragmatic approach.
(see below)

sln

#############
# Csv4 Regex
#############

use strict;
use warnings;

my $fname = 'c:\temp\junk.csv';
open CSV, $fname or die "can't open $fname...";

my ($row, $tmp) = ('','');
my ($parsing, $records, $quotes) = (1,1,0);

while ($parsing)
{
	## Buffer until a full row
	## -------------------------
	if (!($_ = <CSV>)) {
		$parsing = 0; # eof, parse what's left
	} else {
		$tmp = $_;

		## this block will trim newlines ---
		  $tmp =~ s/\s+$//s;
		  next if (!length($tmp));
		  $row .= " $tmp";
		## ---

		## this block will keep newlines ---
		  # $row .= $tmp;
		## ---

		$quotes += $tmp =~ tr/"//;

		if (!($quotes % 2 == 0 &&	# Even number of double quotes?
		      $tmp =~/"$/))		# " at eol? <-- WHAT A SHIT CSV GENERATOR !!!!!!!
		{				# Good to go, parse it ...
			next;
		}
	}

	print " (".$records++.") ----------\n";

	## Parse the row
	## -------------------
	while ($row =~ /\s*"\s*(.*?)\s*"\s*,|\s*"\s*(.*?)\s*"\s*$/gs)   # span lines
	{
		my $val = $1;
		$val = $2 unless (defined $val);
		# clean up double quotes
		$val =~ s/""/"/g;
		print "val = $val\n";
	}
	$row = '';
	$quotes = 0;
}
close CSV;

__END__

Output:


C:\temp>perl csv4.pl
 (1) ----------
val = HostName
val = TaskName
val = Next Run Time
val = Status
val = Logon Mode
val = Last Run Time
val = Last Result
val = Creator
val = Schedule
val = Task To Run
val = Start In
val = Comment
val = Scheduled Task State
val = Scheduled Type
val = Start Time
val = Start Date
val = End Date
val = Days
val = Months
val = Run As User
val = Delete Task If Not Rescheduled
val = Stop Task If Runs X Hours and X Mins
val = Repeat: Every
val = Repeat: Until: Time
val = Repeat: Until: Duration
val = Repeat: Stop If Still Running
val = Idle Time
val = Power Management
 (2) ----------
val = server
val = jobname
val = 09:00:00, 16-10-2008
val =
val = Interactive only
val = 09:00:00, 07-10-2008
val = 0
val = user
val = At 09:00 every day, starting 18-01-2008
val = C:\Program Files\SQLyog Enterprise\sja.exe "afdgroep_progbeh.xml" -l"C:\Do
cuments and Settings\user\Application Data\SQLyog\sja.log" -s"C:\Documents and S
ettings\user\Application Data\SQLyog\sjasession.xml"
val = N/A
val = N/A
val = Enabled
val = Daily
val = 09:00:00
val = 18-01-2008
val = N/A
val = Everyday
val = N/A
val = domain\administrator
val = Disabled
val = Disabled
val = Disabled
val = Disabled
val = Disabled
val = Disabled
val = Disabled

C:\temp>

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

"server","jobname","09:00:00, 16-10-2008","","Interactive
only","09:00:00, 07-10-2008","0","user","At 09:00
every day, starting 18-01-2008","C:\Program Files\SQLyog

^^^^^^^^^^ this is the main culprit, even number of " with eol (but NOT EOR)

"C:\Program Files\SQLyogEnterprise\sja.exe "afdgroep_progbeh.xml" -l"C:\Documents andSettings\user\Application Data\SQLyog\sja.log"-s"C:\Documents and
Settings\user\ApplicationData\SQLyog\sjasession.xml"",

^^^^^^^^^^^ this is the other culprit, double quotes not escaped

"N/A",
"N/A",
"Enabled",
"Daily",
"09:00:00",
"18-01-2008",
"N/A",
"Everyday",
"N/A",
"domain\administrator",
"Disabled",
"Disabled",
"Disabled",
"Disabled",
"Disabled",
"Disabled",
"Disabled",
"Disabled




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

Date: Wed, 15 Oct 2008 20:22:15 -0500
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: Text::CSV problem
Message-Id: <slrngfd5q7.fpk.tadmc@tadmc30.sbcglobal.net>

sln@netherlands.com <sln@netherlands.com> wrote:


> 		  $tmp =~ s/\s+$//s;
                                  ^
                                  ^

What is the point of the s modifier here?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


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

Date: Wed, 15 Oct 2008 16:27:32 -0700 (PDT)
From: "xahlee@gmail.com" <xahlee@gmail.com>
Subject: Re: Textmining with Lisp
Message-Id: <bab0b313-7afc-464f-b66f-715c952bebaa@u27g2000pro.googlegroups.com>

On Oct 15, 1:48 pm, Francogrex <fra...@grex.org> wrote:
> Hi all, I'm trying to write a little program that searches through a
> text for a specific word and then outputs the sentence containg the
> word as well as the sentence before and the sentence after; for
> example the little text below (stored in "c:/target.txt"):
>
> I wandered lonely as a cloud
> That floats on high over vales and hills,
> When all at once I saw a crowd,
> A host, of golden daffodils;
> Beside the lake, beneath the trees,
> Fluttering and dancing in the breeze.
> William Wordsworth
>
> Now if I search for the word daffodils I should get as output:
>
> > When all at once I saw a crowd, A host, of golden daffodils;Beside the =
lake, beneath the trees,

this is much easier in emacs lisp.

Here's the code that does what u want, quickly coded in 11 minutes.
(writing this post took more than 11 min.)

(defun my-search (file word)
  "search `file' and return adjacent lines containing the `word'.
`file' is a file path. `word' is a string."
 (interactive)
 (let (neighborLines p1 p2)
    (find-file file)
    (while (search-forward word nil t)
      (save-excursion
        (move-beginning-of-line 1)
        (previous-line)
        (setq p1 (point))
        (next-line 2)
        (move-end-of-line 1)
        (setq p2 (point))
        (setq neighborLines (buffer-substring-no-properties p1 p2))
        (print neighborLines)
        )
      )
))

(my-search "xx.el" "daffodils")

as you can see, the code is geared toward text processing, so you
don't have to deal with nitty-gritty details. (for example, it has
primitives that deals with lines, sentences, syntax, and handles file
encoding, backup, access, permissions etc all automatically)

Basically, elisp system is the best text processing language, even
more powerful than Perl.

See also:

=E2=80=A2 Text Processing: Emacs Lisp vs Perl
http://xahlee.org/emacs/elisp_text_processing_lang.html

=E2=80=A2 Generate a Web Links Report with Emacs Lisp
http://xahlee.org/emacs/elisp_link_report.html

  Xah
=E2=88=91 http://xahlee.org/

=E2=98=84



> In a rather "lame" attempt I tried to write this:
>  (with-open-file (s-in "c:/target.txt" :direction :input)
>                  (do ((line1 (read-line s-in nil nil) (read-line s-in nil=
 nil))
>                       (line2 (read-line s-in nil nil) (read-line s-in nil=
 nil))
>                       (line3 (read-line s-in nil nil) (read-line s-in nil=
 nil)))
>                      ((not line1))
>                      (setf cp1 (search "daffodils" line1 :test #'char-equ=
al))
>                      (if (not (null cp1))
>                          (format t "~A ~A ~A~%"
>                                  line1 line2 line3))
>                      (setf cp2 (search "daffodils" line2 :test #'char-equ=
al))
>                      (if (not (null cp2))
>                          (format t "~A ~A ~A~%"
>                                  line1 line2 line3)
>                        (setf cp3 (search "daffodils" line3 :test #'char-e=
qual)))
>                      (if (not (null cp3))
>                          (format t "~A ~A ~A~%"
>                                  line1 line2 line3))))
> But the output is:
>
> > A host, of golden daffodils; Beside the lake, beneath the trees, Flutte=
ring and dancing in the breeze.
>
> So it's outputting 3 stences alright, but not the one before and the
> one after (understandably because the function just stores 3 sentences
> at a time).
>
> Can someone help me modify this a little to get what I need (knowing
> that the aim afterwards is to go through a large text where the word
> can occur many times). Thanks




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

Date: Wed, 15 Oct 2008 17:55:39 -0700 (PDT)
From: "xahlee@gmail.com" <xahlee@gmail.com>
Subject: writing emacs commands with your fav lang
Message-Id: <e497ee4d-cba1-4589-a22b-f0bbe373a687@a3g2000prm.googlegroups.com>

Here's a little tutorial that lets you write emacs commands for
processing the current text selection in emacs in your favorite lang.

Elisp Wrapper For Perl Scripts
http://xahlee.org/emacs/elisp_perl_wrapper.html

plain text version follows.
-------------------------------------
Elisp Wrapper For Perl Scripts

Xah Lee, 2008-10

This page shows a example of writing a emacs lisp function that
process text on the current region, by calling a external perl script.
So that you can use your existing knowledge in a scripting language
for text processing as emacs commands.

THE PROBLEM

Elisp is great and powerful, but if you are new, it may take several
months for you to actually become productive in using it for text
processing. However, you are probably familiar with a existing
language, such as Perl, PHP, Python, Ruby. It would be great if you
can use your existing knowledge to write many text processing scripts,
and make them available in emacs as commands, so that you can just
select a section of text, press a key, then the selected text will be
transformed according to one of your script.

SOLUTION

Basically, all your elisp function has to do is to grab the current
region, then pass the text to a external program. The external program
will take the input thru Stdin=E2=86=97, then produce the processed result =
in
Stdout. The elisp function will grab the text from the script's
Stdout, then replace the current region by that text. Lucky for us,
the elisp function shell-command-on-region already does this exactly.

For your script, its should takes input from Stdin and oput to Stdout.
For simplicity, let's assume your script is the unix program =E2=80=9Cwc=E2=
=80=9D,
which takes input from Stdin and output a text to Stdout. (the =E2=80=9Cwc=
=E2=80=9D
command counts the number of words, lines, chars in the text.) For
example, try this: =E2=80=9Ccat =E2=80=B9file name=E2=80=BA | wc=E2=80=9D.

Here's the elisp wrapper:

(defun my-process-region (startPos endPos) "Do some text processing on
region.  This command calls the external script =E2=80=9Cwc=E2=80=9D."  (in=
teractive
"r") (let (scriptName) (setq scriptName "/usr/bin/wc") ; full path to
your script (shell-command-on-region startPos endPos scriptName nil t
nil t) ))

You can assign a keyboard shortcut to it:

(global-set-key (kbd "<F6>") 'my-process-region)

Put the above code in your =E2=80=9C.emacs=E2=80=9D then restart emacs. To =
use your
function, first select a region of text, then press the F6 key.

With the above, you can write many little text processing scripts in
your favorite language, and have them all available in emacs as
commands.

For how to define keyboard shortcuts with other keys, see: How to
Define Keyboard Shortcuts in Emacs.

  Xah
=E2=88=91 http://xahlee.org/

=E2=98=84


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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V11 Issue 1926
***************************************


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