Loading ...
Sorry, an error occurred while loading the content.

Re: [PBML] Small Scoping question.

Expand Messages
  • Charles K. Clarkson
    Franki ... Sprite programming was neat. My first was a business BASIC program simulating a drunkard s walk. I just used N, S, E, and
    Message 1 of 5 , Jan 2, 2002
    • 0 Attachment
      "Franki" <frankieh@...>

      : I remember writing a space invaders clone for my Commodore64 with
      : sprites sound the whole thing, 10,000 lines of basic..
      :
      : I was very impressed with myself back then, then my father made me go and
      : study engineering at uni, and that was the end of my stint with programming,
      : that was over 15 years ago, and I only got back into it last year. (which is
      : why your combining of instructions into one, (as you showed me last week)
      : mostly went over my head at first, Basic doesn't really cater to that sort
      : of thing, (hence it being a training language.)

      Sprite programming was neat. My first was a business BASIC program
      simulating a drunkard's walk. I just used N, S, E, and W. I printed 10,000
      steps taken by a drunk (random steps) and compared ten results to the
      statistical formula. I think it was: sqrt(2N-1). My teacher was so amazed
      I got an A for the course. That was about 25 years ago.

      : started learning C++, Perl, JAVA, unix sys admin and MySQL all at the
      : same time, taught myself html in a weekend and the basics of Javascript
      : in a week or so.. but the others are slower progress, but I think now I
      : should have started with C++ or JAVA first and moved to the others
      : later, the much stricter styles of C++ and JAVA would have trained me
      : to have better habits, perl lets you get away with to much, which is both
      : a good and bad thing I think.

      I started with perl last March or April, after a few years of playing
      with VB. I spend most of my time waiting for the phone to ring, so I
      sit in front of my computer and write programs between seessions of
      Civilization, Alpha Centauri, and Railroad Tycoon.

      : Getting back to my actual question, I have a script that requires a file at
      : the start..
      :
      : that script has many seperate component .pl files which all get required and
      : used at different times.
      :
      : but the config file gets called by all of them at some stage or another (it
      : contains all the paths and values of everything the script needs.) all of
      : the variables in that are currently not strictures.. (ie not declared..) I
      : thought that if I declared them with my, then they wouldn't be available to
      : the rest of that script.

      You're right. I did a quick test and as soon as you add 'my' strict
      balks. This question comes up a lot. I've seen similar ones at least
      once a month on this list. I came up with various answers, but they
      all seem flawed.
      The only thing that seems to fix everything is an object. You'd
      have to create an module that allows any global variable to be
      retrieved. Like the param function in CGI.pm:

      my $query = new CGI;
      print $query->param('file');

      I thought more of something along the line of a tied hash. It would
      look and act like a hash, except it would be read-only and it would
      allow programmers to add new variables. Unfortunately, I haven't
      learned to tie hashes yet.

      : I can't just test it, because the script in its entirely is huge (about
      : 5000-8000 lines or more spread over 15 files.)
      : and turning on scriptures and/or warnings gives me a list of
      : errors a mile long. (it does work in taint mode though.)
      :
      : I guess I will start going thought the script, section by section
      : and do it slowly.. I'll get there eventually I guess.

      I am doing something similar on a "Matt" script. I started with the
      subroutines.

      : One thing I thought I'd add though, if I get it working on strictures
      : and warnings with no errors,, (won't happen soon, but I can hope :-)
      : should I then turn warnings and strict off? is there any overhead
      : with leaving strict and warnings on once the script passes muster?

      I think there is, but it's very small compared to loading the perl
      interpreter each time a script is called. Couldn't hurt to axe them
      on the production code though, every little bit helps.

      : I want to make the memory footprint of this thing as small as possible
      : and get the script as efficient as possible so that one day I can modify
      : it to work with mod_perl. (keep it in memory on the server at all times,
      : change the file writes to some sort of ram cache, and back it to file
      : AFTER each use to get it as fast as possible.. or maybe a mysql
      : database.. haven't worked out what is best yet.. got some testing to do..
      :
      : anyway, I have blathered on enough for one night, thanks for your
      : help once again..
      :
      : When work slows down abit, I am going to make them pay for me
      : to get a degreee in computer science/programming. since they are the
      : reason I am learning all this anyway..
      :
      : How hard is Assembly to learn?? I have always had an interest,
      : (Steve Gibson, of grc.com always writes his apps in Assembly and
      : they all seem so tiny compared to C or C++)

      After C, it would be a lot easier. It took me the longest time to
      get a handle on memory models. I learned on a Color Computer
      and later used MASM 6.0 (microsoft). Ask Steve for advice on
      which tools to get and ask him if he uses a library. Reusing
      assembler code really speeds things up. Oh, and don't take
      vacations. You'll forget everything and have to start all over. :)


      HTH,
      Charles K. Clarkson
      Clarkson Energy Homes, Inc.
      254 968-8328


      The generation of random numbers is too important to be left to chance.
    • Charles K. Clarkson
      Franki ... Think hashes and references. my $global = get_var_from_file ($file_name); sub get_var_from_file { # do some fancy stuff
      Message 2 of 5 , Jan 2, 2002
      • 0 Attachment
        "Franki" <frankieh@...>

        : Just thougth of a related question..
        :
        : What do you do, if you have a sub that opens a file and assigns
        : a value from that file to a variable..(purely hyperthetical, I could
        : think of a dozen examples of what I mean that would have the
        : same problem...)
        :
        : and that variable needs to be available to other subs and stuff
        : throughout the script.
        :
        : if you used "my" in that scenario, would not the variable go
        : "out of scope" once that sub had run and its value not be
        : available to anything outside it?
        :
        : If I am right, how could you make it available outside the sub?
        : Do you have to use "return" to make it available? is that how
        : it works?
        :
        : IF that is correct, does that mean that the 100% correct and
        : proper way to do it would be to pass all needed variables to
        : the sub as parameters, and return the ones you want available
        : to other areas of the script?
        :
        : (I am just trying to improve my habbits now, before getting
        : to used to doing it the way I have been.)

        Think hashes and references.

        my $global = get_var_from_file ($file_name);

        sub get_var_from_file {
        # do some fancy stuff getting a hundred variables from
        # $file_name and place them in a hash %globals
        return \%global;
        }

        You can dereference the hash with:

        $$global{base}

        This way you are only returning a single scalar (the reference)
        from the sub. If you undef the reference or it goes out of scope
        the hash goes away too. Assuming nothing else is pointing to it.
        Calling it global helps most anyone realize where it came from.

        If you need to pass a lot of variables into a sub, consider
        using a reference:

        my $cool_members = cool(\@members);

        my $cool_members = cool( [@alumni, @pledges, @members ] );



        HTH,
        Charles K. Clarkson
        Clarkson Energy Homes, Inc.
        254 968-8328



        Is there another word for synonym?
      Your message has been successfully submitted and would be delivered to recipients shortly.