walto has asked for the wisdom of the Perl Monks concerning the following question:
I thought that if (defined $2) is just a better readable way than if ($2). But I found that gives different results:
but
Can an enlightend monk explain why?
gives : 50.00 as I would expect#!/usr/bin/perl -w # # use strict; my $price = "50,-"; $price =~ m/(\d{1,4}),(\d{0,2})/; if ($2 ) { $price = $1 . "." . $2; } else { $price = $1 . "." . "00"; } print "$price\n";
but
gives 50.if (defined $2 ) { $price = $1 . "." . $2; } else { $price = $1 . "." . "00"; }
Can an enlightend monk explain why?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: if ($2) behaves differently than if (defined $2)
by moritz (Cardinal) on Apr 15, 2008 at 17:41 UTC | |
Re: if ($2) behaves differently than if (defined $2)
by starbolin (Hermit) on Apr 15, 2008 at 17:42 UTC | |
by radiantmatrix (Parson) on Apr 15, 2008 at 18:09 UTC | |
by repellent (Priest) on Apr 16, 2008 at 16:57 UTC | |
Re: if ($2) behaves differently than if (defined $2)
by toolic (Bishop) on Apr 15, 2008 at 18:49 UTC |
Back to
Seekers of Perl Wisdom