tommyw has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a piece of code which needs Perl Tk, which may or may not be installed. It behaves differently depending on whether I use require and import, or use:
#! /usr/bin/perl -w use strict; eval { require Tk; import Tk; }; die "Can't load Tk" if $@; MainWindow->new->Button(-text => "Cancel", -command => sub {exit 1} )->pack(-side => 'left', -expand => 1); Tk::MainLoop();
Assuming that Tk is available, this produces a simple exit button. On my mandrake 9.1 box at home (perl 5.8.0) this works fine, and silently. On my solaris box at work (perl 5.6.1), it produces a segmentation fault at exit. However, if I change to "use" instead:
#! /usr/bin/perl -w use strict; use Tk; MainWindow->new->Button(-text => "Cancel", -command => sub {exit 1} )->pack(-side => 'left', -expand => 1); Tk::MainLoop();
Then it behaves the same on both machines, generates the text:
Callback called exit. Callback called exit.
on stderr, and no segmentation fault occurs. Why? What's different? What am I doing wrong? (Hopefully, if I can make require and import behave like use, I can also get around the segmentation fault.)

--
Tommy
Too stupid to live.
Too stubborn to die.

edited: Thu Jun 12 12:13:52 2003 by jeffa - title change (was: Use != Require & Import?)

Replies are listed 'Best First'.
Re: Segmentation fault at exit on Solaris 5.6.1 if require Tk instead of use
by broquaint (Abbot) on Jun 12, 2003 at 09:44 UTC
    The use statement is equivalent to
    BEGIN { require Tk; Tk->import; }
    I'm not sure why this would make Tk break, but I imagine it might have something to do with compile-time magic (where segfaults have a greater potential to occur).
    HTH

    _________
    broquaint

      Yes, I thought that use was supposed to be the same as require and import, as frequently mentioned. (Yes, I did se use Super Search)

      Ah. Progress. If I put the require inside a BEGIN block, it does work "properly". In that it's the same on both boxes, and importantly, doesn't seg fault on the Solaris machine.

      --
      Tommy
      Too stupid to live.
      Too stubborn to die.

Re: Segmentation fault at exit on Solaris 5.6.1 if require Tk instead of use
by PodMaster (Abbot) on Jun 12, 2003 at 09:45 UTC
    Works fine for me
    perl -Mstrict -we"eval{require Tk;import Tk;1} or die $@;tkinit()->But +ton(-text=>1,-command=>sub{exit})->pack;MainLoop();" # or perl -Mstrict -we'eval{require Tk;import Tk;1} or die $@;tkinit()->But +ton(-text=>1,-command=>sub{exit})->pack;MainLoop();'
    On lastest 5.6 (Tk 800.023) and 5.8 (Tk 800.024) ActivePerl on Win2000SP3.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      I only get the callback called exit messages if I give exit an non-zero parameter. See what happens if you use sub{exit 1}

      --
      Tommy
      Too stupid to live.
      Too stubborn to die.

        Makes no difference (which is what should happen). Where is the message coming from? Which version of Tk you got man?

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.