Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: How can I find a line in a RTF file?

by Anonymous Monk
on Aug 08, 2014 at 11:07 UTC ( [id://1096739]=note: print w/replies, xml ) Need Help??


in reply to Re: How can I find a line in a RTF file?
in thread How can I find a line in a RTF file?

A couple of suggestions for your code:

  1. Although it works, open(IN, ,"<", $file) has an extra comma in it. Also lexical filehandles are generally considered to be better (e.g. open my $in, '<', $file or die $!;)
  2. You use the array @_ in your subs for storing data. While that's possible, @_ is generally only used to access the arguments passed into a sub, and using it the way you're doing is very likely to confuse others working with your code.
  3. Although I'm not sure what your intentions were, I suspect the regex /Date of Last Update:+/ is not doing what you expect: It'll match the string "Date of Last Update" followed by one or more colons. If you want to match the string itself multiple times, you need a group: /(?:Date of Last Update:)+/, although I'm not sure if the input file will ever contain the string "Date of Last Update:Date of Last Update:". Also, {1,} is equivalent to +.
  4. You can use your variable $substr in your regular expressions, e.g. /\Q$substr\E/ (for the meaning of \Q...\E see quotemeta).
  5. Naming a sub the same as a Perl keyword, in this case "grep", is generally a bad idea - the only exception is when you're actually trying to replace the built-in grep - because it will create much confusion as to which function is supposed to be called when, both for the readers of your code and for Perl. This confusion is why you had to call your "grep" as &grep();. Also, inside your "grep", you're calling Perl's grep, any slight mistake in syntax may call your "grep" instead and create infinite recursion.
  6. A small hint: grep { $_ =~ /Date of Last Update:/ } can be written as grep { /Date of Last Update:/ } and "... file: ".$file.": $!\n"; can be written as "... file: $file: $!\n";
  7. On style: If you find yourself using $_ a lot, over multiple lines, then usually it's better to use a lexical variable instead. $_ is global and can (accidentally) be manipulated by other code, especially code that someone else inserts later. You can use something like while (my $line = <$in>) { instead.

Replies are listed 'Best First'.
Re^3: How can I find a line in a RTF file?
by thanos1983 (Parson) on Aug 08, 2014 at 19:36 UTC

    Hello Anonymous Monk,

    Well I am not an expert I consider my self just a beginner, so any suggestions for improvement are always welcome. :D

    Thank you for your time and effort to write these comments. :D

    Seeking for Perl wisdom...on the process...not there...yet!

Log In?
Username:
Password:

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

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

    No recent polls found