BUG: inheritance 
Author Message
 BUG: inheritance

I've stumbled across a bug in STDIN inheritance (Perl 5.00305, Sun
Solaris 2.5.1): Consider this script:

#!/usr/local/bin/perl -w

chomp($group = <STDIN>);
print qq{Group is "$group"\n};
system('newgrp',$group);
__END__

When started, it stops for the entry of a group-ID. When the group ID is
entered, the newgrp shell starts up and when the command 'id' is typed, the
correct (new) group ID is printed.
Consider a file 'infile' that contains the two lines:
-----
<groupname>
id
-----
When the script is invoked as 'script < infile', the newgrp command does
not get the 'id' command and exits without printing the ID.

On the other hand, if you modify the script so that it does NOT use
<STDIN>, e.g.

#!/usr/local/bin/perl -w

$group = $ENV{'NEWGROUP'};
print qq{Group is "$group"\n};
system('newgrp',$group);

it works both ways, i.e. STDIN may be a tty or reading from a file.

To me it seems that one I've read from <STDIN>, system() does not inherit
the right stdin. Does a workaround exist for that?

Regards,

Marek

PS. To send reply by Email, please remove -nospam- from the address. Thank You.

--
  Marek Rouchal                   Phone : +49 89/636-25849
     SIEMENS AG, HL CAD SYS       Fax   : +49 89/636-23650




Tue, 11 Jan 2000 03:00:00 GMT  
 BUG: inheritance

Quote:

> I've stumbled across a bug in STDIN inheritance (Perl 5.00305, Sun
> Solaris 2.5.1): Consider this script:

> #!/usr/local/bin/perl -w

> chomp($group = <STDIN>);
> print qq{Group is "$group"\n};
> system('newgrp',$group);
> __END__

> When started, it stops for the entry of a group-ID. When the group ID is
> entered, the newgrp shell starts up and when the command 'id' is typed, the
> correct (new) group ID is printed.
> Consider a file 'infile' that contains the two lines:
> -----
> <groupname>
> id
> -----
> When the script is invoked as 'script < infile', the newgrp command does
> not get the 'id' command and exits without printing the ID.

Has it occured to you to test the newgroup command from a shell script
or from C to see if, perhaps, it is opening /dev/tty for the shell's
input stream, or if it's doing some other such silly thing?

                        -AJS



Tue, 11 Jan 2000 03:00:00 GMT  
 BUG: inheritance

Quote:

>I've stumbled across a bug in STDIN inheritance (Perl 5.00305, Sun
>Solaris 2.5.1): Consider this script:

You've stumbled accross stdio buffering.

Quote:
>When the script is invoked as 'script < infile', the newgrp command does
>not get the 'id' command and exits without printing the ID.

The way that stdio libraries are often implemented, when a program
starts, it determines whether standard input is a terminal
device. When it is, it sets buffering for line buffering, if not it
uses block buffering.

When using line buffering, when you call an input operator, the
library uses the read system call to grab a lines of text, those lines
are put into the stdio buffers, and then passed back to the
application as needed.

When using line buffering, the stdio library will grab large chunks of
data at a time. Usually a size that is efficient for the
filesystem. These large chunks will be stored in the stdio buffers,
and passed to the application as it requests them. This makes the
program run more efficiently, since it has to perform fewer read
system calls.

In your case, you asked perl for one line, and it grabbed the entire
file for future use.

I guess you could grab single characters via sysread() until you got
your first line. Or open a pipe between your perl script and the
newgrp command and pass it that data from STDIN.

use FileHandle;
open GROUP, "|newgrp $group" or die "Error: $!\n";

GROUP->autoflush();  # try to minimize that stdio buffering.
while(<>) {
  print GROUP $_;

Quote:
}

--
Andrew Langmead


Wed, 12 Jan 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Order of packages/inheritance bug?

2. class data inheritance

3. inheritance problem

4. Multiple Inheritance problem calling base class constructors

5. Tied hash for multiple inheritance of data

6. Constructors and inheritance

7. Inheritance question.

8. inheritance and "private clases"

9. Finding your way home...or Inheritance

10. Inheritance in tk

11. Perl parent-child object inheritance

12. DBI methods inheritance

 

 
Powered by phpBB® Forum Software