2008-12-11 11 views

उत्तर

10

मैच ऑपरेटर चूक लेकिन जब तक यह कुछ समय पाश में प्रयोग किया जाता है तो $_ में कुछ भी नहीं संग्रहीत किया जा रहा है <> ऑपरेटर डिफ़ॉल्ट रूप से $_ में दुकान नहीं है।

perldoc perlop से:

 
    I/O Operators 
    ... 

    Ordinarily you must assign the returned value to a variable, but there 
    is one situation where an automatic assignment happens. If and only if 
    the input symbol is the only thing inside the conditional of a "while" 
    statement (even if disguised as a "for(;;)" loop), the value is auto‐ 
    matically assigned to the global variable $_, destroying whatever was 
    there previously. (This may seem like an odd thing to you, but you’ll 
    use the construct in almost every Perl script you write.) The $_ vari‐ 
    able is not implicitly localized. You’ll have to put a "local $_;" 
    before the loop if you want that to happen. 

    The following lines are equivalent: 

     while (defined($_ =)) { print; } 
     while ($_ =) { print; } 
     while() { print; } 
     for (;;) { print; } 
     print while defined($_ =); 
     print while ($_ =); 
     print while ; 

    This also behaves similarly, but avoids $_ : 

     while (my $line =) { print $line } 
+0

वास्तव में:

$_ = <> until /include/; 

चेतावनी से बचने के लिए? मुझे पता नहीं था। धन्यवाद – user44511

+0

मुझे न तो। <> थोड़ी देर में व्यवहार करते समय अलग-अलग व्यवहार क्यों करता है? – Kip

+0

यह सिर्फ एक शॉर्टकट है ताकि आप "जबकि (परिभाषित ($ _ = <>)) {...}" के बजाय "जबकि (<>) {...}" कह सकें। –

4

<> एक while(<>) निर्माण में केवल जादू है। अन्यथा यह $_ को असाइन नहीं करता है, इसलिए /include/ नियमित अभिव्यक्ति के खिलाफ मिलान करने के लिए कुछ भी नहीं है। यदि आप -w के साथ इस भाग गया पर्ल आपको बता जाएगा:

Use of uninitialized value in pattern match (m//) at .... 

आप के साथ इसे ठीक कर सकते हैं:

while(<>) 
{ 
    last if /include/; 
} 
संबंधित मुद्दे

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