in reply to addresses of objects

The -w switch seems to be unnecessary, and the -l switch seems only to assure that a newline is spit out at the end of the print. If you can do without that, and eliminate all unnecessary whitespace, you can save four more keystrokes:

perl -e'print[]+0'

The principle is this: [] creates an anonymous array reference. Evaluated in scalar context, it would appear as (for example) "ARRAY(0x155513c)." The "+0" operation causes the numeric value of the string "ARRAY(0x155513c)" to be added to zero, resulting in the non-numeric portion of "ARRAY(0x155513c)" being dropped. Finally, print automatically represents the hex number in base ten. The result (on my computer) is "22368572", which happens to be the base-10 version of the hex number "0x155513c".


Dave

Replies are listed 'Best First'.
Re^2: addresses of objects
by Jenda (Abbot) on Aug 09, 2004 at 17:02 UTC

    I think you mean "string context", not "scalar context". Scalar context per se doesn't turn a reference to anything.

    And the numeric value of a string "ARRAY(0x155513c)" is zero:

    perl -e "$x = []; print qq{$x}+0"
    In the original case the reference is evaluated in numerical context, not first in string and then numerical. And in numerical context reference evaluates to the address.

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

Re^2: addresses of objects
by tinita (Parson) on Aug 11, 2004 at 10:05 UTC
    The -w switch seems to be unnecessary, and the -l switch seems only to assure that a newline is spit out at the end of the print.
    just commenting on that:
    when i'm coding on commandline i always use -wle now. it's just automatically. without that i would have been bitten so often from a missed warning. and -l is just so convenient.
    also if i copy and paste commandline code from others i find it annoying if it doesn't contain -wl

      I understand the convenience issues with -l, and the helpful guidance of -w warnings. But the original question was prompted by a thread on Perl golf. And a golf solution that saves two keystrokes (wl) without breaking the results is... better golf. ;)


      Dave

        But the original question was prompted by a thread on Perl golf
        oh, i see. i overread (hope this is correct english) that =)