Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6542879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:16:47+00:00 2026-05-25T11:16:47+00:00

I want to make a small GUI using Tk in Perl that will have

  • 0

I want to make a small GUI using Tk in Perl that will have 2 buttons: Race and Quit.

I want the Race button to run a function that is located in a module Car and is called Race.

I’ve written the following code:

#!/usr/bin/perl -w

use strict;
use warnings;
use Car;
use Tk;

my $mw = MainWindow->new;
$mw->Label(-text => "The Amazing Race")->pack;
$mw->Button(
        -text    => 'Race',
        -command => sub {Car->Race()},
)->pack;
$mw->Button(
        -text    => 'Quit',
        -command => sub { exit },
)->pack;
MainLoop;

It works, but It seems stupid to me to make an unnamed subroutine that will just call another subroutine. But when I tried to use -command => sub Car->Race(), or -command => sub \&Car->Race(), it didn’t work.

I understand that this is because I’m not passing a reference to the function. How do I pass a reference to a function that is located in another namespace (module)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:16:48+00:00Added an answer on May 25, 2026 at 11:16 am

    This syntax is symple:

    $mw->Button(
            -text    => 'Race',
            -command => \&Car::Race,
    )->pack;
    

    But if you need to pass any special arguments to that functions or call it as method, you still need an anon sub:

    $mw->Button(
            -text    => 'Race',
            -command => sub { Car->Race(@_) },
    )->pack;
    

    This one calls Race as method of package Car and pass all arguments to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one small online sale business but I want to make it scalable
I want to make a table in SqlServer that will add, on insert, a
i want to make a small windows app that start every windows startup this
I have a small PHP framework which I want to make available for download.
I have written a small PHP framework and I want to make a website
I want to make a small text-height div run with the text. My code
I'm trying to make a small GUI app and I want to use MigLayout
I have a small view with a few buttons on it. I want to
I created a project in c++ that run for hours. I want to make
A researcher has created a small simulation in MATLAB and we want to make

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.