in reply to Re: Any idea what this does
in thread Any idea what this does

It needlessly uses slices.

Using a (single) slice I would probably do:

for (@{$memoryfile}[0,1]) { LogMess("Error: Binary input data detected",1), return -1 if /[^[:print:]]/; }

Somewhat of an overkill for just two values, but IMHO clearer and also easy to modify if you want something more than 0 and 1 there...

Replies are listed 'Best First'.
Re^3: Any idea what this does
by Util (Priest) on May 29, 2007 at 15:43 UTC

    I would probably do this:

    my $has_unprintable_re = qr{ [^[:print:]] }x; if ( grep { /$has_unprintable_re/ } @{$memoryfile}[0,1] ) { LogMess( 'Error: Binary input data detected', 1 ); return -1; }
    , even though your for&if does the same thing as my if&grep.

    My first reading of your code made me say "Hmmm, it could print the error twice... {Re-reading} Oh, wait, the LogMess and the return are in the same statement, and the return will (of course) exit the loop."

      My first reading of your code made me say "Hmmm, it could print the error twice... {Re-reading} Oh, wait, the LogMess and the return are in the same statement, and the return will (of course) exit the loop."

      Indeed, one tiny difference between my approach and yours is that the latter will always iterate on all (both, in this case) items while the former will exit early. Of course I didn't mention "performance" and I was not thinking of it. Do not even dare to suppose I had the slightest idea of micro-optimizing! ;-)