Switching with case in perl ?

R

Ron Bergin

Quoth Ron Bergin said:
It doesn't appear that you're using the strict or warnings pragmas.
It would help if you ask Perl why it can't open the Loog_rr.txt file.
It would also be better to put that open call prior to the glob.
You also need to make your code easier to read/maintain by reducing
the amount of indentation.
open(my $out_file, '>>', 'Loog_rr.txt') or die "Can't open Loog_rr.txt
$!";
while (my $filename = glob("*$year$month$day$hours$minute*") ){
open($wordlisting1, '<', $filename) or die "Could not open
wordlisting: $!";
while (my $line = <$wordlisting1> ) {
if ($line =~ m/beth/) {
@items = (split(",",$line))[0..3];

You mention strictures above, but @items is not declared anywhere.

Yes, that was a little faux pas on my part which would have revealed
itself when running under strict.
Except for the very special case of ' ', the first argument to split is
a regex. You should write it as a regex so people don't misread it:

my @items = ( split( /,/, $line ) )[0..3];
print $out_file join(',', @items) , ";\n";

You are right again, I should have written it as a regex, however it
does work as written.
This can be made cleaner by using Perl's print-control variables: put

local ($,, $\) = (',', ';\n');

at the top of the loop and then simply

print $out_file @items;
Here I have to disagree. While the print statement itself is cleaner,
the reassignment of the $OUTPUT_FIELD_SEPARATOR and
$OUTPUT_RECORD_SEPARATOR makes it less clear for the beginner, which
is where the OP clearly falls in.

Also, since you're suggesting to do that reassignment "at the top of
the loop" (instead of prior), you're doing it at each iteration of the
loop, which doesn't make sense to me.
You want to be consistent about whether you use || or 'or' to check for
errors. Personally I'd always use 'or'; but then I'd always omit the
parens on open as well.

I agree, consistency is important.
There's little point in checking for errors on close if you haven't
checked for errors as you read. What errors could occur at this point
that you care about?


Again, the point of checking for errors when you close a file you were
writing to is to ensure all the data got written successfully: if you
care, you should be checking print succeeded as well.

Here it's a 50/50 decision. I rarely (almost never) use die or warn
on a close statement, but I included it because the OP was using it
and it doesn't do any harm. In fact, in some cases it would be
recommended, such as when closing a piped open. I've seen people do
aa die statement on each and very print statement which is totally
ridiculous.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top