[26837] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8864 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 18 14:05:18 2006

Date: Wed, 18 Jan 2006 11:05:05 -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, 18 Jan 2006     Volume: 10 Number: 8864

Today's topics:
    Re: FAQ 3.5 How do I debug my Perl programs? <emschwar@mail.ericschwartz.us>
    Re: FAQ 3.5 How do I debug my Perl programs? <jgibson@mail.arc.nasa.gov>
        Open Source Blog SW wanted <RemoveThisPrefixmatts2nospam@ix.netcom.com>
    Re: Scanning file version 2.0 <thundergnat@hotmail.com>
    Re: Script execution blocked when reading from a socket <nospam@thanksanyway.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 18 Jan 2006 08:01:36 -0700
From: Eric Schwartz <emschwar@mail.ericschwartz.us>
Subject: Re: FAQ 3.5 How do I debug my Perl programs?
Message-Id: <etoy81dvaun.fsf@localhost.localdomain>

Alan Stewart <astewart1@cox.net> writes:
> On Tue, 17 Jan 2006 19:06:00 -0500, "Matt Garrish"
> <matthew.garrish@sympatico.ca> wrote:
> >I agree completely with you. I tried the debugger a few times when I first 
> >started with Perl, found it rather useless and never went back. Strictures 
> 
> or how about Devel::ptkdb for a visual debugger . . .

Yes, I never used (and mostly hate) the Perl debugger, but
Devel::ptkdb is happiness and joy.  The only downside is that it
requires Tk, and several of the machines I want to debug on don't have
that by default.  Still, it's so easy to work with, I'll install Tk
just to work with it.

-=Eric


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

Date: Wed, 18 Jan 2006 11:04:21 -0800
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: FAQ 3.5 How do I debug my Perl programs?
Message-Id: <180120061104210746%jgibson@mail.arc.nasa.gov>

In article <20060117205548.955$A8@newsreader.com>, <xhoster@gmail.com>
wrote:

> Jim Gibson <jgibson@mail.arc.nasa.gov> wrote:
> > In article <dqbvv4$ps$1@reader2.panix.com>, PerlFAQ Server
> > <comdog@pair.com> wrote:
> >
> > >
> > > 3.5: How do I debug my Perl programs?
> > >
> >
> > [5 good suggestions snipped]
> >
> >    Use the print statement.
> 
> I would usually suggest warn rather than print.

Acknowledged. You and I are on different sides in the print vs. warn
issue (<http://tinyurl.com/9x9j6>) but I think the advice to use warn
doesn't have the simplicity and directness of using print. The FAQ
advice is for beginners, as experienced programmers already know how to
debug Perl programs. Advising beginners to use the warn statement might
make them wonder what mysterious capabilities warn might have that can
help them debug their programs, when all they need to do is confirm
that variables contain what they think they contain.

   "Use the print/warn statement"
   "Use the print or warn statements"

are just not as concise. Once beginners get into the habit of
displaying variables, they can worry about whether STDOUT or STDERR is
the appropriate file stream.

Thanks to everybody for the comments.

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com


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

Date: Wed, 18 Jan 2006 18:50:14 GMT
From: Matt Silberstein <RemoveThisPrefixmatts2nospam@ix.netcom.com>
Subject: Open Source Blog SW wanted
Message-Id: <b83ts19plbuc7ojb4d3555tqlu7k7c32gk@4ax.com>

I am looking for advice on an open source Blog system, preferably in
Perl. (I have cross-posted this to a Perl and a webmaster group. If
people propose a non-Perl solution they might want to consider
changing the follow-up.) I am working on something blog/forum like and
will end up doing heavy modification. (I can imagine that by the time
I am done most of the original code will be gone, but having a
scaffold will help quite a bit.) I know Perl well enough and don't
want to try to keep Perl and PHP (and JavaScript and ...) in my head
at once if I don't have to. Any suggestions? TIA.




-- 
Matt Silberstein

Do something today about the Darfur Genocide

http://www.beawitness.org
http://www.darfurgenocide.org
http://www.savedarfur.org

"Darfur: A Genocide We can Stop" 


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

Date: Wed, 18 Jan 2006 13:56:10 -0500
From: thundergnat <thundergnat@hotmail.com>
Subject: Re: Scanning file version 2.0
Message-Id: <GYednQNUg8-_ElPeRVn-hg@rcn.net>

Anton van der Steen wrote:
> Hey guys,
> 
> Here is version 2.0 for scanning files.
> I have added scrollbars to the text area and a function
> to print OUT the result to a other file.
> Below you will find the script.
> 
> I have tested it with Perl 5.8.6 from ActiveState.com,
> bear this in mind.
> 
> Have fun.

So... what was your perl question?

Or were you looking for commentary on this script?

Several things:

No use warnings
No use strict
Poor (non-descriptive) variable names
Poor (non-descriptive) sub routine names
Poor indenting
Lots of (duplicate) hard coded values
Lots of cut and paste coding
Use of special variables for other than their intended reason (@ARGV)
Repeated loading of modules (doesn't hurt, but doesn't help either)
No error checking or trapping
Two argument opens with global file handles rather than 3 argument 
  opens with lexical file handles
Usless use of double quotes when not interpolating

All of those are problems with style. None of them will cause the
script to work incorrectly, they will just make it harder to maintain
and debug, and make it harder for other people to help you.

The bigger problem is that your script doesn't work.
It will only find a single occurance of the phrase no matter how many
times (> 1) it appears on a line.

Test it. Make a text file that contains:

the best the best the best the best the
worst the best the best the best the best

then search for the phrase 'the best'. It will report 2, (one per line)
rather than 8.

Also, it won't find phrases that are broken across lines.
Search the above sample file for 'the\sworst' (since your aren't escaping
meta characters you can feed it a regex assertion to search.) It won't
find any.


Here's a quick rewrite of your script to correct all of the above problems,
with some other minor enhancements.


use warnings;
use strict;
use Tk;
use Tk::ROText;
use Tk::Dialog;

my $font = '{helvetica} 10 bold';
my $title = 'Crappy GUI Grep';
my $infile;

my $mw = new MainWindow;
$mw->title( $title );

my $search_frame = $mw->Frame->pack;

$search_frame->Label(
    -text => 'Search phrase :',
    -font => $font,
)->pack;

my $search_term = $search_frame->Entry(
    -width       => 100,
    -borderwidth => 2,
    -font        => $font
)->pack;

my $button_frame = $mw->Frame->pack( -pady => 3 );

$button_frame->Button(
    -text       => 'Choose File',
    -command    => \&load_file,
    -background => 'yellow',
    -font       => $font,
    -width      => 18
)->grid(
    -row    => 0,
    -column => 0,
    -padx   => 2
);

$button_frame->Button(
    -text       => 'Count Matches',
    -command    => \&count_matches,
    -background => 'green',
    -font       => $font,
    -width      => 18
)->grid(
    -row    => 0,
    -column => 1,
    -padx   => 2
);

$button_frame->Button(
    -text       => 'Save Results to File',
    -command    => \&save_results,
    -background => 'cyan',
    -font       => $font,
    -width      => 18
)->grid(
    -row    => 0,
    -column => 2,
    -padx   => 2
);

$button_frame->Button(
    -text       => 'Clear',
    -command    => sub { 
      clear_text();
      $mw->title( $title );
      undef $infile;
      },
    -background => 'orange',
    -font       => $font,
    -width      => 18
)->grid(
    -row    => 0,
    -column => 3,
    -padx   => 2
);

my $match = $mw->ROText(
    -width      => 150,
    -height     => 1,
    -background => 'white'
)->pack(
    -pady => 5
);

my $txt = $mw->Scrolled(
    'Text',
    -scrollbars => 'se',
    -wrap       => 'none',
    -width      => 150,
    -height     => 35,
)->pack(
    -expand => 1,
    -fill   => 'both'
);

MainLoop;

sub count_matches {
    local $/ = '';
    my $occurances = '0';
    my $file       = $infile;
    my $pattern    = $search_term->get();
    return unless defined $file and length $file and length $pattern;
    open( my $filehandle, '<', $file )
      or err( "Could not open file \"$file\". $!" )
      and return;
    while ( my $paragraph = <$filehandle> ) {
        my (@count) = $paragraph =~ m/($pattern)/mg;
        $occurances += @count;
    }
    $match->delete( '1.0', 'end' );
    $match->insert( '1.0',
      "The phrase \"$pattern\" was found $occurances times found in file $file." );
}

sub load_file {
    my $file = $mw->getOpenFile;
    $infile = $file;
    return unless defined $file and length $file;
    open( my $filehandle, '<', $file )
      or err( "Could not open file \"$file\". $!" )
      and return;
    $mw->title( "$title - $file" );
    clear_text();
    $txt->insert( 'end', $_ ) while <$filehandle>;
}

sub clear_text {
    $txt->delete( '1.0', 'end' );
    $match->delete( '1.0', 'end' );
}

sub save_results {
    my $file = $mw->getSaveFile;
    return unless defined $file and length $file;
    open( my $filehandle, '>', $file )
      or err( "Could not open file \"$file\" for writing. $!" )
      and return;
    print $filehandle $match->get( '1.0', 'end' );
}

sub err {
    my $message = shift;
    my $error = $mw->Dialog(
        -title   => 'Oops',
        -text    => $message,
        -bitmap  => 'error',
        -buttons => ['Ok']
    );
    $error->Show;
}


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

Date: Wed, 18 Jan 2006 10:21:32 -0800
From: "Mark" <nospam@thanksanyway.org>
Subject: Re: Script execution blocked when reading from a socket
Message-Id: <rY2dnbQSVoq8GlPeRVn-gQ@w-link.net>

"Rocco Caputo" <troc@pobox.com> wrote:
>
> You're in luck.  ActiveState's Perl implements select() for sockets,
> although not for other types of filehandle.  Cygwin is another option
> for Windows, and it implements a more complete select().

And through some great stroke of luck, I'm using ActiveState Perl!




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

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 V10 Issue 8864
***************************************


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