2009-10-29 7 views
9

perl 5.8.5 में, यदि मैं निम्नलिखित करता हूं, तो मुझे कोई त्रुटि नहीं मिलती:एक अपरिफ मान पर्ल में मान्य सरणी संदर्भ क्यों बनता है?

use strict; 

my $a = undef; 
foreach my $el (@$a) { 
    ...whatever 
} 

यहां क्या हो रहा है? Xzx19 के आउटपुट को प्रिंट करने से पता चलता है कि $a किसी बिंदु पर मान्य सरणी संदर्भ बनने के लिए बदल जाता है। लेकिन मैंने स्पष्ट रूप से $a को कुछ भी सेट नहीं किया है।

ऐसा लगता है कि एक चर की सामग्री कुछ भी करने के बिना बदल सकती है।

विचार, कोई भी?

संपादित करें: हाँ, मैं ऑटो-विविफिकेशन के बारे में सब कुछ जानता हूं। मैंने हमेशा सोचा था कि इसे ट्रिगर करने के तरीके के साथ कहीं भी असाइनमेंट होना था, न केवल एक संदर्भ।

+3

यह perl है। विषमता ठीक है, क्योंकि यह अजीब है। –

+3

मुझे नहीं लगता कि यह अजीब है, कुछ घोषणाओं को बचाने के लिए बस एक सुविधा है। –

+3

@ स्टेफानो: पर्ल प्रश्नों को केवल ट्रॉल करने के लिए क्यों परेशान करते हैं? यह कठिन है ... – Telemachus

उत्तर

11

पढ़ें Uri Guttman's article on autovivification निष्क्रिय किया जा सकता।

इसके बारे में कुछ भी पता नहीं है जब आप इसके बारे में जानते हैं और बहुत अजीबता बचाता है।

Perl first evaluates a dereference expression and sees that the current reference value is undefined. It notes the type of dereference (scalar, array or hash) and allocates an anonymous reference of that type. Perl then stores that new reference value where the undefined value was stored. Then the dereference operation in progress is continued. If you do a nested dereference expression, then each level from top to bottom can cause its own autovivication.

+0

अनुवर्ती प्रश्न। यह ऑटो-विविफ क्यों नहीं करता है: 'मेरा $ ए; मेरा @ एक्स = @ $ ए'? – FMc

+4

@ एफएम: सामान्य रूप से, जो चीजें केवल एक dereference से पढ़ रहे हैं, वे स्वत: सक्रिय नहीं होंगे, जबकि चीजें जो संशोधित की गई हैं को संशोधित कर सकती हैं। फॉर लूप कुछ ऐसा उदाहरण है जो संशोधित कर सकता है (चूंकि $ el सरणी में अलियाकृत है और सरणी इसके माध्यम से बदला जा सकता है)। यह केवल एक ढीला नियम है, हालांकि; कुछ मोटे किनारों हैं। – ysth

+0

एक सबस्क्रिप्ट के साथ मामलों से सावधान रहें, यहां तक ​​कि जब उनसे पढ़ना भी हो, तो स्वाभाविकता संभवतः हो जाएगी –

15

Auto-vivification शब्द है। लिंक से:

Autovivification is a distinguishing feature of the Perl programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. In other words, Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.

In contrast, other programming languages either: 1) require a programmer to expressly declare an entire variable structure before using or referring to any part of it; or 2) require a programmer to declare a part of a variable structure before referring to any part of it; or 3) create an assignment to a part of a variable before referring, assigning to or composing an expression that refers to any part of it.

Perl autovivication can be contrasted against languages such as Python, PHP, Ruby, JavaScript and all the C style languages.

ऑटो vivification साथ no autovivification;

+0

मैंने इस पृष्ठ को ताज़ा करने के लिए उत्तर दिया क्योंकि मैं उत्सुक था। वाह, पर्ल वास्तव में अजीब है :) – Bartek

+2

यह मेरे लिए प्राकृतिक लगता है। –

+0

यह डीएलआईएम का पर्ल का गुण है (मेरा मतलब है)। – spoulson

संबंधित मुद्दे