On 3/11/06, Ben Tilly <btilly-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx> wrote:
> On 3/11/06, Joel Gwynn <joelman-Re5JQEeQqe8AvxtiuMwx3w@xxxxxxxxxxxxxxxx>
> wrote:
> > I know I've done this before, but I'm not sure what I'm doing
> > differently today. I'm trying to capture a simple command-line option
> > like so:
> >
> > my $debug = 0;
> >
> > if(grep(/--debug=(\d+)/, @ARGV)){
> > $debug = $1;
> > print "debug: $debug\n"; # Error here
> > }
> >
> > But I keep getting "Use of uninitialized value in concatenation (.) or
> > string" when I try to do something with the debug variable. How can
> > $1 not be initialized? If it's matching, then it should have a value,
> > no?
>
> That looks like a bug to me. But you can work around it as follows:
Correcting myself, I don't think it is a bug. $1 is dynamically
scoped. In your construct above, that means that when grep ends, $1
is cleaned up.
> while(grep(/--debug=(\d+)/, @ARGV)){
> $debug = $1;
> print "debug: $debug\n";
> last;
> }
And I think this works because the inner part of the while loop
executes while the grep is still executing. Which is a Perl
optimization to avoid generating a long temporary list in this
situation.
Cheers,
Ben
|