Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi, the scope of variables has been a problem for me as I'm writing my casual scripts. For some reasons I have been having trouble accessing some of my variables (declared by "my")although they should be inside the block as I do see those. Maybe some of you can show me what is wrong. I guess I am having some trouble understanding what a block is that could contain a declaration. For example this script:

#!/usr/bin/perl -w use strict; use warnings; while (my $line=<>) { my @part=split(/\t/,$line); my @saba=split(/\|/,$part[2]); for(my $i=0;$i<@saba;++$i) { if ($saba[$i]!~m/unclassified/) {(if $i==$saba){print "$_\n";} else {print "$_|";} } else {if ($i==$saba){print "unclassified_$saba[$i-1]\n";} else {print "unclassified_$saba[$i-1]|";} } } }
will post errors
syntax error at ./claskri.pl line 13, near "(if" Global symbol "$saba" requires explicit package name at ./claskri.pl l +ine 13. syntax error at ./claskri.pl line 16, near "else" Global symbol "$i" requires explicit package name at ./claskri.pl line + 17. Global symbol "$saba" requires explicit package name at ./claskri.pl l +ine 17. syntax error at ./claskri.pl line 18, near "else" Execution of ./claskri.pl aborted due to compilation errors.
I do not understand as I have declared my @saba already in encasing brackets. Should a loop inside those understand that I am trying to access the variable a "a level" up so to speak. What I want is for all those loops to have access to variable @saba (or $saba in scalar context). Right now it seems perl looks the innermost loops and checks that the saba is not declared locally within these (by not having "my" infornt them) - then demands it to be declared globally. What would be seem more normal for me would it to check one loop outside to check if it is declared in this - a sort of inheritance so to speak. I do not dare do declare it inside these innermost loops as then it would probably remain empty == not assosiated with the actual saba form outer loop. And I do not want to declare it globally - as to eventually avoid colliding variable names.

In reply to scoping problems by naturalsciences

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (14)
    As of 2015-07-06 12:19 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (74 votes), past polls