in reply to Counting bytes in a Unicode document

Replacing

open my $FH, '<', $file
with
open my $FH, '<:raw', $file

will do the trick, but the aforementioned stat solution is much more efficient.

Replies are listed 'Best First'.
Re^2: Counting bytes in a Unicode document
by LanX (Saint) on Oct 08, 2024 at 17:17 UTC
    > the aforementioned stat solution is much more efficient

    I suppose the -s in the OP's code does exactly the same.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Yes, -s is just another way to stat.

      For example, one could write the following:

      defined( my $s = -s $qfn ) or die( "Can't stat `$qfn`: $!\n" ); ... $s ...

      But it could also be written as follows:

      stat( $qfn ) or die( "Can't stat `$qfn`: $!\n" ); ... -s _ ...

      or

      defined( my $s = ( stat( $qfn ) )[ 7 ] ) or die( "Can't stat `$qfn`: $!\n" ); ... $s ...
        It's worth also mentioning the idiom of stat $file; ... (stat(_))[7] to use the result of the last stat.