Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: First attempt at bringing in file for input/output

by haukex (Archbishop)
on Oct 22, 2018 at 08:34 UTC ( [id://1224461]=note: print w/replies, xml ) Need Help??


in reply to Re: First attempt at bringing in file for input/output
in thread First attempt at bringing in file for input/output

It is not allowed to optionally create a variable in an if statement.

You're probably referring to this piece of documentation (discussed in e.g. Variable Scope):

NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons.

However, if you are referring to syntax like if (open my $fh, '<', 'file.txt'), this is perfectly legal; from Private Variables via my():

if ((my $answer = <STDIN>) =~ /^yes$/i) { user_agrees(); } elsif ($answer =~ /^no$/i) { user_disagrees(); } else { chomp $answer; die "'$answer' is neither 'yes' nor 'no'"; }
the scope of $answer extends from its declaration through the rest of that conditional, including any elsif and else clauses, but not beyond it.

One very common case of this kind of scoping being while (my $line = <>).

Replies are listed 'Best First'.
Re^3: First attempt at bringing in file for input/output
by Marshall (Canon) on Oct 24, 2018 at 05:42 UTC
    I liked your post++

    To further clarify this and another thread having to do with while loop and $line=<filehande>:

    #!usr/bin/perl use strict; use warnings; while (my $line =<DATA> and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Value of <HANDLE> construct can be "0"; test with defined() at C:\User +s\mmtho\Documents\PerlProjects\Monks\test_myVar_inwhile.pl line 5. Global symbol "$line" requires explicit package name (did you forget t +o declare "my $line"?) at C:\Users\mmtho\Documents\PerlProjects\Monks +\test_myVar_inwhile.pl line 5. Execution of C:\Users\mmtho\Documents\PerlProjects\Monks\test_myVar_in +while.pl aborted due to compilation errors.
    To fix the first error message,...
    #!usr/bin/perl use strict; use warnings; while (defined (my $line =<DATA>) and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Global symbol "$line" requires explicit package name (did you forget t +o declare "my $line"?) at C:\Users\mmtho\Documents\PerlProjects\Monks +\test_myVar_inwhile_ver2.pl line 6. Execution of C:\Users\mmtho\Documents\PerlProjects\Monks\test_myVar_in +while_ver2.pl aborted due to compilation errors.
    Back up and fix just this error message...
    #!usr/bin/perl use strict; use warnings; my $line; while ($line =<DATA> and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ Value of <HANDLE> construct can be "0"; test with defined() at C:\User +s\mmtho\Documents\PerlProjects\Monks\test_myVar_inwhile_ver2.pl line +7. do somehing with non blank line do somehing with non blank line
    To make this work completely:
    #!usr/bin/perl use strict; use warnings; my $line; while (defined($line =<DATA>) and $line !~ /^\s*$/) { print "do somehing with non blank line\n"; } __END__ do somehing with non blank line do somehing with non blank line do somehing with non blank line do somehing with non blank line
      To make this work completely

      In such loops I prefer to limit the scope of $line but this might not concern you.

      #!usr/bin/perl use strict; use warnings; while (my $line = <DATA>) { last unless $line =~ /\S/; print "Read: $line"; } __DATA__ do a thing with non blank line do a thing with non blank line last one no action on this line nor this
        I prefer to put the main condition that "ends the loop" in the conditional.
        For a command line input, that might be detection of /(Q)uit/i or something like that.
        Other secondary conditions can go in the body of the loop.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 15:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found