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

Hi,

i have a bug in my program and i cannot figure out. since the procedure is quite complex i'm gonna try to summarize it within the following example. i know this is not the popper way to do it but i'm gonna start here and then give additional examples if needed. so what i have is a perl shell through which i'm sending commands to my os. the shell was written by me and it uses Term::ReadLine and Term::ReadKey for acquiring the input. after it gets an input, it sends it to evaluation using the eval()function. the evaluation here stands for discriminating which functions are allowed in my psh and which are not.example: i do not allow variables under no strict; pragma, then i do not allow calling next unless('something') && print 'something'; and so on....

where is the problem? once i call a function that takes as input a path to the file which is in a different folder, like:

run(open => './path/to/', do=> 'tell what to do', save => './path1/to' +);
what happens, the path variable is send to an object in a library that takes it as :
sub run{ my ($self,%arg) = @_; if (defined $arg{open}){ $self->_open_it_and_run_it(open => $arg{open}); } }
and then in the next sub the procedure gets finished like:
sub _open_it_and_run_it{ my ($self,%arg) = @_; open (IN, "<",$arg{open}) || die "$!"; while (<IN>){ ... } close IN: }
so what happens is that i doesn't matter how many sub processes like this i have, if i set the path for opening to be :
run(open =>'./', ....);
everything is fine but if i set it to be :
run(open =>'./dir/', ....);
once the dir gets opened the whole contents of that dir , gets erased, where in the first case this is not so.

so my question is, does any one has a clue what is going on here, is it a bug in perl or ... because i tried the various combinations and non showed this behavior but this one.

Please state what input do you need to get a clear view. i cannot present the whole code at this time, sorry.(this is not commercial software)

thnx

UPDATE

ok, ppl.

i know i pushed you through some trouble and i am GRATEFULL for all your help. i figured it out but only due to your comments.

there is a problem with my OS. I am running a CentOS 4.7 64 - no updates(i have to!) and for some reason when i run the script as described, when the file is being red it creates a .file.txt for no reason(or just changes the name)and then the file becomes invisible. i have tested the program on the debian and it works like a charm.

So i'm relay sorry for doubting in perl, I know that,as Corion said "suspecting a bug in Perl is a bad approach" but i was desperate.

so once more thank you!!!!!

Replies are listed 'Best First'.
Re: stdin erases all my files
by cdarke (Prior) on Jul 02, 2010 at 13:48 UTC
    Several things puzzle me about this. The first is your post title, which implies there is a problem with stdin. What makes you think that erasing files is connected with stdin in anyway? By the way, none of the code shows stdin being used.

    Behaviour of file IO is operating system dependant, although judging from your mention of rm -r I guess you are working with some sort of UNIX or Linux. Opening a directory for write on such platforms should return error EISDIR (is a directory), although opening for read is allowed.

    So, what ways can the files be erased? I'm guessing you do not have anything as blatent as unlink glob('dir/*'); or an opendir/readdir/closedir loop.
    However there can be some confusion surrounding the < thing > notation in Perl, in that it can mean a glob construct, or it can mean a read from a file handle. Maybe worth checking your syntax on such instructions.

    It sounds that the entire directory list is being erased somehow, but, thinking out of the box, maybe the files were renamed? A dot '.' prefix to the filename would effectively hide the files, so ls -a might be worth a go.

    Other than that, run the program under strace(1) or truss(1) to see the kernel calls.
Re: stdin erases all my files
by Corion (Patriarch) on Jul 02, 2010 at 11:42 UTC

    The error is in your program. Most likely, you overwrite your files because not only do you open them for reading but also open them for writing somewhere.

    As you don't show any working code, it's hard to help you further. I would replace the opening and writing of files by subroutines that simply print what they do instead of doing. This should show you that you are trying to write to the files that you are supposed to read.

    Of course, this is some basic debugging strategy - suspecting a bug in Perl is a bad approach if you haven't done any debugging yourself.

      ok, but then why doesn't it erase my files when i change the path to './', then it behaves normally. but if i switch the path to './somewhere/' it erases the whole ./somewhere/ folder. the folder still exists but it is empty. and i cannot find the bug in my code. plus it does this for every function , procedure.(and only common things for all functions are , stdin, Term::ReadLine and eval()) i would expect that if i had a bug then other functions should not work properly but the behavior is universal. so it led me to a conclusion either something is happening during eval() operation or stdin doesn't work properly with Term::ReadLine so ....

      but thnx :)

      PS

      even if in the folder are files that are totally unrelated with my work, they are also erased. folder gets completely erased it is like i called :rm -r ./dir/ and then mkdir ./dir/ even the existing dir's in that dir get erased...

      agreed, it's gonna take me a while but i try to do it and post it

        How am I supposed to tell from your vague description?

        You need to note that your vague description is necessarily vague because if you could describe your code exactly, you would also know where the problem is. So you will need to show us the code, or at least the reduced code that still exhibits the problem but is shorter than 20 lines.

        Usually this kinds of problems turns to be something else that we haven't considered, and probably you already believe that is something wrong with the open ( which might be or not ) and you focus only on that.

        Try to write a shorter program that only opens a directory and see if that removes everything, ... or run your program using the debugger with perl -d your_program.pl and check if after each step the directory gets wiped out, if so you'd find the line that did it and you'd be available to write a minimal program that evidences the bug ( or new test case for perl 5.12.X :D )