Weird Perl error with "Inappropriate ioctl for device"
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Weird Perl error with "Inappropriate ioctl for device"
I am in the process of building 'upgraded' servers and I have moved over all of our previously working scripts. All of the scripts worked fine with no errors and now some of them are dying with a "Inappropriate ioctl for device" error. Here is some detail:
Like I said, just doing a simple open works fine, but I don't want the script to just exit. We need to do error checking so that if 'a' fails then do 'b' type of thing. I figured out what the issues were with the eval statements, the programmers that wrote those scripts were using the wrong variable to check the status of the eval (this is what happens in larger shops when people clone programs without understanding what they are doing. So if it is broke in the original program then it will be broken in the clone.)
I still have not figured out why we get the errors when using the unless statement though.
I know that the () are not required but it is easier for non-Perl programmers to follow and understand when the () are used. I think it is just a matter of taste, but I also think it is good coding. You have to remember that in most real world cases YOU are not the only person that will ever look at the code. Sometimes even when YOU are the one looking at the code and you have not seen it in a year or more you have a hard time following along. ;-)
Anyway if anybody out there knows why wrapping an open in an unless statement throws the error I sure would love to know.
The error you are getting is a spurious one and is caused by
the fact that the system error variable is checked too late
by the code you are using.
Here is some code that should work:
if open(FH, "<test.txt") {
#read the file here
}
else {
$errmsg = "File open problem ".$!;
$errors = $errors + 1;
error_exit($errmsg);
#error_exit is my error handling sub routine
}
This code probably won't work because it checks $! tto late
if open(FH, "<test.txt") {
#read the file here
}
else {
$errors = $errors + 1;
$errmsg = "File open problem ".$!;
error_exit($errmsg);
#error_exit is my error handling sub routine
}
The value of $! holds the last system error message but it
gets set by almost everything so if you don't check it as
soon as possible you will get the spurious error you are
seeing. Correcting your code may show up a real file error which you can then correct. Hope this helps.
BTW, in certain circumstances (not necessarily with open() ), you get different results in Perl, depending on whether you use parentheses or not. Note that many things are implemented as fns/subs behind the scenes, as Perl is written in C.
Personally, if I know or reasonably suspect this is the case, I use (), it makes code reading clearer (as mentioned above).
FYI, the Perl manual, ie Camel book starts the defn of 'open' by stating 'This function opens the file ...'
Just wanted to let everybody know I figured it out. This is a problem with people cloning code without understanding what they are doing. The problem is close to what was stated above with the $! variable. They were checking the wrong variable after their eval statement therefore messing things up. Here is an example of what they were doing:
Quote:
eval { open(FH, $somefile); };
if ($!) {
...
When I changed it to look at the correct variable without changing any other code to look like this:
even my program worked if i used "$@" instead of "$!".
But I have no idea what it is.. The program I changed was written by someone else and not me.
So could you please explain what is $! and $@ in perl. I am beginner in perl.
$@ The Perl syntax error message from the last eval() operator.
If $@ is the null string, the last eval() parsed and executed
correctly (although the operations you invoked may have failed
in the normal fashion). (Mnemonic: Where was the syntax error
"at"?)
Warning messages are not collected in this variable. You can,
however, set up a routine to process warnings by setting
$SIG{__WARN__} as described below.
Also see "Error Indicators".
$! If used numerically, yields the current value of the C "errno"
variable, or in other words, if a system or library call fails,
it sets this variable. This means that the value of $! is
meaningful only immediately after a failure:
if (open my $fh, "<", $filename) {
# Here $! is meaningless.
...
} else {
# ONLY here is $! meaningful.
...
# Already here $! might be meaningless.
}
# Since here we might have either success or failure,
# here $! is meaningless.
In the above meaningless stands for anything: zero, non-zero,
"undef". A successful system or library call does not set the
variable to zero.
If used as a string, yields the corresponding system error
string. You can assign a number to $! to set errno if, for
instance, you want "$!" to return the string for error n, or
you want to set the exit value for the die() operator.
(Mnemonic: What just went bang?)
Also see "Error Indicators".
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.