bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] tests: refactor to use the new getlimits utility


From: Bo Borgerson
Subject: Re: [PATCH] tests: refactor to use the new getlimits utility
Date: Fri, 12 Dec 2008 11:22:55 -0500
User-agent: Thunderbird 2.0.0.18 (X11/20081125)

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";

reply via email to

[Prev in Thread] Current Thread [Next in Thread]