Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^7: Moo and Spreadsheet::ParseExcel

by tobyink (Abbot)
on Nov 09, 2012 at 13:32 UTC ( #1003125=note: print w/replies, xml ) Need Help??


in reply to Re^6: Moo and Spreadsheet::ParseExcel
in thread Moo and Spreadsheet::ParseExcel

my $_ should work, but local $_ would fail.

It's because the loop variable in a for loop is an alias to each element of the array. So the global $_ is an alias into @roles in this case. Assigning to global $_ assigns into the @roles array. local (despite the name) operates on global variables, providing a local value for them, but not really creating a new local variable. So even in the scope of local $_ assigning to $_ will assign into @roles.

It's a particular combination of Perl features which is normally fairly safe to use, but because apply_roles_to_package also happens to load modules (and thus run third-party code - i.e. code which is not part of Moo) it causes problems.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^8: Moo and Spreadsheet::ParseExcel (explain?)
by tye (Sage) on Nov 17, 2012 at 19:29 UTC

    Could you try explaining this again? In particular, what is the code outside of Moo that causes $_ to exhibit this problem? It probably doesn't help that I'm not clear on how 'undef' ends up in @roles (is the code that does that shown anywhere in this thread?). I'm also unclear how the 'uninitialized' warning causes the Role::Tiny module to fail (leading to a "Compilation failed") without producing an error message.

    Thank you (in advance).

    - tye        

      "Could you try explaining this again? In particular, what is the code outside of Moo that causes $_ to exhibit this problem? It probably doesn't help that I'm not clear on how 'undef' ends up in @roles (is the code that does that shown anywhere in this thread?)."

      Moo::Role loops through the list of roles with a for loop, using the default loop variable, global $_. (Or rather it did, as this is now fixed.)

      In for loops, the loop variable is an alias into the array. If you assign to it, the original array is altered. Thus within that loop, if $_ were assigned to, it would alter the list of roles.

      Now, what happens within the loop? The role modules get required. And thus any modules that the role modules use get loaded too. And in all that code that gets loaded, somewhere, undef gets assigned to global $_. (And no, I don't know exactly where... Spreadsheet::WriteExcel has a number of dependencies.)

      That's how undef gets into @roles.

      "I'm also unclear how the 'uninitialized' warning causes the Role::Tiny module to fail (leading to a "Compilation failed") without producing an error message."

      Role::Tiny has:

      use warnings FATAL => 'all';
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

        Ah. Thanks. What I was missing was that there was an unspecified outer layer that was calling Moo::Role, not just the inner layer that also used Moo::Role (as well as Spreadsheet::ParseExcel).

        So it was a standard mistake of calling out to arbitrary code while using $_. And there is a spot somewhere in Spreadsheet::ParseExcel that it would be good to add local $_ to.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003125]
help
Chatterbox?
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)
[LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2017-03-29 12:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.