in reply to Avoid run-time checking

Are the files ever mixed endianness? Where does $endian come from , how is endianness determined?

So far, the only idea I have came up - create 2 tables of unpack template, one for big and one for little and select once after determining endianness.

That can make sense, you've identified a repetitive copy/paste pattern and you're refactoring/abstracting away the repetitiveness, so next step is to actually make two modules with same API, so you can  use MyRecs -bigendian; and you get in your program a record_xxx() which is an alias for MyRecs::BigEndian::record_xxx()

Replies are listed 'Best First'.
Re^2: Avoid run-time checking
by bagyi (Acolyte) on Sep 12, 2015 at 07:34 UTC

    >>Are the files ever mixed endianness? Where does $endian come from , how is endianness determined?

    No. The endianness is fixed. it is specified in first record of file. Using table approach seems viable, but I don't like to separate subroutine and template string in different places if possible.

      Using table approach seems viable, but I don't like to separate subroutine and template string in different places if possible.

      :) All approaches are viable, it will always come down to your prejudice/bias/choice :D

      so type up a few 1-3 subs of each and then compare/contrast what you like about each

      my %templates; $templates{big}{foo} = 'v3A*'; $templates{lil}{foo} = 'n3A*'; $templates{big}{bar} = 'v2A*'; $templates{lil}{bar} = 'n2A*'; sub record_foo { my( $end, ... ) = ; my @res = unpack $templates{$end}{foo}, ...; ...; } sub record_bar { my( $end, ... ) = ; my @res = unpack $templates{$end}{foo}, ...; ...; } %templates = ( big => { bar => "v2A*", foo => "v3A*", }, lil => { bar => "n2A*", foo => "n3A*", }, ); sub record_foo { my( $big, ... ) = ; my @res = unpack $big? "v3A*" : "n3A*", ...; ...; } sub record_bar { my( $big, ... ) = ; my @res = unpack $big? "v2A*" : "n2A*", ...; ...; }