Announcement

Collapse
No announcement yet.

Perl masters - help please!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Perl masters - help please!

    Hi,

    I've run up against a problem at the last stage of a script I've written. Everything worked on a test script, but when I add the subroutines to the Actinic perl scripts it ceases to work.

    Essentially I'm trying to read in the contents of another perl file at runtime using require(). This external file holds one hash containing some stock levels. On my test script, the following worked great:
    Code:
    my %stockLevels;					# initialise the hash
    require ("stock.pl");					# read in the file with the new hash
    my $stockLevel=$stockLevels{'product1'};		# grab the stock level
    stock.pl contains:

    Code:
    %stockLevels=('product1'=>12,'product2'=>50);
    1;
    I then inserted this inside a function in ShoppingCart.pl. The function runs OK, and require() seems to pickup the file, but $stockLevel remains unassigned. I've also tried eval ("stock.pl") and eval ("require stock.pl") with no results.

    $stockLevels just isn't being assigned. If I assign some values to the hash, then read in the file, the original hash remains unaffected and I can access the original values. I'm assuming this is some kind of variable scope problem but I really don't know that much about Perl, and I'm tearing my hair out.

    Can anyone from Actinic or any perl masters shed any light on this? Thanks in advance

    Graham
    www.gbradley.co.uk
    Web Development, Actinic Patches, Scripts & more

    #2
    I think you're on the right track with scope. ShoppingCart.pl has a 'use strict' at the top, so 'my %stockLevels' is local to that module and not visible to the included module. Getting rid of the 'my' might solve it.

    I am not a Perl expert.

    Alan Compton
    www.greenknightgames.co.uk
    Great board games and cards games you won't find in the High Street

    Comment


      #3
      I tried without 'my', but that just killed the script saying

      "global symbol '%stockLevels' requires explicit package name..."

      I then read variables with "main::" should refer to the global scope, so I tried prefixing all references to stockLevels with this, but that had no effect either. I could try requiring the file at the start of the script to make them truly global, but there's actually a choice between several files to include depending on the product's supplier - I only want to require what I need.

      So, I guess I need to define the included %stockLevels with the scope of the 'parent' subroutine...but how do I do that?
      www.gbradley.co.uk
      Web Development, Actinic Patches, Scripts & more

      Comment


        #4
        Have you found perl.com yet?

        Alan

        Comment


          #5
          Eventually found that 'our' instead of 'my' will set the variable to the global scope, which resolves the problem Probably not best practice but oh well..
          www.gbradley.co.uk
          Web Development, Actinic Patches, Scripts & more

          Comment


            #6
            Another problem with the same script....

            I now need to share the procedures across a couple of files so I put them in 'stockCheck.pl' and added the appropriate require statements to the start of the Actinic scripts. This resulted in

            stockCheck.pl did not return a true value at g:\websites\xxxxxxxx\cgi-bin\ca000001.pl line 33.

            The .pl file does have '1;' at the end of the file to make sure it returns true (I also tried 'return 1;'), but it seems something else is causing this error. Any more ideas....?
            www.gbradley.co.uk
            Web Development, Actinic Patches, Scripts & more

            Comment


              #7
              Looking at the other Perl modules that get included with a 'requires', they all:
              - are called xxxxxx.pm (presumably Perl-Module)
              - start with: package <package_name>;
              - only contain subroutines (sub)
              - finish with a 1; (presumably this is their equivalent of a main program)

              Have you checked all the subroutine braces are matching?

              Alan

              Comment


                #8
                Could it just be that you have to use the Actinic naming convention of 000001.pl so that it matches the script id?
                Cheers

                David
                Located in Edinburgh UK

                http://twitter.com/mcfinster

                Comment


                  #9
                  'return 1;' seems to do the job now. Thanks for the replies
                  www.gbradley.co.uk
                  Web Development, Actinic Patches, Scripts & more

                  Comment

                  Working...
                  X