Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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?

Comment on User variables with two values?
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 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.

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 Marshall (Prior) 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"; }

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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-30 14:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls