Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: Re: Z80 Assembler -> Hex Opcode

by Elgon (Curate)
on Feb 09, 2003 at 12:52 UTC ( #233888=note: print w/replies, xml ) Need Help??

in reply to Re: Z80 Assembler -> Hex Opcode
in thread Z80 Assembler -> Hex Opcode

Hi Tachyon,

I did indeed realise this, however in the code as you have written it the declaration of %opcodes is in a different scope to that of the subroutine and hence, all you get returned is an error. I am not sure exactly how to access the hash from the subroutine's scope - %main::opcodes ???

I did also originally run the program without the encode routines as a separate sub and it didn't appear to run any faster.



Update: I tried running the program as a simple loop, without the sub as before, and it took about 1 second instead of 36 or so to run. Must have declared the hash inside the loop the first time. Oops. Time to go back to the Blue Camel and re-read the sections on scoping and packages :-)

Update^2: I've been trying to do what you suggest by passing a reference to %opcodes to the subroutine along with the line and then saying local *hash = shift; a la blue camel page 294 but this isn't liked either. FYI I'm using Perl 5.005 so use of our is right out.

Update^3: See root node.

"What this book tells me is that goose-stepping morons, such as yourself, should read books instead of burning them."
       - Dr. Jones Snr, Indiana Jones and the Last Crusade

Replies are listed 'Best First'.
Re: Re: Re: Z80 Assembler -> Hex Opcode
by tachyon (Chancellor) on Feb 09, 2003 at 14:18 UTC

    With the example I posted %opcodes is in main::, so too the sub is in main:: - ergo and therefore same scope, no errors, runs as posted. You don't need the use vars *and* the my declaration for %opcodes. One or the other will pacify strict...

    # declare this in implicit main:: package my %here = ( in => ' main' ); run(); sub run { # we are still in main:: here, so we can access %hash just fine print "I am still ", %here }




      Ah, found the problem,

      a) poor understanding of scoping on my part and b) when I first ran as you described, I had the declaration of %opcodes after the main loop containing the call to encode() so, in effect, the declaration never took place until after the program had effectively run its course.

      You live and learn.


      "What this book tells me is that goose-stepping morons, such as yourself, should read books instead of burning them."
             - Dr. Jones Snr, Indiana Jones and the Last Crusade

        Declaring hash structures within subs that get repeatedly called is a common problem. Another solution that allows the hash to be declared on demand (if the sub may or may not be called this is good) but still only be called once is to use a closure like this:

        { my %hash; sub my_sub { unless (%hash) { %hash = ( blah ); } } } # %hash is not accessible here, this is a different one my %hash...

        Doing it this way makes %hash private to the sub as the my delaration localises it to the bare block where it is accessible to the sub but not main:: per se. You start the sub by checking if the hash is populated. On the first call to the sub it will not be so you set it. From then on it will populated so you don't need to set it again during subsequent calls to the sub. Perl retains a reference to it between calls to the sub so it never gets garbage collected.




Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://233888]
[Corion]: LanX: Maybe have an infinite-loop cmd file? Much easier than trying to manage that from within Perl IMO
[Corion]: Alternatively, relaunch the application from cron (or a Windows cron) every minute
[LanX]: will try infinite loop ...
[Corion]: :START ... goto START
[LanX]: oh .bat you mean?
[LanX]: obscure cult ...
[Corion]: LanX: cmd.exe is just another way to launch Perl
[LanX]: I know its a cult... and I repent it every time
[LanX]: But cygwin comes with path problems and powershell requires too much learning for me
[LanX]: eshell in emacs is often my resort in development

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-03-27 15:56 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (320 votes). Check out past polls.