Pádraig Brady wrote:
> I'd especially like a review of the perl bits

Hi Pádraig,

I'm not sure that this function will behave quite as you intended it to:

+sub getlimits()
+{
+  my $NV;
+  open NV, "getlimits |" or die "Error running getlimits\n";
+  my %limits = map {split /=|\n/} <NV>;
+  return \%limits;
+}
+

I think that filehandle is opened using a broader scope than you might
be expecting.  It's not using your subroutine-scoped lexical $NV, but
rather a package-scoped "NV" symbol.  If you open $NV and read using
<$NV> it will use your subroutine-scoped lexical variable, as I think
you intended.

Please see the attached demonstration of this:

$ ./filehandles.pl
limits: 'foo'
caller: ''

$ ./filehandles.pl safe
limits: 'foo'
caller: 'bar'

Thanks,

Bo
#!/usr/bin/perl

my ($safe) = @ARGV;

system ("echo -n foo > test_foo");
system ("echo -n bar > test_bar");

sub getlimits {
  my $NV;

  # This clobbers caller's "NV"
  open NV, "<", "test_foo" or die "Failed to open test_foo: $!";
  <NV>;
}

sub getlimits_safe {
  my $NV;

  # This uses subroutine-scoped "$NV"
  open $NV, "<", "test_foo" or die "Failed to open test_foo: $!";
  <$NV>;
}

open NV, "<", "test_bar" or die "Failed to open test_bar: $!";

my $limits = $safe ? getlimits_safe : getlimits;
my $caller = <NV>;

print "limits: '$limits'\n";
print "caller: '$caller'\n";
_______________________________________________
Bug-coreutils mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-coreutils

Reply via email to