Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Calling a subroutine within a conditional

by GrandFather (Sage)
on Sep 04, 2012 at 00:35 UTC ( #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

Comment on Re: Calling a subroutine within a conditional
Download Code
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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2015-11-28 05:39 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (738 votes), past polls