in reply to Re: Help with recurring "uninitialized variable" problem.
in thread Help with recurring "uninitialized variable" problem.

if (!($one_line =~ /^#+/))
That did the trick!

I THINK I understand why...

!~ is a new one on me, BTW. Very cool.

Many thanks!

CT

Charles Thomas
Madison, WI

Replies are listed 'Best First'.
Re: Re: Re: Help with recurring "uninitialized variable" problem.
by Fletch (Bishop) on Apr 09, 2004 at 17:54 UTC

    B::Deparse can shed some light . . .

    $ perl -MO=Deparse,-p -e '$one_line =~ !/^#+/' ($one_line =~ (not /^#+/));

    So what you were really doing was first matching /^#+/ against $_, taking the logical not of what that returned and then attempting to use the string version of that as a regexp to match against $one_line.

    Update: And to further explain, that meant you were always splitting and hence would sometimes get lines without all the fields you expected and those would be undefined.

      >B::Deparse can shed some light . . .

      This is a Perl module? Sorry, this statement doesn't make much sense to me yet.

      >$ perl -MO=Deparse,-p -e '$one_line =~ !/^#+/' >($one_line =~ (not /^#+/));
      >So what you were really doing was first matching /^#+/ against $_,

      Why is that? Doesn't the =~ operator mean that I'm matching against whatever is to the left of the operator? Was it matching against $_ because of where I put the "not" operator(!)?

      >taking the logical not of what that returned and then attempting to use the string version of that as a regexp to match against $one_line.

      I guess I wish I understood this better. I'll keep at it.

      Charles Thomas
      Madison, WI

        B::Deparse tells perl to spit back what it parsed your code as, optionally adding parens which you may have omitted to make precedence clearer.

        As for =~, it does say match against what's on the left hand side; the problem in this case is that what you had on the right hand side wasn't a simple m// so it was treated as an expression yielding a regexp object as if you'd used qr//. In other words, what you did was more:

        $foo = /^#+/; # match against $_. a false '' since you never set $_ $bar = not $foo; # take the logical opposite, so always "1" $baz = qr/$bar/; # make a regep object from the contents matching "1" $one_line =~ m/$baz/; # returns true if $one_line has a 1