in reply to Real World 1, Great Expectations 0

If you're going to use IO::Handle, use it, don't just stick it in there and then ignore it. Code which makes use of a few methods from IO::Handle, but otherwise uses the regular functions looks schizophrenic. It's not unlike driving on the right hand side of the road most of the time, but sometimes, switching to the left side for no clear reason. Confusing, at best, especially when you're adding code somewhere in the middle. Which method do you use then?

IO::Handle would have you do it like so:
#!/usr/bin/perl -w # # test.pl -- IO:: test code... use strict; use warnings; use diagnostics; use IO::File; my @list = qw[ one.tmp two.tmp three.tmp ]; my @handles = map { IO::File->new(">$_") } @list; foreach my $handle (@handles) { $handle->print("Hi how the hell are ya!"); $handle->close(); }
A few notes:

I'm using map to convert your @list list into the @handles list. map is great at performing list conversions, so I try to use it as much as possible. Stuff in, stuff out. No mess.

When you're calling functions, try and put brackets on them to make it clear what you're doing. Sometimes the interpreter can get a little confused about what is the function and what is the object. Throwing brackets in makes it very clear, even to the reader.

If you open with IO::Handle, close with it too. That is, don't use the internal close method on an object. Only use that on something created with the internal open method. Make everything match up, because sometimes the object does something important that you are skipping.

Replies are listed 'Best First'.
Re: Re: Real World 1, Great Expectations 0
by hsmyers (Canon) on Oct 18, 2001 at 04:31 UTC
    A few things... First I'm not using IO::Handle, second, I'm fully aware of a variety of workarounds— that is not the point, the question is why the syntax used doesn't work! A question that was well answered in a later reply. Now for the good news, I hadn't gotten around to using IO::Handle, your comments suggest I should investigate post haste, and I will. Thanks

    hsm
      IO::File inherits from IO::Handle, so the parent methods are also available to you. (You might have better luck with the ambiguous syntax if you use a Perl-style loop. I have not tested that, however.)

      Update: PSI::ESP will only be available when the already-complex Perl grammar is complete enough to DWIMHM (might have meant). :)

      Anyone who can figure out how to allow post-expression for clauses while retaining the standard procedural loop and adding support for finding missing semicolons in the first case, should feel free to send me a message. We could fix a parser bug. *shiver*

        True, but all of the perl-style loops do the sensible thing and flatten the syntax to a simple scalar, I was just curious about why the array element didn't work. I'd forgotten the ambiguous syntax print problem (if I'd ever really known!). I sort of got caught up in the notion that not only is there more than one way to do it, but that any logical way should work as well— here's that expectation thing again…

        hsm
      Yeah well, so I'm not using it directly, IO::Handle that is. Insert sheepish grin here[ ____ ]. In defense of self, this whole exercise arose from someone else's code—which same I reduced to a test case, so that we could look at the array reference problem. In real life, I don't mix approaches like that— no really I don't. Nope, not me, my evil twin (who fires from the hip enough to get hip burns) might, but no, not me!

      hsm

      p.s. thanks again...