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

Re: [PBML] Passing variable to second file (cross posted to 'beginners@perl.org

Expand Messages
  • Mike Southern
    General answer to match a general question: You can pass data from one file to another by writing subroutines that process the passed data and return a result.
    Message 1 of 4 , May 3, 2005
    • 0 Attachment
      General answer to match a general question: You can pass data from one file
      to another by writing subroutines that process the passed data and return a
      result.

      That then should draw your attention to program design. It may not be a good
      enough answer to segregate code purely because of manageability. That in
      itself may give you a hint that you will be heading for madness, ultimately
      having TWO unmanageable files instead of one.

      It may be a better approach to segregate your code according to (pardon if I
      teach granny to suck eggs) function and operation. Examine any repeated
      operations to see if they qualify to be written as subroutines. Define areas
      of static/constant data. 1400 lines is not the issue - is it 1400 lines of
      50% repeated data or 1400 lines of pure unique functionality? You may find
      that during this assessment your code CAN actually be handled in one file.

      There is often a requirement and justification for multiple files especially
      if - say - all the subroutines are in one file and control in the other. If
      a "project" calls for different processes, for example, several and
      fifferent operations on a text file, then I may write each of those
      operations into separate files, each calling on the same static/subroutines
      in a fixed file, but I personally don't pass active control between two
      files and expect things to work.

      I would say it's worth reassessing the content of both files. When you start
      throwing dynamic variables between files you really have to pay attention to
      your controls, and if you are going to spend that much time on your code you
      may as well set off on the right foot to start with.

      It's not so much perl coding as effective programming techniques that you
      may need to delve into.



      > Hi,
      >
      > I have a script that is divided into 2 files for manageability. Each
      > is
      > about 700 lines in length and the second file is called from the
      > first
      > using the 'Require' method.
      >
      > Is there an easy way of passing a variable from the first file to the
      > second? Or should I be looking at using @Export or something similar?
      >
      > Thanks
      > John
    • Mike Southern
      PS: I assumed that you were writing a program running outside of a website environment - like a file or text processor. If what you are doing is web page form
      Message 2 of 4 , May 3, 2005
      • 0 Attachment
        PS: I assumed that you were writing a program running outside of a website
        environment - like a file or text processor.

        If what you are doing is web page form processing then not much of what I
        wrote applies (except to encourage good design :) )

        HTML form get/post methods of course are built entirely to pass data between
        different handling scripts.




        > Hi,
        >
        > I have a script that is divided into 2 files for manageability. Each
        > is
        > about 700 lines in length and the second file is called from the
        > first
        > using the 'Require' method.
        >
        > Is there an easy way of passing a variable from the first file to the
        > second? Or should I be looking at using @Export or something similar?
        >
        > Thanks
        > John
      • johnbr123zzz
        Hi Thanks everyone for your comments. I have taken note of the advice re better programming practices. This was my first Perl script and the aim was to just
        Message 3 of 4 , May 3, 2005
        • 0 Attachment
          Hi

          Thanks everyone for your comments. I have taken note of the advice re
          better programming practices.

          This was my first Perl script and the aim was to just get something
          that ran reliably and learn perl at the same time. It runs as a unix
          Cron job (outside of a website) on our network doing daily number
          crunching and then emails the results.

          Wanting to pass variables between the two files has only come about
          since trying to do a recent change. For ease of testing I wanted the
          script to know whether its running in the production or testing
          environment.

          In the long term I will probably rewrite it. I am thinking it might
          be a good exercise to put a lot of the subs into a module, for reuse.

          Thanks, John

          --- In perl-beginner@yahoogroups.com, Mike Southern <gb1198@c...>
          wrote:
          > PS: I assumed that you were writing a program running outside of a
          website
          > environment - like a file or text processor.
          >
          > If what you are doing is web page form processing then not much of
          what I
          > wrote applies (except to encourage good design :) )
          >
          > HTML form get/post methods of course are built entirely to pass
          data between
          > different handling scripts.
          >
          >
          >
          >
          > > Hi,
          > >
          > > I have a script that is divided into 2 files for manageability.
          Each
          > > is
          > > about 700 lines in length and the second file is called from the
          > > first
          > > using the 'Require' method.
          > >
          > > Is there an easy way of passing a variable from the first file to
          the
          > > second? Or should I be looking at using @Export or something
          similar?
          > >
          > > Thanks
          > > John
        Your message has been successfully submitted and would be delivered to recipients shortly.