Monks,
I've been struggling with a particular segment of my 300-line program I've been writing. I've added a remove function from <> to pass the string to the interpreter, I tell Perl to have a look at a file to find this string. If it exists the ENTIRE line that held that string will be deleted.
Should I use:
if ($remove eq ~ m/^.*\b($remove)\b.*$) {
# do stuff here
}
OR:
sub clean_the_file {
my $sourcefile = "/u/ccsys/CC_print.printers";
# grab the sourcefile and the string to remove as the first and second
+ arguments
my ( $sourcefile, $remove ) = @_;
# create a temporary file
my $tempfile = # Some random file creation logic;
# extract lines from $sourcefile which do NOT have the string to r
+emove
# note that the \' inserts ticks so that strings with spaces can w
+ork, too
`grep -v \'$remove\' $sourcefile\n > $tempfile`;
# delete source file, then rename working file
unlink($sourcefile);
rename( $tempfile, $sourcefile );
}
Obviously the second logic makes a bit more sense; considering I'm not editing a live file, IF: the second logic should be used, how would I go about generating a random filename?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.