Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl File Parsing - My Code Works, but it's Ugly!

by aaron_baugher (Curate)
on May 31, 2015 at 18:02 UTC ( [id://1128497]=note: print w/replies, xml ) Need Help??


in reply to Perl File Parsing - My Code Works, but it's Ugly!

Nothing huge, but some cleanup things:

  • You don't ever use Time::Local. localtime is a built-in function.
  • You need my in front of your date/time variable list, to run under strict.
  • You don't need 'junk' variables ($j1, $j2, $j3) for the extra values from localtime. If your list on the left has fewer variables than are passed from the right, the extra ones will be discarded.
  • Your day and month adjustments can be done shorter and more cleanly with increment operators: $year += 1900; $mon++;
  • You should probably do error checking on your mkdir statements.
  • It's strange that you're opening your output files once, and then opening them again right away inside your subroutine. In fact, I'd almost expect that to cause an error or warning, but maybe Perl silently closes and re-opens it. Are you doing that so the first open (with '>') will start the files empty, and then switch to appending? It's probably unnecessary, but maybe you could explain your thinking there.
  • When you have multiple print statements in a row, you can often clean things up with a here-document:
    print $html_report <<END; #### Now Analyzing '$file_01_txt' END
  • When you have "if(this){single_statement}" constructs, you can often eliminate much of the clutter of parens and brackets with postfix syntax:
    print $html_report "$line\n" if $line =~ /search criteria 01/i;

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.

Replies are listed 'Best First'.
Re^2: Perl File Parsing - My Code Works, but it's Ugly!
by Nico (Novice) on May 31, 2015 at 20:55 UTC

    Thank you for your response! I have no idea what I was thinking with those opens and closes. I only need to specify them once at the beginning of the script, and I can use them throughout the script.

    I removed the junk variables and removed Time::Local. It works!

    I will need to figure out how to error check my mkdir statements. But luckily I only have those two statements there so shouldn't be too hard

      You can error-check mkdir much like you do open:

      mkdir $newdir or die "Unable to mkdir $newdir : $!";

      That's true of most commands in Perl; they return true if successful and false on failure, so you can use "do_this() or show_error()" logic. (One exception is system, which returns the return value of the underlying command, which (on Unix at least) is zero on success, so you have to watch out for that.)

      Aaron B.
      Available for small or large Perl jobs and *nix system administration; see my home node.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found