in reply to Re^2: user input behavior from within a subroutine
in thread user input behavior from within a subroutine

Hi again, bw,

    I have to refer to it during the rest of the script as main::@whatever

No, you're correct that you shouldn't have to do that.  Can you post or /msg me a very short example?  I'll be happy to show you how to fix it.

Here is the Perl documentation for use strict;.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
  • Comment on Re^3: user input behavior from within a subroutine

Replies are listed 'Best First'.
Re^4: user input behavior from within a subroutine
by bw (Novice) on Aug 03, 2006 at 13:51 UTC
    I see in my earlier posting that I referred to the fully-qualified variable improperly. I should have said @main::whatever, not main::@whatever. I think that I understand that @main::whatever qualifies it, assuming I'm declaring the value in the 'main' part of the program and not in a subroutine somewhere, for the rest of the program.
    If I use it within a subroutine (declaring it first in the 'main' portion of the program), am I correct to understand that I should pass it to the subroutine as subroutine_name(\@main::whatever)?

    In any case, here's the answer to your question to me--

    Doing:
    use strict; use warnings; @existingattributes = ();
    Produces the error:

    Global symbol "@existingattributes" requires explicit package name at C:\Perl\scripts\configurator.pl line 5.
    Execution of C:\Perl\scripts\configurator.pl aborted due to compilation errors.

    But perl is happy when I do this:
    use strict; use warnings; # initialize arrays @main::existingattributes = ();
      Hi bw,

      Very simply, because you need my in your declaration:

      use strict; use warnings; my @existingattributes = ();

      Then you will no longer get the error.

      That's what use strict does; it enforces your declaration of variables with my or our.  Using my lets you use it like a global variable (except that it's really a lexical variable), and it's visible only within the lexical scope in which it's declared.  If that's at the top of the program file, then the entire program can use it.

      However, it you need to share variables between files, you may need to use our instead.


      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        > because you need my in your declaration
        (insert sound of palm of right hand slapping my forehead here ...)

        So simple that even I can understand it!

        Thanks so much, liverpole