in reply to Re: returning to a loop later on
in thread returning to a loop later on

yes, you're right there was a typo. that fixed that part of the code but now, the code doesnt do what i want it to do. basically, there is some data that the user can either output to a file or the the screen. the screen bit is easy. then the user chooses a filename and that filename is checked for validity. that filename is then appended with the extension ".txt" and then it is checked to see if that exists. if it does exist then the user can either overwrite it or go back to the original loop of asking for the desired filename to be written. im completely stuck. i guess i should be doing some sort of subroutining or something but i dont know the first thing about them!!! please help!

Replies are listed 'Best First'.
Re^3: returning to a loop later on
by mreece (Friar) on Sep 07, 2006 at 01:21 UTC
    without commenting on the sanity of this approach in general (ie, obligatory mention of IO::Prompt), here are some comments about this particular code:

    1. always use strict;
    2 (related). declare your variables in the appropriate scope
    3. your code always prints "Invalid input" even when it is valid -- you want to print that only if they have typed something already.
    4. you 'redo FILENAME' without resetting $save, so your while ($save !~ ...) fails the second time through
    5. your open/print/close is within the if (-e $save) block, so it never happens if the file does not already exist
    6. the {1} in your regular-expressions is useless, since 1 is the default.
    7. you could use m/^[SF]$/i to match case-insensitively (/i means ignore-case), but i won't bother with that in the code below..

    here is a revised version. i have tried to change as little as possible to make it work, so you can learn from your mistakes rather than be confronted with entirely new/unfamiliar code.

    use strict; my $output = "here is some output\n"; # for testing.. your output can + come from whereever my $out; while ( $out !~ m/^[sSfF]$/ ) { print("\nInvalid input.\n Please type either S or F.\n") if $out; print("Do you want to output to (S)creen or to (F)ile?\n"); chomp( $out = <STDIN> ); } if ( $out =~ m/^[sS]$/ ) { print $output; } elsif ( $out =~ m/^[fF]$/ ) { my $save; # this is outside the FILENAME:{} block because it is +needed at the end FILENAME: { while ( $save !~ (m/^[a-zA-Z][a-zA-Z_0-9]*$/) ) { print("\nInvalid input.\n Please begin with a letter and d +o not add extension, it will be added automatically.\n") if $save; print("Please enter filename:\n"); chomp( $save = <STDIN> ); } if ( -e $save ) { my $overwrite; while ( $overwrite !~ (m/^[nNyY]$/) ) { print("\nInvalid input.\n Please type either Y or N.\n +") if $overwrite; print("\nFilename exists. Overwrite? (Y) or (N)\n"); chomp( $overwrite = <STDIN> ); } if ( $overwrite =~ (m/^[nN]$/) ) { undef $save; # reset $save to trigger new prompt! redo FILENAME; } } } open( DATA, ">$save" ) || die "Couldn't open $save for writing: $! +\n"; print DATA $output; close(DATA); }
      your code doesnt seem to work. nothing appears and then the code ends. any ideas?
        did you click the download link before copy/paste? it runs fine when i do it.
Re^3: returning to a loop later on
by ikegami (Patriarch) on Sep 07, 2006 at 01:25 UTC

    All your while loops are wrong. while tests the condition before entering the loop, but you want to execute the loop body once before checking whether to exit or not. Replace

    while (...cond...) { ...body... }

    with

    do { ...body... } while (...cond...);