Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Perl MIME parser partially works with my code I wrote (code does not exist , there is no code)

by Anonymous Monk
on Oct 03, 2013 at 07:48 UTC ( [id://1056731]=note: print w/replies, xml ) Need Help??


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

  • Comment on Re: Perl MIME parser partially works with my code I wrote (code does not exist , there is no code)
  • Select or Download Code

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

    Great advice, and I've printed the script out to look it over and pick out the code I have so far to place in subroutines.

    As of now, I have changed some of the code to execute almost the entire code in a subroutine &ipod;, which I have set to match no matter what in a if/else statement (It runs the logger in both cases).

    if ($from =~ /address|address2/ig) { if ($to =~ /address|address2/ig) { if ($textstuff =~ /sent from my ipod/ig || $textstuff =~ /sent from my + iphone/ig) { print "From ME----\nSubject: $subject\nFrom: $from\nTo: $to\nDate: $da +te\nContent Type: content\nMime Type: $mime\nEffective Type: $effecti +ve\n"; &ipod; } else { if ($textstuff !~ /sent from my ipod/ig || $textstuff !~ /sent from my + iphone/ig) { print "----NOT ipod ----\nSubject: $subject\nFrom: $from\nTo: $to\nDat +e: $date\nContent Type: content\nMime Type: $mime\nEffective Type: $e +ffective\n"; &ipod; }

    It's ugly, but it gets the job done mostly. Unfortunately, it still suffers from only running through and prints only one instance of 'from ME' or '----NOT ipod' and then no longer prints any messages, other than the statement that it's moving files. Also, it never reaches the bottom statement to extract files using attachment:stripper, which is somewhat confusing.

    I would imagine that it's a problem with the foreach statement only executing once due to the fact that it stops printing, but since it executes all the other code (opens the log file, prints to it, executes the moving of files), I just have to try to track down where the problem is happening as well as why it's not executing the attachment stripper section of code.

    Thanks again for the reply, and once I can track down why the above is happening, I plan to redo it into subroutines (as well as all future scripts). :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1056731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-18 15:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found