asthaonard has asked for the wisdom of the Perl Monks concerning the following question:

Hello

I'm very new to perl - just starting :) And so rather theoretical question

How could i force perl to treat variable as number or text?

Very short example:

my $a = '94'; my $b = '98'; my $c = $a ^ $b; print "$c\n"; my $x = 94; my $y = 98; my $z = $a ^ $b; print "$z\n"; my $i = 94 ^ 98; print "$i\n";

and output is:

♀ ♀ 60

Replies are listed 'Best First'.
Re: variable treatment
by oxone (Friar) on Jun 08, 2011 at 05:59 UTC
    Good question! See Bitwise String Operators, which says "You may explicitly show which type of operation you intend by using "" or 0+". So this:
    use strict; use warnings; my $a = '94'; my $b = '98'; my $c = 0+$a ^ 0+$b; print "$c\n"; my $x = 94; my $y = 98; my $z = 0+$a ^ 0+$b; print "$z\n"; my $i = 94 ^ 98; print "$i\n";
    Gives:
    60 60 60
      Thank you. I hadn't thought it's so easy.
Re: variable treatment
by choroba (Cardinal) on Jun 08, 2011 at 07:48 UTC
    On line 7, you probably meant $z = $x ^ $y.