Sam wrote:
>
> Thanks for all your responses; I learned a bit.

Good: well done.

> 1.  I wasn't clear on $_ in my email; that's being read elsewhere in program so
> it's already set by the time print_lines is called.

You /mustn't/ use $_ like that. It's meant to be an 'it' in places like

  For each banana, look at it, smell it, and eat it.

NOT

  When I say 'it', it means 'banana 3 from crate 5'.
  For each apple, look at it, smell it, and eat it.
  Do it to it.

> 2.  Will you bet your life on this equivalence: "not /^\s*$/ == /\S/"?  I
> believe it's safer to negate an EXPR than to find some other EXPR2 that is
> equivalent to 'not EXPR'.

Yes. (But I can think of more valuable things to bet on.)

The first says,

  Does the entire string, from start to end, consist of zero or more 'space' 
characters?

The second says,

  Does the string contain a non-'space' character anywhere?

The only difficulty could be with a zero-length string, which is still fine, if
you think about it.

> 3.  Hadn't thought about xor; good to know.

Be careful though. 'xor' goes with the other low-precedence operators 'and'
and 'or', which have the high-precedence equivalents && and ||. 'xor' has
no equivalent. (The caret, ^, is a bitwise operator, which is completely
different.)

> 4.  As for the generalized case, I learned about using refs.  Anonymous subs
> also work.
>
> my $re  = sub { return /^\s*$/; };
> my $nre = sub { return not &$re; };

Eek. Don't do that. It may work, but it's not at all obvious what $_ is in the
context of a subroutine call. Having $nre dereference $re at each call isn't
your best bet either.

> my $expr = $blank ? $re : $nre;
> do ... while (&$expr and not eof);

The better syntax for a call to a subroutine reference is

  $expr->()

which doesn't assume all sorts of things that could keep you awake at night if
you knew.

> But I'm not sure which is faster though.

If you don't know, it doesn't matter.

If your software is /too/ slow and you would die for a 10% speed increase then
check out

  use Benchmark;

If not, don't bother.

HTH,

Rob



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to