Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

User variables with two values?

by Anonymous Monk
on Aug 19, 2012 at 21:08 UTC ( #988358=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

$! acts like both a string or an integer depending upon context.

Can I create my own variables that do the same? How?

Replies are listed 'Best First'.
Re: User variables with two values?
by DamianConway (Beadle) on Aug 19, 2012 at 21:55 UTC
    For a lighter solution, just use a (built-in) dualvar:
    use Scalar::Util 'dualvar'; my $funny = dualvar 12, 'pancake'; printf "Polly wanna %s every %d days \n", $funny, $funny; printf "Polly wanna %s every %d days \n", $funny, 0+$funny; __END__
    Or, if you really do want a full object-based solution, Contextual::Return can do the hard work (of setting up the class and overloading) for you:
    use Contextual::Return; my $funny = NUM { 12 } STR { 'pancake' }; printf "Polly wanna %s every %d days \n", $funny, $funny; printf "Polly wanna %s every %d days \n", $funny, 0+$funny; __END__
    But, if you just want number and string, a dualvar is probably the better choice.
Re: User variables with two values?
by Anonymous Monk on Aug 19, 2012 at 21:33 UTC

    See overload, Polly wanna pancakes every 12 days

    #!/usr/bin/perl -- use strict; use warnings; my $funny = NumStr->new( num => 12, str => "pancakes" ); printf "Polly wanna %s every %d days \n", $funny, $funny; printf "Polly wanna %s every %d days \n", $funny, 0+$funny; BEGIN { package NumStr;{ use overload "0+" => \# use overload '""' => \&str; use overload "fallback" => 1; sub new { my $class = shift; bless { @_ }, $class; } sub num { return shift->{num}; } sub str { return shift->{str}; } $INC{'MyDo.pm'}=__FILE__; } }
Re: User variables with two values?
by Marshall (Abbot) on Aug 20, 2012 at 06:24 UTC
    DamianConway has given an answer about "how to do it".

    I am curious as to "why?".

    Many strange things are possible in Perl. Not all strange things that are possible are "good things". Can you provide more context about "why"?- meaning an example application?

Re: User variables with two values?
by Khen1950fx (Canon) on Aug 20, 2012 at 06:26 UTC
    You could use constants via Constant::Generate:
    #!/usr/bin/perl -l use strict; use warnings; use Constant::Generate [qw(FOO)], -stringy_vars => 1, -start_at => 12; if (FOO == 12 and FOO eq FOO) { print "FOO is a dualvar"; }
Re: User variables with two values?
by Kenosis (Priest) on Aug 19, 2012 at 21:49 UTC

    Unless I'm misunderstanding your question, you can do it like this:

    use Modern::Perl; my $a = '0001'; say 'The variable $a contains: ', $a; say 'The sum of $a and 1 is: ', $a + 1;

    Output:

    The variable $a contains: 0001 The sum of $a and 1 is: 2

    The perl interpreter uses context to determine how a variable should be treated: as a string in the first case, and as an integer in the second.

    Update: Yes, I did misunderstand. DamianConway provides an excellent solution below.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://988358]
Front-paged by Corion
help
Chatterbox?
[ambrus]: 1nickt: for some reason, these days they call every computer "gaming", even ones that gamers wouldn't buy. I've bought a keyboard that was labelled "gamer", despite that it has hard springs and seems to be way better for typing than for gaming;
[1nickt]: I though the gamers like that because they bash the keys so hard.
[ambrus]: and I've seen motherboards with no fast expansion ports for a video card but built-in hardware RAID advertized as "gaming".
LanX has a shaming laptop
[ambrus]: 1nickt: my impression is that the gamers like the softer springs, because fast reaction time is more important to them then feedback from keypresses to recognize typos.
[1nickt]: Ah, I see. I did read some gamer mag reviews, and yes, they lamented the fact that laptops with no discrete video card are sold as "gaming" hardware.
[1nickt]: But, they do have red keyboard backlighting! And gargoyles on the front, or words like "Maxxx" here and there. They know their demographic!
[1nickt]: True gamers don;t buy Dells, HPs, or Lenovos, I think ;-)
[ambrus]: The keyboard I bought doesn't have backlighting. I specifically had to select for that, because so many keyboards these days have lights. Why would I want lights in the keyboard? I want lights in the room, such as on the ceiling, not in the keyboard.
choroba has a small USB lamp

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (11)
As of 2017-03-27 13:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (320 votes). Check out past polls.