On Mon, Nov 17, 2008 at 04:47, Richard Lee <[EMAIL PROTECTED]> wrote:
> say something like == or eq ..
>
> Can you sub them w/ varilable like $unknown ?
>
> Let me be more specific.
> Let's say I don't know what the variable will hold
>
> I guess I can say something like,
>
> sub check_unknown {
> my $unknown = shift; ## it's either digits or letters but both will
> be same kind
> my $unknown1 = shift; ## it's either digits or letters
>
> my $result = ( $unknown =~ /^\d+$/ ) ? '==' : 'eq';
> if ( $unknown $result $unknown1 ) {
> do something...
> }
> }
>
> But obviously above dont work.. can someone shed some light on this?
>
> thanks!
Well, you could say something like this
#!/usr/bin/perl
use strict;
use warnings;
use Scalar::Util qw/looks_like_number/;
sub compare {
my ($x, $y) = @_;
my $compare = looks_like_number($x) ? sub { shift == shift } : sub {
shift eq shift };
return $compare->($x, $y);
}
print join(" :: ",
compare("x", "y"),
compare("x", "x"),
compare("5", "6"),
compare("5", "5"),
compare("0", "x"),
), "\n";
but it would only be worthwhile if you used $compare a lot after
setting it once; otherwise it would be simpler to just say
sub compare {
my ($x, $y) = @_;
return looks_like_number($x) ? $x == $y : $x eq $y;
}
--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/