LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2006, 01:39 PM   #1
bnutting
LQ Newbie
 
Registered: Dec 2003
Posts: 9

Rep: Reputation: 0
Exclamation 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:

Old working systems:
SuSE 7.3
Perl 5.6.1

New non-working systems:
SuSE 9.3
Perl 5.8.6

The lines that these scripts are dying on are:
Quote:
unless (open(STATFILE, ">",$status_file)) {
or
Quote:
eval { open(STATFILE, ">",$status_file) || die; };
Now if I do a simple:
Quote:
open(STATFILE, ">",$status_file) || die;
Things seem to be fine. I would like to wrap the open statements for error checking, why are these previously working scripts failing now?

I have done a couple of hours of googling and have not found any answers.

Thanks,
Bryan
 
Old 01-20-2006, 12:25 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,141

Rep: Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671
I use this form:
# Open text file
open( TEXT_FILE, "<$text_file" ) or
die "Can't open text file: $text_file: $!\n";
 
Old 01-20-2006, 01:52 AM   #3
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
I kept getting this error in my programs, and I (finally) figured out that the best format to use is:

open fh, "> my_file.my_format" or die
"You idiot you are not worthy of using this program!\n $!\n";
 
Old 01-20-2006, 01:55 AM   #4
DanTaylor
Member
 
Registered: Jan 2006
Distribution: Debian Sarge
Posts: 265

Rep: Reputation: 30
Quote:
Originally Posted by chrism01
open( TEXT_FILE, "<$text_file")

Why the parenthesis?? it seems like a waste of typing for me. Never quite understood why people use this format.
 
Old 01-20-2006, 09:25 AM   #5
bnutting
LQ Newbie
 
Registered: Dec 2003
Posts: 9

Original Poster
Rep: Reputation: 0
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.

Thanks,
Bryan
 
Old 01-23-2006, 07:22 PM   #6
shaunw
Member
 
Registered: Dec 2005
Posts: 77

Rep: Reputation: 15
Smile Detecting files errors and $!

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.
 
Old 01-23-2006, 07:34 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,141

Rep: Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671Reputation: 2671
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 ...'
 
Old 01-24-2006, 09:06 AM   #8
bnutting
LQ Newbie
 
Registered: Dec 2003
Posts: 9

Original Poster
Rep: Reputation: 0
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:
Quote:
eval { open(FH, $somefile); };
if ($@) {
...
The program works as expected.

Thanks again to everyone!
Bryan
 
Old 03-12-2012, 03:09 AM   #9
priyankarathi
LQ Newbie
 
Registered: Apr 2009
Posts: 4

Rep: Reputation: 0
hi,

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.

Thanks,
Priyanka
 
Old 03-12-2012, 08:25 AM   #10
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
man perlvar
Code:
       $@      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".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
kismet "channel get ioctl failed...no such device" fatblueduck Linux - Software 0 01-03-2006 02:51 AM
Getting error: "ALSA device "default" is already in use by another program." brynjarh Debian 7 02-04-2005 12:45 PM
Inappropriate ioctl for device(25) why1957 Linux - Networking 0 08-03-2003 05:45 AM
stty: standard input: Inappropriate ioctl for device fwalthard Linux - General 0 07-14-2003 03:55 AM
LFS 4.1: Stalled at Perl, "missing seperator" error from "make" SparceMatrix Linux From Scratch 1 06-07-2003 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration