Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

replace deprecated function

by mitchreward (Acolyte)
on Jun 23, 2014 at 13:18 UTC ( [id://1090910]=perlquestion: print w/replies, xml ) Need Help??

mitchreward has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I use the following code with a perl v5.14.2, and it tells me that defined function on hash is deprecated. how can I fix this test easily ?

if (!$Config{'recipient'}) {^M if (!defined(%Form)) { &error('bad_referer') }^M else { &error('no_recipient') }^M }^M else {^M ...

Replies are listed 'Best First'.
Re: replace deprecated function
by Athanasius (Archbishop) on Jun 23, 2014 at 13:23 UTC

    From defined:

    Use of defined on aggregates (hashes and arrays) is deprecated. It used to report whether memory for that aggregate had ever been allocated. This behavior may disappear in future versions of Perl. You should instead use a simple test for size:

    if (@an_array) { print "has array elements\n" } if (%a_hash) { print "has hash members\n" }

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: replace deprecated function
by choroba (Cardinal) on Jun 23, 2014 at 13:24 UTC
    diagnostics would tell you:
    $ perl -E 'defined(%h)' 2>&1 | splain defined(%hash) is deprecated at -e line 1 (#1) (D deprecated) defined() is not usually useful on hashes because i +t checks for an undefined scalar value. If you want to see if the h +ash is empty, just use if (%hash) { # not empty } for example. (Maybe you should just omit the defined()?)
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: replace deprecated function
by wjw (Priest) on Jun 23, 2014 at 13:24 UTC

    This had me going for a bit too until I found the following in ... of all places... perldoc!

    Use of defined on aggregates (hashes and arrays) is deprecated. It used to report whether memory for that aggregate had ever been allocated. This behavior may disappear in future versions of Perl. You should instead use a simple test for size: if (@an_array) { print "has array elements\n" } if (%a_hash) { print "has hash members\n" }

    Hope that is helpful...

    ...the majority is always wrong, and always the last to know about it...

    Insanity: Doing the same thing over and over again and expecting different results...

    A solution is nothing more than a clearly stated problem...otherwise, the problem is not a problem, it is a facct

      Thanks you guys!
Re: replace deprecated function
by AnomalousMonk (Archbishop) on Jun 23, 2014 at 17:04 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1090910]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2025-07-10 16:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.