http://www.perlmonks.org?node_id=1166928


in reply to Re: archival storage ($/--)
in thread archival storage

Thanks for the heads up. It also seems the string "-1" in the second file shortens what is worked on there and leaves the rest as plaintext.

This would be fixed by using undef$/; rather than $/--;. However, this is supposed to be using the record size setting mentioned in perlvar rather than stringification. According to perlvar, for 5.22 and 5.24:

Trying to set the record size to zero or less is deprecated and will cause $/ to have the value of "undef", which will cause reading in the (rest of the) whole file.
... and using $/-- does leave the variable with a numeric value of -1. In a suitably old version of perlvar (that for 5.10.1), the text reads:
Trying to set the record size to zero or less will cause reading in the (rest of the) whole file.
... and in perl 5.10.1 we also get that the numeric value has been successfully set to -1.
perl -e '$/--; printf "%d\n", $/; printf "%f\n", $/; $foo = <>; $bar = + <>; $_ = $foo ^ $bar; print $_ . "\n"' in1 in2 -1 -1.000000 '1
where in1 is:
abc-1def
and in2 is:
12345678
while the proper output for the important line is:
PPPRR^

This appears to my eyes to be a bug in perl or the documentation, but in the bigger picture is what happens when one pushes into dark corners in the name of cleverness.

Replies are listed 'Best First'.
Re^3: archival storage ($/=\)
by tye (Sage) on Jun 30, 2016 at 15:19 UTC
    $/=\-1;

    works for me. The \ character is important as it isn't being a number that causes $/ to be interpreted as a record length, but it being a reference to a scalar (containing what gets interpreted as a number).

    - tye        

      Ah. So it seems on rereading very carefully, I've tripped up on some slight ambiguities in this line:

      Setting $/ to a reference to an integer, scalar containing an integer, or scalar that's convertible to an integer will attempt to read records instead of lines, with the maximum record size being the referenced integer.
      That should be read as reference to (integer|scalar containing integer|scalar convertible to an integer) and not as (reference to integer|scalar containing an integer|scalar convertible to an integer). It's not talking about scalar values. It's talking about Perl's idea of Scalar variables, a definitional ambiguity for which I should have been prepared as it's peppered through the Perl documentation that way. The "reference to" part covers all three cases in the list, a structural ambiguity that I should have tested out.

      So in trying to golf out three characters and testing edge cases insufficiently (IIRC I tested with all alpha text) I ended up with a bug on my arm. Again, if clear but still shortened was the goal I'd have just gone with undef$/, but $/=\-1; is pretty nice. It still comes down to trying to be overly clever. It's a minimal code change to fix it and the code still makes its intended point with or without the bug.

      Just please forgive me if I don't patch the hardcopy right away...

        Yeah, that is badly ambiguous. Thanks for quoting it. I think it also suffers from stressing unimportant aspects too much, making it more confusing beyond the ambiguity. The minimal change I'd make would be to go more like:

        Setting $/ to a reference to an integer (or a reference to a scalar that contains an integer or that is convertible to an integer) will attempt to read records instead of lines, with the maximum record size being the referenced integer.

        You should submit a doc patch request.

        Just please forgive me if I don't patch the hardcopy right away...

        I seriously considered not posting at all, going back and forth several times before finally deciding. I liked your tattoo (that you recently linked to a photo of in chatter) but I didn't understand what $/-- was meant to do so I went looking for some context. It is still a great tattoo. Now you just know that it has some extra "character" and of a variety that most will be completely unaware of. In some ways that makes it more interesting, more subtle.

        IMHO, you don't need to ever patch the hardcopy. I'm still jealous that you came up with such a cool tattoo idea and I'm still waiting to come up with one cool enough for me to do.

        - tye