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

  • Home
  • SEARCH
  • 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 7179109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:04:26+00:00 2026-05-28T17:04:26+00:00

Using sub prototypes, we can define our own subs that look like map or

  • 0

Using sub prototypes, we can define our own subs that look like map or grep. That is, the first coderef argument has shorter syntax than a normal anonymous sub. For example:

sub thunked (&) { $_[0] }

my $val = thunked { 2 * 4 };

Works great here, since the first argument is the coderef. For latter arguments however, it simple won’t parse properly.

I made a with sub designed to make writing GTK2 code cleaner. It’s meant to look like this (untested since it’s hypothetical code):

use 5.012;
use warnings;

use Gtk2 '-init';    

sub with ($&) {
    local $_ = $_[0];
    $_[1]->();
    $_;
}

for (Gtk2::Window->new('toplevel')) {
    $_->set_title('Test Application');
    $_->add(with Gtk2::VBox->new {
        my $box = $_;
        $box->add(Gtk2::Button->new("Button $_")) for (1..4);
    });
    $_->show_all;
}
Gtk2->main;

It doesn’t work because with needs to take the block as a first argument for the nice syntax to work. Is there any way to pull it off?

  • 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-28T17:04:26+00:00Added an answer on May 28, 2026 at 5:04 pm

    The module Devel::Declare contains tools for extending Perl’s syntax in a relatively safe way.

    Using Devel::Declare you would create a hook on the with token, which will stop the parser when it reaches that word. From there, you have control over the parser and you can read ahead until you reach a { symbol. At that point, you have what you need to work with, so you rewrite it into valid Perl, and pass it back to the parser.

    in the file With.pm:

    package With;
    use warnings;
    use strict;
    use Devel::Declare;
    
    sub import {
        my $caller = caller;
        Devel::Declare->setup_for (
            $caller => {with => {const => \&parser}}
        );
        no strict 'refs';
        *{$caller.'::with'} = sub ($&) {
            $_[1]() for $_[0];
            $_[0]
        }
    }
    
    our $prefix = '';
    sub get {substr Devel::Declare::get_linestr, length $prefix}
    sub set {       Devel::Declare::set_linestr $prefix . $_[0]}
    
    sub parser {
        local $prefix = substr get, 0, length($_[0]) + $_[1];
        my $with = strip_with();
        strip_space();
        set "scalar($with), sub " . get;
    }
    
    sub strip_space {
        my $skip = Devel::Declare::toke_skipspace length $prefix;
        set substr get, $skip;
    }
    
    sub strip_with {
        strip_space;
        my $with;
        until (get =~ /^\{/) {
            (my $line = get) =~ s/^([^{]+)//;
            $with .= $1;
            set $line;
            strip_space;
        }
        $with =~ s/\s+/ /g;
        $with
    }
    

    and to use it:

    use With;
    
    sub Window::add {say "window add: ", $_[1]->str}
    sub Window::new {bless [] => 'Window'}
    sub Box::new    {bless [] => 'Box'}
    sub Box::add    {push @{$_[0]}, @_[1..$#_]}
    sub Box::str    {"Box(@{$_[0]})"}
    sub Button::new {"Button($_[1])"}
    
    with Window->new {
        $_->add(with Box->new {
            for my $num (1 .. 4) {
                $_->add(Button->new($num))
            }
        })
    };
    

    Which prints:

    window add: Box(Button(1) Button(2) Button(3) Button(4))
    

    A completely different approach would be to skip the with keyword altogether and write a routine to generate constructor subroutines:

    BEGIN {
        for my $name (qw(VBox)) { # and any others you want
            no strict 'refs';
            *$name = sub (&@) {
                use strict;
                my $code = shift;
                my $with = "Gtk2::$name"->new(@_);
                $code->() for $with;
                $with
            }
        }
    }
    

    and then your code could look like

    for (Gtk2::Window->new('toplevel')) {
        $_->set_title('Test Application');
        $_->add(VBox {
            my $box = $_;
            $box->add(Gtk2::Button->new("Button $_")) for (1..4);
        });
        $_->show_all;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are currently using Fitnesse for sub system testing. [All our applications are in
I found that many sites using sub-domain to handle login. one of them is
I am trying to achieve the following without using sub query. For a funding,
I want to create sub-domains using PHP on the fly. Suppose a user registers
I'm using iReport and I need to create a sub-report using a XML DataSource
In Excel I have attached a picture to a cell using the following Sub
Using the following code: Private Sub MakeMeSomeXmlBeforeRyanGetsAngry() Dim db As New MyDBDataContext Dim customer
I am using DirectoryInfo.GetDirectories() recursively to find the all the sub-directories under a given
I'm using the following code to get an array with all sub directories from
UPDATED Using RedDot CMS, linking to a 'sub-page' (page within a page) directly will

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.