Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

close ARGV inside a while

by jesuashok (Curate)
on Jun 08, 2006 at 13:11 UTC ( #554268=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

use strict; use warnings; #prints one line and bails out @ARGV = ( '<'.$0 ); while (<>) { close ARGV; print '.',$_; }
Output :-
could any one explain me that strange behaviour ?

Thanks a lot.

"Keep pouring your ideas"

Replies are listed 'Best First'.
Re: close ARGV inside a while
by liverpole (Monsignor) on Jun 08, 2006 at 13:17 UTC
    Hello again, jesuashok,

    I'm not sure why you think it's strange behaviour; it seems logical to me.

    You are putting $0 (the name of the current file) into @ARGV, and then reading from that file.  When you close the file with close ARGV, you've already read one line, which is in $_.  So when you do:

    print '.',$_;

    you are simply printing out a period '.', concatenated with the first line of the Perl script itself.

    Try taking out the line close ARGV; and the whole program will be printed:

    #!/usr/bin/perl -w use strict; use warnings; #prints one line and bails out @ARGV = ( $0 ); while (<>) { # close ARGV; print $_; } # Output #!/usr/bin/perl -w use strict; use warnings; #prints one line and bails out @ARGV = ( $0 ); while (<>) { # close ARGV; print $_; }

Re: close ARGV inside a while
by Joost (Canon) on Jun 08, 2006 at 13:45 UTC
    jesuashok, your recent posts seem to mostly consist of code snippets that are suspiciously impractical, followed by the question "please explain".

    I don't mind that much, but please try to be more clear about why you think the output is strange. At the very least, try to provide an example of what you think the output should be.

    If you don't do that we end up going round and round and in the end still won't know what's going wrong for you.

    I'm not even going to attempt to answer your post here until you explain what you think is wrong with the output, since it seems too obvious to me why it does what it does.

      Can't you see? You close the filehandle and it mysteriously doesn't return any more lines and the <> operator returns an undefined value? That's just what The Man wants you to believe. The Truth Is Out there. I mean, come on! Maybe you need a refresher course. And why would a Wookie live on Endor? That does not make sense!

      Wait . . . now I'm confused. Where does the pony come in?

      I wouldn't presume to know (or ask for an explanation) as to what Jesusahok is doing, but this node may explain what he's up to. I've seen obviously broken code in perldoc with explanations of why they don't work. I don't think these questions are unreasonable in that context.

        So he's got new people . . . and he's trying to find exercises for them to do to help them learn . . . so he's asking people on Perlmonks about broken example code . . . so that these people he's trying to bring up to speed . . . can learn . . .

        Wait, I think the Wookie just ran off with the pony there.

        I could see the benefit in using intentionally broked code as a teaching tool; however if the teacher themselves doesn't understand why it's broked they're probably not the best candidate to be teaching people (at least not with their present level of knowledge; the optically unreceptive pathfinding the optically unreceptive as it were).

        Addendum: I will grant that Increment avoids warning unexpectedly is somewhat wonky behavior, and whlle I can deduce why it behaves that way I certainly could see how it's surprising. However calling things like Data::Dumper converting characters to their backwhacked double quote equivalent when you explicitly tell it to do so or expecting to get data from closed filehandles "unexpected" behavior just makes me want to quote Inigo: "You keep using that word. I do not think it means what you think it means.


      Please, don't mistake me. I will try to be more specific in my upcoming writeups.

      I am trying on my own some sample program practice in perl. If I am not able to understand any answers, I will post it in perlmonks.

      All those nodes are down voted anyway. Still, I am very happy to get lot inputs from you monks.

      I got your point. I will be more specific and clear in my upcoming writeups.

      Thanks a lot for your helps. :-)

      "Keep pouring your ideas"

        I'd suggest that you use Acme::MJD in all your snippets


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://554268]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2017-09-21 03:56 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (241 votes). Check out past polls.