Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Why will referencing an undefined value as array reference create a real array

by mfort (Initiate)
on Jun 18, 2014 at 18:45 UTC ( #1090329=perlquestion: print w/ replies, xml ) Need Help??
mfort has asked for the wisdom of the Perl Monks concerning the following question:

Script:
#!/usr/bin/perl print "test=$test\n"; print "this won't be printed\n" if (scalar @{$test}[0]); print "test=$test\n";
Output:
test= test=ARRAY(0x7fcc8c004ee8)
In a larger script, $test will either be undef or will be a reference to an array of data, where the first element is an array reference. I would have expected 'scalar' to examine only, not create an anonymous array. So what is the logic employed by perl?

Comment on Why will referencing an undefined value as array reference create a real array
Select or Download Code
Re: Why will referencing an undefined value as array reference create a real array
by jeffa (Chancellor) on Jun 18, 2014 at 18:54 UTC

    That's a feature called auto-vivification. Consider the following alternative:

    print "this won't be printed\n" if (scalar @{$test} and scalar @{$test +}[0]);


    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Thanks for pointing me to the Auto Vivification. I swear I learn something new every day with perl and I've been using it for decades now.
Re: Why will referencing an undefined value as array reference create a real array
by Anonymous Monk on Jun 18, 2014 at 18:57 UTC
Re: Why will referencing an undefined value as array reference create a real array
by LanX (Canon) on Jun 19, 2014 at 02:55 UTC
    > ... or will be a reference to an array of data, where the first element is an array reference.

    Why don't you just use ref for your check?

    Cheers Rolf

    (addicted to the Perl Programming Language)

Re: Why will referencing an undefined value as array reference create a real array
by sundialsvc4 (Abbot) on Jun 19, 2014 at 13:44 UTC

    That was a conscious design-decision by the language designers ... and a very good one, indeed.   When you refer to an as-yet undefined variable in a particular context, it will automagically become a reference to an empty instance of whatever is appropriate to that context ... “auto-vivification,” as previously stated.   An incredibly useful feature of the language for pragmatic script-writing.

Re: Why will referencing an undefined value as array reference create a real array
by ikegami (Pope) on Jul 29, 2014 at 14:43 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-18 04:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (41 votes), past polls