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

I have never seen this before:
$uptime=`cat /proc/uptime`; print ref(\$uptime)," $uptime\n"; $uptime*=1; print ref(\$uptime)," $uptime\n";
which prints
SCALAR 12844.72 24452.05 SCALAR 12844.72
It's not really a dereference is it? This is perl 5.10-comes-with-CentOS, so the * dereference operator had not been invented yet. Or is this a variation on *foo{THING}? Even more odd:
$uptime=`cat /proc/uptime`; print ref(\$uptime)," $uptime\n"; $uptime*=2; print ref(\$uptime)," $uptime\n";
Returns
SCALAR 12956.65 24667.72 SCALAR 25913.3
No idea where that second value of $uptime comes from ... I would do this with a regular expression myself, much more obvious ... Thanks in advance, w

Replies are listed 'Best First'.
Re: What is the * postfix operator, pre-'feature postderef'?
by roboticus (Chancellor) on Sep 10, 2014 at 20:40 UTC

    wsanders:

    No, it's a shortcut. It turns out that:

    $uptime *= 2;

    is a shortcut for:

    $uptime = $uptime * 2;

    There's also +=, etc. See perldoc perlop for all the details.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Gaaak I feel STUPID! Of course you are correct. The lack of spaces threw off my disused spatial recognition skills. And I just got new glasses, sheesh .... But it also threw me off that multiplying a string containing spaces by 1 results in the first "field" of the string, the same as 's/\s.*//'. One really ought to use:
      $uptime=`cat /proc/uptime`; print ref(\$uptime)," $uptime\n"; $uptime =~ s/\s.*//; print ref(\$uptime)," $uptime\n";

        If you want the first numeric field I'd use:

        ($uptime) = $uptime =~ /(\d+)/;

        which makes the intent much clearer. What's with the ref(\$uptime)?

        Perl is the programming world's equivalent of English