Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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 ./ line 13, near "(if" Global symbol "$saba" requires explicit package name at ./ l +ine 13. syntax error at ./ line 16, near "else" Global symbol "$i" requires explicit package name at ./ line + 17. Global symbol "$saba" requires explicit package name at ./ l +ine 17. syntax error at ./ line 18, near "else" Execution of ./ 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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (3)
    As of 2015-11-27 20:47 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (732 votes), past polls