How to create a dynamic Object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • annabel
    New Member
    • May 2010
    • 2

    How to create a dynamic Object

    I'm trying to create an object dynamically using a string as the object type. It works for me with a simple example:

    my $string = "Employee";

    my $objct = new {$string}("Firs t", "Last", 12345);

    Employee is a derived class of a Person class. However, with my real code I get an error: Compilation failed in require at studyMngr.pm line 7. The line triggering it is:

    my $nextStepbjct = new {$nextProcessSt ep}("name");

    Line 7 in this file (studyMngr.pm) has use SomeFile.pm.

    What's the difference between the two ways of using a dynamic object is beyond me.
    I'm on a deadline with this code. Help....
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Sorry, but your deadline is not our concern. However, if you post a short but complete script that demonstrates the problem, we'll see if we can help.

    Comment

    • annabel
      New Member
      • May 2010
      • 2

      #3
      The full error message is given below.

      The code is attached as a tar file. If you run:

      perl ChIPSeqPipeline _main.pl it should generate the following error message:

      Uncaught exception from user code:
      Uncaught exception from user code:
      Uncaught exception from user code:
      Uncaught exception from user code:
      syntax error at ChIPSeqPipeline _processStepMng r.pm line 134, near "}("
      Compilation failed in require at ChIPSeqPipeline _studyMngr.pm line 7.
      StudyMngr::BEGI N() called at ChIPSeqPipeline _processStepMng r.pm line 7
      eval {...} called at ChIPSeqPipeline _processStepMng r.pm line 7
      require ChIPSeqPipeline _studyMngr.pm called at ChIPSeqPipeline _main.pl l
      ine 7
      main::BEGIN() called at ChIPSeqPipeline _processStepMng r.pm line 7
      eval {...} called at ChIPSeqPipeline _processStepMng r.pm line 7
      BEGIN failed--compilation aborted at ChIPSeqPipeline _studyMngr.pm line 7.
      require ChIPSeqPipeline _studyMngr.pm called at ChIPSeqPipeline _main.pl l
      ine 7
      main::BEGIN() called at ChIPSeqPipeline _studyMngr.pm line 7
      eval {...} called at ChIPSeqPipeline _studyMngr.pm line 7
      Compilation failed in require at ChIPSeqPipeline _main.pl line 7.
      main::BEGIN() called at ChIPSeqPipeline _studyMngr.pm line 7
      eval {...} called at ChIPSeqPipeline _studyMngr.pm line 7
      BEGIN failed--compilation aborted at ChIPSeqPipeline _main.pl line 7.
      Attached Files

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        I have no interest in installing and troubleshooting your entire app.

        Start by enabling strict in all scripts and fix the problems that are pointed out. Commenting out the use strict lines to get around coding errors is the wrong approach.

        Don't use the indirect object syntax.

        Change:
        Code:
        my $objct = new {$string}("First", "Last", 12345);
        To:
        Code:
        my $objct = ${string}->new("First", "Last", 12345);

        Comment

        Working...