[Israel.pm] FW: newbie question

sawyer x xsawyerx at gmail.com
Thu Jan 15 05:39:12 PST 2009

(Avishalom already wrote a reply, but I was near the end, so I'm posting anyway)


When you do: if ($something)
it checks whether $something is anything EXCEPT the number zero, an
empty string or undefined value. So, for instance, if $something is
zero, the if() statement will not work. This can be confusing
sometime, but it's very useful when you have subroutines. You can do:
sub check_ok {
    my $to_check = shift;
    if ( $to_check eq 'good' ) {
        return 1;
    return 0;

if ( check_ok('bad') ) {

- You can say that if I omit the "return 0", it will return "undef",
which would work for my case too, and that's true, but it's good
practice to return every statement, so it's worthwhile doing. Try the
use strict;
use warnings;

my $name;
while ( $name ne 'stop' ) {
    print "Please enter your name: ";
    chomp ( $name = <STDIN> );

The while will result in an uninitialized error at starting point
because $name was not initialized. If you would do:
my $name = '';
it wouldn't cause that problem.

However, if you want to test whether something really is defined, you
use: defined $scalar.
That helps avoid the problems with zeros and empty strings.

Another difference (other than what Avishalom noted) when writing:
my @arr = ();
my @arr;
is that in the second one you do not initialize it, but only declare
it. in the first one, you're declaring it, just making it an empty

Sometimes perldoc is not very understanable and it assumes you know
basic programming. I would suggest checking out a few tutorials and
some books or even ebooks.

On Thu, Jan 15, 2009 at 3:22 PM, Chanan Berler <chananb at centerity.com> wrote:
> Hi All,
> I am wondering if there is a difference between:
> The last one I believe I know the answer, empty string still defined, but I
> sure wanna clear view..:-)
> PS: will use strict / warning will make a difference ?
> do_somthing if ($legend);
> or
> do_something if (defined($legend));
> or
> do_something if ($legend == "");
> another question:
> is there a difference between
> @arr = ();
> And
> @arr;
> Since both of them showed nothing when trying to do this:
> print "Yea" if (@arr);
> thanks
> Chanan
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl

More information about the Perl mailing list