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 6703403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:10:01+00:00 2026-05-26T07:10:01+00:00

I’m a big fan of functional programming, so when I discovered block references in

  • 0

I’m a big fan of functional programming, so when I discovered block references in Perl I started using them a lot.

However, the functions I’ve written that take blocks as arguments are written in this style:

sub mygrep (&@) {
    my $code = shift;
    my @result;
    foreach $_ (@_) {
        push(@result, $_) if &$code;
    }
    @result;
}

(From http://perldoc.perl.org/perlsub.html#Prototypes)

In essence, most of my functions set $_ in order for the code block to gain access to data in my sub. I guess my question can be split into three subquestions:

  1. Are there some major pitfalls in this approach?
  2. Is it a better idea to localize $_ before setting it?
  3. Should i use partially applied functions instead?

I’m still a Perl newbie so any answers and suggestions are appreciated – thanks in advance! 🙂

  • 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-26T07:10:02+00:00Added an answer on May 26, 2026 at 7:10 am

    1. Are there some major pitfalls in this approach?

    1. my $_; in view of the block will hide your changes to package variable $_. There’s nothing you can do about that from inside of mygrep.

    2. &$code is very special. You want &$code() or $code->() instead.

    3. Changing $_ will change the arguments passed to mygrep. That’s undesirable here.

    2. Is it a better idea to localize $_ before setting it?

    for provides much better localisation that local, but it also provides aliasing that’s undesirable here.

    3. Should i use partially applied functions instead?

    I don’t know what that means.


    Fixed:

    sub mygrep (&@) {
        my $code = shift;
        my @result;
        for (@_) {
           # Create copy so $_ can be modified safely.
           for (my $s = $_) {
              push @result, $_ if $code->();
           }
        }
    
        return @result;
    }
    

    That said, I think mygrep is kind pointless, since map+grep already does what you want more easily. Compare

    mygrep { if ($_ % 2) { ++$_; 1 } else { 0 } } LIST
    

    with

    map { $_+1 } grep { $_ % 2 } LIST
    

    You can even merge the map and grep.

    map { $_ % 2 ? $_+1 : () } LIST
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

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.