Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

using the define option

by Anonymous Monk
on Aug 22, 2001 at 13:10 UTC ( #106897=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am using this code : if (defined @array ) { print "Array is found"; } but it doesn't work, something is wrong and it doesn't print my message. what is wrong with it, thanks.


Edit Masem 2001-08-22 - Removed offensive sig line

Replies are listed 'Best First'.
Re: using the define option
by azatoth (Curate) on Aug 22, 2001 at 13:13 UTC

    perldoc -f defined.

    Quoth the Perldoc : "...You may also use defined() to check whether a subroutine exists. On the other hand, use of defined() upon aggregates (hashes and arrays) is not guaranteed to produce intuitive results, and should probably be avoided."

    "This counterintuitive behaviour of defined() on aggregates may be changed, fixed, or broken in a future release of Perl." Unquoth the Perldoc.

    This is why you're having problems. Also, remove that .sig. It's offensive.

    Azatoth a.k.a Captain Whiplash

    Make Your Die Messages Full of Wisdom!
    Get YOUR PerlMonks Stagename here!
    Want to speak like a Londoner?
Re: using the define option
by dmmiller2k (Chaplain) on Aug 22, 2001 at 16:29 UTC

    Arrays cannot be tested for defined(), only scalars can.

    Arrays can be tested for emptiness, as:

    # the scalar of an array is its number of elements if ( scalar(@array) ) { ... } # or equivalently, and shorter # exactly the same thing, since 'if ()' forces scalar context if ( @array ) { ... }


    Just call me the Anti-Gates ...
Re: using the define option
by Cine (Friar) on Aug 22, 2001 at 13:28 UTC
    defined @array is deprecated, just use if (@array) instead

    T I M T O W T D I

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://106897]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-02-24 10:29 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (310 votes). Check out past polls.