Author image Valery Kalesnik
and 1 contributors

NAME

FTN::Bit_flags - Object-oriented module for working with bit flags.

VERSION

version 20160324

SYNOPSIS

  use Log::Log4perl ();
  use FTN::Bit_flags ();

  Log::Log4perl -> easy_init( $Log::Log4perl::INFO );

  # let's work with message attributes
  my $attribute = FTN::Bit_flags -> new( { abbr => 'PVT',
                                           name => 'PRIVATE',
                                         },
                                         { abbr => 'CRA',
                                           name => 'CRASH',
                                         },
                                         { abbr => 'RCV',
                                           name => 'READ',
                                         },
                                         { abbr => 'SNT',
                                           name => 'SENT',
                                         },
                                         { abbr => 'FIL',
                                           name => 'FILEATT',
                                         },
                                         { name => 'TRANSIT',
                                         },
                                         { name => 'ORPHAN',
                                         },
                                         { abbr => 'K/S',
                                           name => 'KILL',
                                         },
                                         { name => 'LOCAL',
                                         },
                                         { abbr => 'HLD',
                                           name => 'HOLD',
                                         },
                                         { abbr => 'XX2',
                                         },
                                         { abbr => 'FRQ',
                                           abbr => 'FREQ',
                                         },
                                         { abbr => 'RRQ',
                                           name => 'Receipt REQ',
                                         },
                                         { abbr => 'CPT',
                                         },
                                         { abbr => 'ARQ',
                                         },
                                         { abbr => 'URQ',
                                         },
                                       );

  $attribute -> set_from_number( get_attribute_from_message() );

  print join ', ', $attribute -> list_of_set;

  print 'this is a private message'
    if $attribute -> is_set( 'PVT' );

  # make sure it is local and its flavour is crash
  $attribute -> set( 'LOCAL', 'CRASH' );

  # though we don't need it to be killed after sent
  $attribute -> clear( 'K/S' );

  update_message_attribute_field( $attribute -> as_number );

  $attribute -> set_from_number( get_attribute_from_another_message() );

  # work with new attribute value the same way as above

DESCRIPTION

FTN::Bit_flags module is for working with bit flags commonly used in FTN messages.

OBJECT CREATION

new

  my $bit_flags = FTN::Bit_flags -> new( { abbr => 'flag 1' },
                                         { name => 'second lowest bit' },
                                         { abbr => 'flag 2',
                                           name => 'flag numeric mask is 4'
                                         }
                                       );

Parameters are hash references representing bit in order from low to high. At least one parameter is required. Each hash reference should have 'abbr' and/or 'name' fields. Dies in case of error.

set_from_number

After object describing all possible fields is created we can use it to work with already defined value:

  $bit_flags -> set_from_number( 3 );

clear_all

We can clear all bitfields (setting numeric value to 0):

  $bit_flags -> clear_all;

set

To set one (or more) fields:

  $bit_flags -> set( 'second lowest bit', 'flag 2' );

If you have equal 'abbr' for one field and 'name' for another field, then 'abbr' has higher priority here.

clear

To clear one (or more) fields:

  $bit_flags -> clear( 'second lowest bit' );

If you have equal 'abbr' for one field and 'name' for another field, then 'abbr' has higher priority here.

is_set

To check if some field is set:

  print 'it is set'
    if $bit_flags -> is_set( 'second lowest bit' );

If you have equal 'abbr' for one field and 'name' for another field, then 'abbr' has higher priority here.

as_number

To get numeric value after you set or cleared some flags:

  print $bit_flags -> as_number;

list_of_set

To get list of set flags:

  print join ' ', $bit_flags -> list_of_set;

By default it tries to return 'abbr' field value for each set bit and if there is none, then return 'name' field value. If 'name' field is preferable, pass optional parameter 'name'.

  print join ' ', $bit_flags -> list_of_set( 'name' );

AUTHOR

Valery Kalesnik, <valkoles at gmail.com>

BUGS

Please report any bugs or feature requests to bug-ftn-bit_flags at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FTN-Bit_flags. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

  perldoc FTN::Bit_flags