A good way to approach this is to pass in the filehandle as one of the parameters. You can pass it from the caller either using the syntax the previous reply used, or by using the IO::File class to manage your open filehandles. As for the subroutine itself:
sub ReadBytes { my $fh = shift; my $num = shift || 1; # Default to 1 byte instead of error return undef unless ($fh); my $bytes; return undef unless (read($fh, $bytes, $num)); unpack("C$num", $bytes); }
Note that you don't need the for-loop, since both read() and unpack() can use the byte-count you already have. This routine isn't very helpful to the caller in error cases, you may want to do something cleaner with the two points where undef is being returned.
--rjray
In reply to Re: Nested subs - inherently evil?
by rjray
in thread Nested subs - inherently evil?
by myocom
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |