Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 21

Thread: Perl: Oposite of require

  1. #11

    Re:Perl: Oposite of require

    Here is a problem with undef though, it seems to totally kill off a funtion and not allow a new funtion to take it's place later ... for instance ..

    Code:
    sub foo {
    
       print "foo\n";  
    
    }
    
    foo();
    
    undef &foo;
    
    sub foo {
    
       print "lalalala";
    
    }
    
    foo();
    gives errors ... the second foo() is undefined which totally halts a Tk application in terms of even attempting to run the sub :-/.

    Code:
    Undefined subroutine &main::foo called at - line 17.
    lalalalanwsupplies:/home/ash/darkfire/dkfutils/linux_standard_installer/beta#

  2. #12

    Re:Perl: Oposite of require

    [quote author=Ashcrow link=board=9;threadid=4291;start=0#44023 date=1028156402]gives errors ... the second foo() is undefined which totally halts a Tk application in terms of even attempting to run the sub :-/.[/quote]

    Wow, I don't know what to say. I'm totally dumbfounded by this -- undef undefines something permanently?? I'm stumped. Hang on...

    Instead of undefining the function, you could just redefine it to do nothing. IOW:

    Code:
    sub foo
    {
      print "foo\n";
    }
    
    foo();  # prints 'foo'
    
    sub foo
    {
    }
    
    foo ();  # does nothing
    
    sub foo
    {
      print "bar\n";
    }
    
    foo();  # prints 'bar'

  3. #13

    Re:Perl: Oposite of require

    Code:
    ash@nwsupplies:~$ perl 
    sub foo
    {
      print "foo\n";
    }
    foo();
    
    sub foo
    {
    }
    foo ();
    sub foo
    {
      print "bar\n";
    }
    foo(); 
    bar
    bar
    bar
    ash@nwsupplies:~$
    :P It seems like such a simple problem but the solution escapes me!

  4. #14

    Re:Perl: Oposite of require

    oh shit! damnit

    I can't believe I forgot. perl does a whole whack of preprocessing stuff, it doesn't just blindly execute line by line. In other words, foo() gets defined three different times before foo() gets run at all, and the end result is that only the last definition sticks. That also explains the undef thing.

    I was thinking you might be able to use anonymous subroutines, but unfortunately they can't be passed arguments, and can only consist of single commands.

    My perl reference book says something or other about function overloading, but it's just talking about perl builtins, not userdefined subroutines... I'm pretty lost on this...

  5. #15

    Re:Perl: Oposite of require

    Ok, I've got it.

    What you'll want to do is define all your functions to have different names, then use references to call the one that you want to call.

    For example:

    Code:
    sub foo1 { print "foo\n" }
    sub foo2 { print "bar\n" }
    sub foo3 { print "baz\n" }
    sub foo4 { print "qux\n" }
    
    $foo = \&foo1;
    &$foo;
    
    $foo = \&foo2;
    &$foo;
    
    $foo = \&foo3;
    &$foo;
    
    $foo = \&foo4;
    &$foo;
    produces the expected output:

    foo
    bar
    baz
    qux
    I haven't quite figured out how you're supposed to pass arguments to the function, though... hehe

  6. #16

    Re:Perl: Oposite of require

    It works but it's a little extra code I think ... this seems to work (though I havn't tested it 100% yet)

    Code:
    sub remmodule {
      
       install = { };
       remove = { }'
       upgrade = { };
       query = { };
       sig = { };
    
    }
    Oddly enough, it works as long as there is nothing (besides whitespace) inside the function and seems to only want to work correctly if called inside a sub (verses tty perl stuff).

    Thanks Feztaa for your help! :-)

  7. #17

    Re:Perl: Oposite of require

    Though here comes the second problem, each module can only be read once it seems. For instance ...

    Code:
    require this;
    require that;
    require this;
    still acts like ::that:: after the second require this. :P.

  8. #18

    Re:Perl: Oposite of require

    [quote author=Ashcrow link=board=9;threadid=4291;start=0#44469 date=1028433148]
    Code:
    sub remmodule {
      
       install = { };
       remove = { }'
       upgrade = { };
       query = { };
       sig = { };
    
    }
    [/quote]

    That's weird, because it really shouldn't work. In this context, AIUI, perl has no idea what you mean by 'install' or 'remove' --> it shouldn't be able to know that you are talking about functions. I would have prefixed all those lines with 'sub', but maybe it won't work then. I have no idea!

    Anyway, if it works, then don't dick with it

    Thanks Feztaa for your help! :-)
    No problem

  9. #19

    Re:Perl: Oposite of require

    [quote author=Ashcrow link=board=9;threadid=4291;start=0#44481 date=1028440446]Though here comes the second problem, each module can only be read once it seems. For instance ...

    Code:
    require this;
    require that;
    require this;
    still acts like ::that:: after the second require this. :P.[/quote]

    Ok, now that's just fucked up.

    The only thing I can suggest is that you try putting the require statements into functions, and then calling the functions as necessary. That might do the trick, but I haven't tested it. According to my Perl Cookbook, it will "simulate a use, but at runtime" which is probably what you want.

  10. #20

    Re:Perl: Oposite of require

    require is weird i posted here a while ago about requiring more than once:

    Code:
    sub rerequire {
       my ($file) = @_;
       delete($INC{$file});
       eval('require("$file")');
       if($@) {
          return "** Failed: $file:$@" if ($@);
       } else {
          return "Reloaded commands.";
       }
    }
    that's what i ended up writting.

    good luck.

Similar Threads

  1. Wireless NIC seems to require restart
    By axxis in forum Linux - Hardware, Networking & Security
    Replies: 1
    Last Post: 01-17-2007, 06:02 AM
  2. Perl and XML
    By coltrane in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 12-09-2004, 10:57 PM
  3. Perl Help
    By pwrhouse in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 04-06-2003, 11:57 PM
  4. perl question from a perl newb
    By tolstoy in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 09-06-2002, 01:16 AM
  5. What does WineX require?
    By airhead in forum Linux - Software, Applications & Programming
    Replies: 3
    Last Post: 08-03-2002, 03:21 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •