Concerning your code ... TL;DR ... sorry!
But
> This code is working. But I would be interested in your opinion. What could I do better?
dualvar means fiddling with internals, especially if you need to tie it with even more magic, too.
I'd reserve this approach for really hard to achieve stuff or temporary tricks (like for debugging)
IMHO using OOP together with overload for stringification and numification (see overload# String, numeric, boolean, and regexp conversions ) offers a much better interface and is easier to maintain. (untested!°)
HTH! :)
update
°) see Cookbook section in overload for an example
# Two-face Scalars
# Put this in two_face.pm in your Perl library directory:
package two_face; # Scalars with separate string and
# numeric values.
sub new { my $p = shift; bless [@_], $p }
use overload '""' => \&str, '0+' => \&num, fallback => 1;
sub num {shift->[1]}
sub str {shift->[0]}
# Use it as follows:
require two_face;
my $seven = two_face->new("vii", 7);
printf "seven=$seven, seven=%d, eight=%d\n", $seven, $seven+1;
print "seven contains 'i'\n" if $seven =~ /i/;
# (The second line creates a scalar which has both a string value, and
+ a numeric value.) This prints:
seven=vii, seven=7, eight=8
seven contains 'i'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.