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

Thank you liverpole. I will indeed take this approach. Regarding strict and warnings, I do use the latter. I've tried the former but don't understand it very well. As a result, I end up using it very badly.

I'm having a tough time getting my mind around what 'use strict' does (though I'm busily poring through various FAQs I can find to sort it out). Any good/understandable-for-the-newbie links you happen to know on the subject will be most appreciated!

As a "for instance" regarding my lack of 'strict', whenever I use an array where I've specified 'strict', I have to refer to it during the rest of the script as main::@whatever. I'm sure that's not the right way to approach this, but I haven't found a better way to do this.
  • Comment on Re^2: user input behavior from within a subroutine

Replies are listed 'Best First'.
Re^3: user input behavior from within a subroutine
by liverpole (Monsignor) on Aug 02, 2006 at 21:26 UTC
    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$..$/
      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$..$/