in reply to Perl MIME parser partially works
When you get stuck, and what you think should be matching is not matching, its time to ddumperBasic debugging checklist the data you're matching against ( $textstuff, $from ), so you can figure out if the problem is with the data (missing, not what you expected), or with your regex pattern ( m//matching or s///ubstitution )
The script is composed mostly of bits and pieces of other scripts I have found over the last few months .... ny ideas on what would make this work better, and actually log the urls? make it log any messages that match any of the from addresses, IF they don't have an attachment? Thanks all for your help!
Hello and congratulations, you got pretty far, you've got a prototype, but now you're a little bit stuck, its time to rethink your approach :) its time to start over :)
Why? :) Because you have lines eleven indentation levels deep, there are too many variables around to keep track of ; you need more subroutines
The way I would approach this problem/exercise/task, is to pretend the code you have written doesn't exist, grab a pencil and paper, and draw a few boxes , putting a goal into each box :) say
------ | get list of files ---- \ ------ | check each file for from address ---- \ ------ | if address matching explode plaintext attachment into outdir ---- \ ------ | scan each exploded plaintext for ... links? ---- \ ------ | add each found link to logfile ----
Now that you have goals, start turning them into subroutines, so when you get stuck you can copy/paste only the subroutine which isn't working and concentrate on only fixing it -- easier than fixing entire program :)
So you then write something like
#!/usr/bin/perl -- use strict; use warnings; use MIME::Parser; ... Main( @ARGV ); exit( 0 ); sub Mainsky { ... my @files = get_files( $fromdir ); for my $file ( @files ){ if( matches_adresses_iwant( $file ) ){ informit( "explode_plaintext_fromfile_into( $file, $outdir + )" ); explode_plaintext_fromfile_into( $file, $outdir ); my @links = extract_links( $outdir ); if( @links ){ log_links( @links ); } else { informit( "not extract_links( $file )" ); } } else { informit( "not matches_adresses_iwant( $file )" ); } } } sub get_files { ... } sub matches_adresses_iwant { ... } sub extract_links { ... } sub explode_plaintext_fromfile_into { ... } sub log_links { ... } sub informit { print STDOUT @_,"\n" } __END__
Or like
#!/usr/bin/perl -- use strict; use warnings; use MIME::Parser; ... Main( @ARGV ); exit( 0 ); sub Mainskee { ... my @files = get_files( $fromdir ); for my $file ( @files ){ if( matches_adresses_iwant( $file ) ){ informit( "explode_plaintext_fromfile_into( $file, $outdir + )" ); lincon_plaintext_logs( $file, $outdir ); } else { informit( "not matches_adresses_iwant( $file )" ); } } } sub lincon_plaintext_logs { my( $outdir , $file ) = @_; explode_plaintext_fromfile_into( $file, $outdir ); my @links = extract_links( $outdir ); if( @links ){ log_links( @links ); } else { informit( "not extract_links( $file )" ); } } sub get_files { ... } sub matches_adresses_iwant { ... } sub extract_links { ... } sub explode_plaintext_fromfile_into { ... } sub log_links { ... } sub informit { print STDOUT @_,"\n" } __END__
or something like this, all depending on how complicated the matching/extracting is and how it needs to be grouped , which parts are common/similar/alike/reusable
#!/usr/bin/perl -- use strict; use warnings; use MIME::Parser; ... Main( @ARGV ); exit( 0 ); sub Maincakes { ... my @files = get_files( $fromdir ); for my $file ( @files ){ iwant_iphone_links( $file ) or iwant_other_links( $file ) or iwant_pancake_links( $file ); } } sub iwant_iphone_links { ...; return $stop_or_keep_going } sub iwant_other_links { ...; return 1 } sub iwant_pancake_links { ...; return 0 } __END__
More of this type of idea of rewriting your code in Re: RFC: beginner level script improvement (version control), skimmable code is the idea, more subs, more subs, more subs, more subs, more subs,
More generic advice :) On debugging, verify everything, talk to teddybear ... checklists and more talking to yourself out loud is a pretty good debugging technique :) 1 / 2/3
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Perl MIME parser partially works with my code I wrote (code does not exist , there is no code)
by CalebH (Acolyte) on Oct 07, 2013 at 15:23 UTC |