Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

"defined" in while loop Edit | Delete | Quote | Reply | Private Reply

by manish.rathi (Acolyte)
on Feb 16, 2009 at 16:29 UTC ( #744123=perlquestion: print w/ replies, xml ) Need Help??
manish.rathi has asked for the wisdom of the Perl Monks concerning the following question:

1) while(<stdin>){print " I saw $_" ;}

# Is thie equivalent to

2) while($_=<stdin>){ print "I saw $_" ;}

If it is so, , how will this code end after printing "I saw whateverentered"

As there is no "defined" used in while loop , so how will while loop end because undefined value in the $_ will not let the loop end.

3) while(defined($_= <stdin>)){print "I saw $_ " ;}

In either of these three codes, output is the same.

While I was going thru book, it is stated for the first code "Read a line of input and see if its true. And if its true, enter the while loop but throw away that line of input." What does this mean ? Does it mean that line of input will be read but not printed ? Why not ? Because by default any input will be assigned to $_.

Pls explain.

20090217 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Comment on "defined" in while loop Edit | Delete | Quote | Reply | Private Reply
Select or Download Code
Re: "defined" in while loop Edit | Delete | Quote | Reply | Private Reply
by targetsmart (Curate) on Feb 16, 2009 at 16:46 UTC
    to make your code understandable by monks just see here before posting Markup in the Monastery

    Vivek
    -- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.
Re: "defined" in while loop Edit | Delete | Quote | Reply | Private Reply
by moritz (Cardinal) on Feb 16, 2009 at 16:47 UTC
    perlsyn gives some answers to your questions, scroll down to which is Perl short-hand for the...
Re: "defined" in while loop Edit | Delete | Quote | Reply | Private Reply
by ELISHEVA (Prior) on Feb 16, 2009 at 23:25 UTC

    OP writes:

    1) while(<stdin>){print " I saw $_" ;} # Is thie (sic) equivalent to 2) while($_=<stdin>){ print "I saw $_" ;} so how will while loop end because undefined value in the $_ will not let the loop end

    In Perl, the assignment $_ = <> doesn't necessarily make $_ "defined"? Strange isn't it? But here is how it happens:
    my $x = undef; #clears out $x and makes it undefined my $y = $x; #since $x is undefined, so is $y

    It might be easier to think of "undefined" in Perl as "null" rather than "not yet assigned".

    Also, it isn't necessary for $_ to be undefined (or unassigned) for the loop to end. A while loop ends when expr in while (expr) evaluates to false. Being undefined is only one way of being false. Other ways of being false are:

    • being equal to 0
    • being equal to the empty string, ''

    Best, beth

      Also, it isn't necessary for $_ to be undefined (or unassigned) for the loop to end.

      While that's true in general,

      >perl -le"$_=4; while (--$_) { print }" 3 2 1

      It's not true in this case.

      >perl -e"print 0" | perl -le"while ($_ = <>) { print $_ ? 'true' : 'fa +lse' }" false >perl -MO=Deparse -e"while (<>) {}" while (defined($_ = <ARGV>)) { (); } -e syntax OK >perl -MO=Deparse -e"while ($_ = <>) {}" while (defined($_ = <ARGV>)) { (); } -e syntax OK >perl -MO=Deparse -e"while (defined($_ = <>)) {}" while (defined($_ = <ARGV>)) { (); } -e syntax OK
        Thanks! Learn something every day! (well, lots today). But what is going on here? What (under the covers) makes this a special case? Why is Perl wrapping $_=<> in defined($_ =<>)? Another convenience feature? And where else does this happen?

        Here's my output with a slightly expanded print line checking both the value and the defined status and just to make sure there was no monkey business with the special variable $_ I changed it to $line - same result as you, of course. (I had to switch " and ' to keep the bash shell from trying to resolve $_):

        $ perl -e'print 0' | perl -e'while ($line = <>) { print "\$line=<$lin +e> \$line=<" . ($line ? "true" : "false") . "> defined=<" . (defined( +$line) ? "true" : "false") . ">\n"};' $line=<0> $line=<false> defined=<true>

        It is also not anything special about the syntax $somevar = <> because I get the same result if I use readline(...):

        $ perl -e'print 0' | perl -e'while ($line = readline(*STDIN)) { print +"\$line=<$line> \$line=<" . ($line ? "true" : "false") . "> defined=< +" . (defined($line) ? "true" : "false") . ">\n"};' $line=<0> $line=<false> defined=<true>

        Or is it? Could it be that both while($somevar = readline(...)) and while($somevar = <...>), but only these, have special semantics? I noticed that if I wrap readline in a function of my own that the special behavior goes away. For example,

        use strict; use warnings; #The following all output: # BEFORE # $line=<0> $line=<false> defined=<true> # AFTER # # while (my $line = readline(*STDIN)) { # while (my $line = <STDIN>) { # while (my $line = <>) { # # but wrapping readline in a sub produces only # BEFORE # AFTER print "BEFORE\n"; while (my $line = wrapped_readline(\*STDIN)) { print "\$line=<$line> \$line=<" . ($line ? "true" : "false") . "> defined=<" . (defined($line) ? "true" : "false") . ">\n"; } print "AFTER\n"; sub wrapped_readline { my $fh = shift @_; my $sLine = readline($$fh); #print "wrapped_readline: <$sLine>\n"; return $sLine; }

        Thanks in advance, beth

        Update: refined question a bit; fixed error in sub of $_ with $line; added examples using readline and a sub wrapping readline.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://744123]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (12)
As of 2014-09-23 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (219 votes), past polls