Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Calling a subroutine within a conditional

by GrandFather (Saint)
on Sep 04, 2012 at 00:35 UTC ( [id://991522]=note: print w/replies, xml ) Need Help??


in reply to Calling a subroutine within a conditional

There look to be a bunch of things there that will make the code hard to understand and maintain.

First off, don't use prototypes. That is your immediate problem as has already been suggested. They don't do what you think and cause subtle trouble that can lead to really hard to debug errors.

Don't use artificial boolean values. Just accept that $continue_tag is a boolean variable that is false if it is empty or 0 and true otherwise.

Make it clear where information comes from. You don't show @array_of_lines (bad name btw, why not just @lines?) being assigned any content. It's not clear how the work look_through_file does relates to the work create_output does. Where does $continue_tag's value get changed?

Without any idea what your intent for the code is I can only sketch a better structure, but consider:

#!/usr/bin/perl use strict; use warnings; my $fName = 'wibble.dat'; my $group; open my $fIn, '<', $fName or die "Can't open $fName: $!\n"; while (my @lines = scanFile($fIn)) { report(++$group, @lines); } sub scanFile { my ($fIn) = @_; my @lines; while (@lines < 10 && defined (my $line = <$fIn>)) { chomp $line; next if ! length $line; push @lines, $line; } return @lines; } sub report { my ($group, @lines) = @_; printf "%04d: %s\n", $group for @lines; }

which batches lines from a file into groups of 10 non-blank lines then prints each group with a group prefix.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Calling a subroutine within a conditional
by Freezer (Sexton) on Sep 05, 2012 at 13:01 UTC
    I appreciate the code, however some of the commands and methods used are a bit advanced for me.

    My code currently stands as:

      Ask about the stuff you don't understand, but don't get distracted by peripheral stuff from the main point of the code: make the flow of information obvious.

      True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2024-04-23 08:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found