Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Inappropriate I/O control operation

by cormanaz (Deacon)
on Sep 26, 2017 at 15:22 UTC ( [id://1200118]=perlquestion: print w/replies, xml ) Need Help??

cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day Bros. I am getting Inappropriate I/O control operation when trying to open some text files. I have been coding Perl for a long time and this has never happened to me, so I'm a little flummoxed. I originally tried to do the open with the typeglob IN which I have always used before. I researched it a little and found some suggestions that this can cause the error and it was better practice to use a lexical filehandle, so I did so:
#!/usr/bin/perl -w use strict; $| = 1; my @coders = qw(kristin madison scott sean steve zeb); my @files = qw( 17524106 18033790 19683851 27033992 3987896 4058888 4139141 4454294 5303897 5380762 ); foreach my $f (@files) { foreach my $c (@coders) { my $fn = "round1/$c/$f\.txt"; open(my $in, $fn); print "$f\t$c\t$!\n"; close $in; } }
Every open returns an output like 17524106    kristin    Inappropriate I/O control operation Another strange thing is that the above is a pared-down version of a script that actually processes the data in the files. When I ran that, it read and processed the files for some of the @coders (I/O error notwithstanding) but not the others.

Not sure what I'm doing wrong or how to fix it. Suggestions?

Replies are listed 'Best First'.
Re: Inappropriate I/O control operation (Updated)
by 1nickt (Canon) on Sep 26, 2017 at 15:25 UTC

    Update: OIC, you are using $! in your print statement. I missed that. Why? What are you trying to print?

    As explained below by dave_the_m, you use $! after an error when it might hold a message about the error (if whatever encountered the error is nice enough to tell you). That's why I used it to check the return value of your file open call, which is where I assumed the error was coming from: it will tell you if you've made a typo in the path and the file does not exist, for example. In your case it is printing something unuseful because you're not using it the right way. What did you want to print there?

    Original reply:


    Try adding error checking and debugging to your program:

    open(my $in, '<', $fn) or die "Died opening file: $fn error: $!";
    Also note the three argument form of open, which is safer.

    Hope this helps!


    The way forward always starts with a minimal test.
      Thanks for the suggestion. I did print $! in the statement underneath the open in the original script. But I modified the open per your suggestion. It didn't die on any of the opens but still gave the error in $! in the following line. Then I tried reading and printing the first line of each file and it worked. Strange.
        $! is only meaningful after an error. Its value after a successful open is unspecified and could be anything.

        Dave.

Re: Inappropriate I/O control operation
by talexb (Chancellor) on Sep 26, 2017 at 18:09 UTC

    I'm a little puzzled -- what problem are you trying to solve here?

    If you're just looking to see if the file exists (seeing as you just open and close the file), why not just test for the existence of the file with -e? You could also check for whether the file's empty with -z.

    I see an opportunity to use join on both your input and output ..

    my $f = join ( '-', 'round1', $c, $f ) . '.txt'; my $exists = ( -e $f ) ? 'exists' : 'doesn\'t exist'; my $zero = ( -z $f ) ? 'empty' " 'not empty'; print join ( '-', $f, $c ) . ": $exists / $zero\n";
    Coded but not tested. :)

    PS Yeah, the $zero value is meaningless if the file doesn't exist.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

    Edit: Fix inadvertent backslash in second paragraph.

Re: Inappropriate I/O control operation
by virtualsue (Vicar) on Sep 26, 2017 at 15:55 UTC
    If you don't check the return values from system calls, then you won't know why they failed. It'd be nice if you didn't make the unnecessary assumption that you're talking just to men in your post, by the way.
      That's intended to be a play on the "Monks" theme, not a sexist reference.

        Monks = Brothers = Friars ... and all our other levels. What about Perlnuns.org?

Re: Inappropriate I/O control operation
by Anonymous Monk on Sep 26, 2017 at 18:26 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-23 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found