| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # convert::number Brik | ||
| 5 | # | ||
| 6 | package Metabrik::Convert::Number; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | commands => { | ||
| 19 | to_hex => [ qw(int_number) ], | ||
| 20 | to_int => [ qw(hex_number) ], | ||
| 21 | }, | ||
| 22 | }; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub to_hex { | ||
| 26 | my $self = shift; | ||
| 27 | my ($int) = @_; | ||
| 28 | |||
| 29 | $self->brik_help_run_undef_arg('to_hex', $int) or return; | ||
| 30 | |||
| 31 | if ($int !~ /^[0-9]+/) { | ||
| 32 | return $self->log->error("to_hex: invalid format for int [$int]"); | ||
| 33 | } | ||
| 34 | |||
| 35 | return sprintf("0x%x", $int); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub to_int { | ||
| 39 | my $self = shift; | ||
| 40 | my ($hex) = @_; | ||
| 41 | |||
| 42 | $self->brik_help_run_undef_arg('to_int', $hex) or return; | ||
| 43 | |||
| 44 | if ($hex !~ /^[0-9a-f]+$/i && $hex !~ /^0x[0-9a-f]+$/i) { | ||
| 45 | return $self->log->error("to_int: invalid format for hex [$hex]"); | ||
| 46 | } | ||
| 47 | |||
| 48 | return sprintf("%d", hex($hex)); | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; | ||
| 52 | |||
| 53 | __END__ | ||