Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Any idea what this does
if (@{$memoryfile}[0]=~/([^[:print:]])/ || @{$memoryfile}[1]=~/([^[:print:]])/ ) { LogMess("Error: Binary input data detected",1); return -1; }

Replies are listed 'Best First'.
Re: Any idea what this does
by ikegami (Patriarch) on May 29, 2007 at 03:00 UTC

    It needlessly uses slices.
    @{$memoryfile}[0]
    would be less misleading as
    ${$memoryfile}[0]
    and I think it would be more readable as
    $memoryfile->[0]

      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...

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

Re: Any idea what this does
by ysth (Canon) on May 29, 2007 at 02:39 UTC
    Complains if the first or second element in the array referred to by the arrayref in $memoryfile contains one or more non-printable characters.

    But it really should have $ instead of @ there. I'm surprised it doesn't warn.

Re: Any idea what this does
by swampyankee (Parson) on May 29, 2007 at 10:35 UTC

    Is this a quiz?

    The regex, itself, is an example of an IEEE Std 1003.1, also known as POSIX character class syntax; see perlre, which states [:print:] is "Any alphanumeric or punctuation (special) character or the space character." The [^] syntax is normal regex syntax, so in this case, the regex will return true if any character which is not in the list of what [:print:] considers "printable" is found either array element.

    The @{...}...appears to be an (possibly incorrectly written) dereferencing; I don't think it's relevant to the question.

    emc

    Any New York City or Connecticut area jobs? I'm currently unemployed.

    There are some enterprises in which a careful disorderliness is the true method.

    —Herman Melville
      The @{...}...appears to be an (possibly incorrectly written) dereferencing; I don't think it's relevant to the question.
      Since the anonymous OP didn't say, it's not possible to know which part wasn't understood. It's quite possible the OP threw up his/her hands in bewilderment at the whole thing, without even trying to break it down into pieces.

      The @{} is correct (in that it does what was presumably intended), just poorly written. It is a one-element slice in scalar context, where a simple array element would have done.

Re: Any idea what this does
by jesuashok (Curate) on May 29, 2007 at 01:42 UTC
    If the first character of @{$memoryfile}[0] or (||) @{$memoryfile}[1] is not a printable character then log a error message and returns -1 value.

      Close... The caret character (^) is a negation operator inside a character class here. Outside the character class, it would indeed be an anchor to the start of the string.

      No. If *any* character.

      And what's with "(||)"?

        He's explaining that "||" means "or".
      Also I feel LogMess must be some subroutine somewhere in the code, which accepts two arguments.