jonnyfolk has asked for the wisdom of the Perl Monks concerning the following question:

I started with a form which has a standard action when in receipt of standard parameters. I added a checkbox to the form to change the action. What I opted to do was the following:

if ($checkbox) { #open file while (<FILE)> { #do something } }
Of course when I tried it all sorts of error messages came up, but unfortunately none of them said "Oi, plonker, you can't put a 'while' statement inside an 'if'"! So I battled away until I finally understood that for myself.

What I did do was whack the action into a subroutine and put that into the 'if' statement.:

if ($checkbox) { &dosomething; } } sub dosomething { #open file while (<FILE)> { #do something } }
My question is: are there other ways of making this work apart from a subroutine?

Replies are listed 'Best First'.
Re: Using loops inside an 'if' statement
by liz (Monsignor) on Oct 02, 2003 at 09:33 UTC
    Your first example contains an error:
    if ($checkbox) { #open file while (<FILE>) { # ^^ these two were exchanged #do something } }

    Maybe that was the problem?

    Liz

      That typo was created especially for my question, I'm afraid. My apologies for that.

      I can't provide the code that I was having provlems with because I made so many changes getting it to work that I no longer have the original. I only know that when I cut and pasted everything inside the 'if' statement the script magically worked. The only problem is that in order to provide code for this thread I cut and pasted the whole lot back inside the 'if' statement, deleting the subroutine, tested it and the blooming script worked like a charm! Sounds of jonnyfolk beating his head against brick wall!

      It is fortunate that I asked the question (despite the copious -- votes!) because I had obviously jumped to the wrong conclusion which would not have been helpful to my 'perl progress' in the future.

      Thanks to all who pointed out the error of my ways

Re: Using loops inside an 'if' statement
by thens (Scribe) on Oct 02, 2003 at 09:39 UTC
    There is no such rule that you cannot put a while statement inside a if loop

    You are doing something else which you havent shown us here that is causing the problem. Please post the actual code here for the monks to understand your problem and suggest solutions.

    I can give you a few suggestions. None of it may be relevant to this problem. But these are good practices that you should follow while coding in perl.

    • All perl programs should have 'use strict' and 'use warnings'. This will help you catch many errors.
      use strict; use warnings; # # Your code here #
    • If you are not able to comprehend a error message. 'use diagnostics' will give you more details about the error message and what may be the possible cause for it.
    • perldoc is the definitive source of information for all related questions. Consult the doc before you ask anyone else.
      $perldoc <Module name> # for documentation of the modules $perldoc -f <function> # eg perldoc -f fork $perldoc -q "HTML parsing" # search the perlfaq $perldoc perldoc # for documentation on perldoc

    Hope this helps

    -T

Re: Using loops inside an 'if' statement
by broquaint (Abbot) on Oct 02, 2003 at 09:33 UTC
    Of course when I tried it all sorts of error messages came up
    Er ...
    print "pre if() statement\n" if(@ARGV == 0) { my $n = 1; while(<DATA>) { print $n++, $_; } } print "post if() statement\n"; __DATA__ this totally works
    When the above is run it outputs
    pre if() statement 1: this 2: totally 3: works post if() statement
    The point being, of course you can put a while statement or any other looping construct in a block if statement, and vice versa. Blocks in perl can take any and all valid code, as they are more or less just delimiters of scope.

    What were the errors you were getting when you had the while in the if?

    HTH

    _________
    broquaint

Re: Using loops inside an 'if' statement
by davido (Cardinal) on Oct 02, 2003 at 09:33 UTC
    There is no reason why a while loop cannot exist within the block that gets executed when an if statement evaluates true.

    However, both of your examples had syntax errors in them, leading me to wonder if that's where you're going to find the real source of the problem.

    For the record, it's
    while( <FILE> ) {...
    not

    while (<FILE)> {...

    Dave

    "If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Re: Using loops inside an 'if' statement
by thinker (Parson) on Oct 02, 2003 at 09:36 UTC

    Hi jonnyfolk,

    There is nothing wrong with putting a while in an if statement. The problem you have is a syntax error. Change

    while (<FILE)> { to while (<FILE>) {

    Notice the order of the brackets

    Hope this helps

    thinker

Re: Using loops inside an 'if' statement
by shenme (Priest) on Oct 02, 2003 at 15:06 UTC
    Could it be the head-slappingly obvious tromping all over $_ ?
    my $checkbox = 1; $_ = 'Wowza'; print "Before the loop we see \$_ as '$_'\n"; if( $checkbox ) { while( <DATA> ) { chomp; print " We see \$_ is '$_'\n"; } } print "After the loop we see \$_ as '$_'\n"; __DATA__ There I was, minding my own business, when ...
    will output
    Before the loop we see $_ as 'Wowza'
      We see $_ is 'There I was, minding my own business, when ...'
    Use of uninitialized value in concatenation (.) or string at jonnyfolk1.pl line
    18, <DATA> line 1.
    After the loop we see $_ as ''