windowsperl

Code ambiguity when -X file handle for file size


echo 444 > temp.txt
perl -MData::Dumper -E "open $f, pop; print Dumper [-s $f, -s($f) +10]" temp.txt

$VAR1 = [
          6,
          undef
        ];

How to tell perl it should take the size of a file and add arbitrary number to it?


Solution

  • ( -s $f ) + 10
    

    Unlike a sub call and unlike most function invocations, an opening paren following -s isn't taken as the start of an argument/operand list.

    This means that

    -s($f) +10
    

    is parsed identically to

    -s ( $f + 10 )
    

    In this sense, -s is closer to an operator like ! than an operator like print.

    The usual way to override precedence is by using parens, so a solution is to use

    ( -s $f ) + 10