use external data import value into script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cassbiz
    New Member
    • Oct 2006
    • 202

    use external data import value into script

    I am working on a shopping cart and have implemented a coupon hack for it. What I am trying to do (without breaking it too bad, LOL ) is to have the values stored in an external file and call it when needed. Like require in PHP.

    Is there an example of a line of code that will work for my application?

    I have been reading all over the web and see require() and use() but am not understanding the foobar

    currently the line I want to import into the script is

    Code:
    @sc_discount_logic = ("1005||||5%", "1500||||5.00");
    Thanks in advance
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    You probably just want to use the open() function to open and read the file into a variable. In it's simplest form:

    Code:
    open(FH,"yourfile.txt") or die "$!";
    my @data = <FH>;
    close(FH);

    Comment

    • cassbiz
      New Member
      • Oct 2006
      • 202

      #3
      Originally posted by KevinADC
      You probably just want to use the open() function to open and read the file into a variable. In it's simplest form:

      Code:
      open(FH,"yourfile.txt") or die "$!";
      my @data = <FH>;
      close(FH);

      I used the above snippet and the following error came up.

      Error loading library ./library/agora.setup.db:

      No such file or directory at ./library/agora.setup.db line 339. Compilation failed in require at (eval 7) line 1.

      Please fix the error and try again.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        I don't know where the "error loading library" is coming from, but the the next error appears as though the path to the file or the filename is not correct. Make sure to use the full path to the file you want to open. Make sure the spelling is correct and the case is correct if using a case sensitive operating system.

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Is it a text file you are wanting to get the data from into your perl script? If not, my suggestion will not work.

          Comment

          • cassbiz
            New Member
            • Oct 2006
            • 202

            #6
            Originally posted by KevinADC
            Is it a text file you are wanting to get the data from into your perl script? If not, my suggestion will not work.
            Yes it is a txt file. I would insert it into the actual script but the end user needs to alter just the one line and honestly don't trust them with the rest of it.

            LOL

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              OK, but I really don't understand what you are trying to do so I have no further suggestions unless you can clarify your situation in more detail.

              Comment

              • cassbiz
                New Member
                • Oct 2006
                • 202

                #8
                Originally posted by KevinADC
                OK, but I really don't understand what you are trying to do so I have no further suggestions unless you can clarify your situation in more detail.

                OK I will try,


                the current code is:

                Code:
                blah blah blah
                
                ### THE LINE BELOW I WANT TO INCLUDE AS A SEPARATE FILE
                ### with the script reading the following line into it.
                
                @sc_discount_logic = ("1005||||5%", "1500||||5.00");
                
                
                blah blah blah
                Currently the external file I have is called coupon.txt:
                Code:
                ### Change below
                
                @sc_discount_logic = ("1005||||5%", "1500||||5.00");

                I have tried the open() function that you wrote earlier, had all files in the
                same directory.

                when I used the snippet you provided, the script erred. Put it back to the original and it runs fine.

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Then what you really want is a module, not a text file. Either that or put just the data part in the file, not the @array = (blah blah blah) stuff.

                  coupon.txt:

                  1005||||5%
                  1500||||5.00


                  in your perl scipt:

                  Code:
                  open(FH,"coupon.txt") or die "$!";
                  my @array = <FH>;
                  close(FH);
                  chomp(@array);
                  now @array will be populated with the lines from coupon.txt.

                  Comment

                  Working...