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

MCE seemingly stray array in sample code for Strassen's algorithm

by cormanaz (Chaplain)
on Apr 14, 2013 at 00:11 UTC ( #1028572=perlquestion: print w/ replies, xml ) Need Help??
cormanaz has asked for the wisdom of the Perl Monks concerning the following question:

Good day bros. I am trying to implement some sample code in mod MCE for implementing Strassen's algorithm for matrix multiplication. At the beginning of the subroutines is the following:
###################################################################### +######### # * # * # * # * # * # * # * # * # * # * # * # * # * # * # * # * # * # + * # * # ###################################################################### +######### my @p; sub store_result { my ($n, $result) = @_; $p[$n] = $result; return; } sub configure_and_spawn_mce { return MCE->new( max_workers => 7, user_func => sub { my $self = $_[0]; my $data = $self->{user_data}; my $tam = $data->[3]; my $result = [ ]; strassen_r($data->[0], $data->[1], $result, $tam); $self->do('store_result', $data->[2], $result); } )->spawn; }
My question is about the my @p. This occurs after the completion of the main script, yet it is declared as a local variable so it is within the scope of the main script. It is referenced in sub store_result but is outside its scope. My debugger (Komodo) doesn't flag this as a problem, which it seems like it should. Does anyone know what's going on here? I have not used MCE so maybe it's something peculiar about it?

Comment on MCE seemingly stray array in sample code for Strassen's algorithm
Select or Download Code
Re: MCE seemingly stray array in sample code for Strassen's algorithm
by kcott (Abbot) on Apr 14, 2013 at 01:38 UTC

    G'day cormanaz,

    @p is a global variable visible to all the subroutines (i.e. in scope). It's used in strassen() as well as store_result() (as you pointed out).

    Here's a simplified example of what you're seeing. Both subroutines have access to the global array @nums:

    $ perl -Mstrict -Mwarnings -E ' say "Hello, world!"; populate_nums(); report_nums(); my @nums; sub populate_nums { push @nums => 0 .. 9 } sub report_nums { say "@nums" } ' Hello, world! 0 1 2 3 4 5 6 7 8 9

    Here, only populate_nums() has access:

    $ perl -Mstrict -Mwarnings -E ' say "Hello, world!"; populate_nums(); report_nums(); my @nums; sub populate_nums { push @nums => 0 .. 9 } sub report_nums { my @nums; say "@nums" } ' Hello, world!

    Perhaps take a look at perlintro - Variable scoping; and follow-up with perlsub - Private Variables via my().

    -- Ken

      Your use of the word "global" is truly discriptive, but I believe that perl documentation reserves it for package variables. A possible alternative is "file scope variable".

      Example: The description of "our" in perlfunc contains the following quote.

      our associates a simple name with a package (read: global) variable in the current package,...
      Bill
        "File scope" is also potentially incorrect - if the code has several nested blocks in the file, only those declared in the outer-most block could be considered to be "File scope".

        The correct terminology is "lexical scope".

        From "perldoc -f my" :
        .... local (lexically) to the enclosing block, file, or "eval".

        In this case, the subroutine is accessing a variable declared in an enclosing block.
        I suppose this could be considered "global to the subroutine".

                     "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
                -- Dr. Cox, Scrubs

      Well blow me down. I have thought all these years that a variable declared "my" in the main block was only accessible in the main block and not in its subs, and that to make it accessible you had to declare it "our." I guess not!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1028572]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-09-16 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (47 votes), past polls