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

The Archive Base Latest Questions

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

For long time, I was always thinking that parameters in Perl subs are passed

  • 0

For long time, I was always thinking that parameters in Perl subs are passed by value. Now, I hit something that I don’t understand:

use strict;
use warnings;

use Data::Dumper;

sub p {
    print STDERR "Before match: " . Data::Dumper->Dump([[@_]]) . "\n";
    "1" =~ /1/;
    print STDERR "After  match: " . Data::Dumper->Dump([[@_]]) . "\n";
}

my $line = "jojo.tsv.bz2";

if ($line =~ /\.([a-z0-9]+)(?:\.(bz2|gz|7z|zip))?$/i) {
    p($1, $2 || 'none');
    p([$1, $2 || 'none']);
}

On first invocation of p(), and after executing of regexp match, values in @_ will become undefs. On the second invocation, everything is OK (values passed as array ref are not affected).

This was tested with Perl versions 5.8.8 (CentOS 5.6) and 5.12.3 (Fedora 14).

The question is – how this could happen, that regexp match destroys content of @_, which was built using $1, $2 etc (other values, if you add them, are not affected)?

  • 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-23T07:16:25+00:00Added an answer on May 23, 2026 at 7:16 am

    The perlsub man page says:

    The array @_ is a local array, but its elements are aliases for the actual scalar parameters.

    So when you pass $1 to a subroutine, inside that subroutine $_[0] is an alias for $1, not a copy of $1. Therefore it gets modified by the regexp match in your p.

    In general, the start of every Perl subroutine should look something like this:

    my @args = @_;
    

    …or this:

    my ($arg1, $arg2) = @_;
    

    …or this:

    my $arg = shift;
    

    And any capturing regexp should be used like this:

    my ($match1, $match2) = $str =~ /my(funky)(regexp)/;
    

    Without such disciplines, you are likely to be driven mad by subtle bugs.

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

Sidebar

Related Questions

For a long time, I always thought that the bs and count parameters for
I've used capistrano for a long time, but always for sites that weren't critical.
A long time ago I remember reading that you should always use the smallest
Long time reader never posted until now. Im having some trouble with Android, im
This question's been bugging me for a long time. I've always wondered how game
long time reader, first time poster. I’m coming with an issue that many of
For a long time I've noticed that adjusting line-height and adding some extra space
I have this problem for a long time now, I have searched the web
Installing third-party components always take a long time specially if you have large ones,
I'm a long-time ActionScript 2 user, now getting started with ActionScript 3. The one

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.