Wednesday, March 12, 2014

Compare values - examples

Perl: Compare values - examples


Expression Value
"12.0" == 12 TRUE
"12.0" eq 12 FALSE
2 < 3 TRUE
2 lt 3 TRUE
12 > 3 TRUE
12 gt 3 FALSE !
"foo" == "" TRUE ! (Warning)
"foo" eq "" FALSE
"foo" == "bar" TRUE ! (Warning)
"foo" eq "bar" FALSE

When reading from STDIN you can always expect a string

examples/scalars/is_empty_string.pl
 
#!/usr/bin/perl
use strict;
use warnings;

my $input = <STDIN>;
chomp $input;
 
if ($input == "") {     # wrong! use eq
    # empty string 
 
 
--Thanks: Edu Maven 

No comments: