Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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?
[vedagiri89]: CentOS Linux release 7.0.1406 (Core)
[hippo]: How did you install libapreq2?
[vedagiri89]: that one is done by system team from the tar i guess
[hippo]: Really? Not using the one from the CentOS repository? Perhaps that's the problem.
[vedagiri89]: i need when this issue will come usually
[hippo]: How about mod_perl? Was that installed from the CentOS packge or from source?
[vedagiri89]: from the source
[hippo]: But APR.pm is under vendor_perl in your error message so you are mixing up modules installed from the packager and by hand. That's rarely a good plan.
[vedagiri89]: the prm is..works fine if i run file directly..not working if i run through apache http file

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-06-19 11:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (113 votes). Check out past polls.

    Notices?