Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Passing $2 to a sub

by SparkeyG (Curate)
on Feb 13, 2001 at 22:50 UTC ( #58188=perlquestion: print w/replies, xml ) Need Help??
SparkeyG has asked for the wisdom of the Perl Monks concerning the following question:

In the following snippit, I wish to take the second match, $2, and pass it to a sub that will create a DB table. The problem is that w/in the sub, the variable shows the entire line matched.
if ( /(^\s?>)(.*)$/ ) { print "$2\n"; &createTable($2); next; } sub createTable { print $_; }
The output I am getting is:
apples >apples trees >trees etc.. >etc..
The expected output would be, I thought...
apples apples trees trees etc.. etc..
JAPP SparkeyG

Replies are listed 'Best First'.
Re: Passing $2 to a sub
by japhy (Canon) on Feb 13, 2001 at 22:53 UTC
    $_ is not the argument passed the function. To get that, you need to access the @_ array -- you want the first element, which is $_[0].

    japhy -- Perl and Regex Hacker
      Ahh, boy is my face red.
Re: Passing $2 to a sub
by baku (Scribe) on Feb 13, 2001 at 23:03 UTC

    You can't use $_ in stead of @_ like that. You want to change the subroutine to one of:

    sub createTable { my $value = shift; print $value; } # or sub createTable { print $_[0]; # subscript means array }

    The $x syntax only works for @x variables when followed by a subscript in []'s. See perlvars for LOTS of details :-)

Re: Passing $2 to a sub
by Gloom (Monk) on Feb 13, 2001 at 22:56 UTC
    Try something like this :

    if( /^\s*>/ ) { print "$'\n"; createTable( $' ); } sub createTable { print shift; }

    Hope this helps
      I may be wrong (I've only read Mastering Regular Expressions once), but doesn't using $&, $' or $` anywhere in your program cause some fairly severe performance issues?

      Looks like the regex originally used should work - just trying to print $_ instead of $_[0]
        Yep, you are right. From perlre:
        WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program. Perl uses the same mechanism to produce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the grouping behaviour, use the extended regular expression (?: ... ) instead.) But if you never use $&, $` or $', then patterns without capturing parentheses will not be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid the price. As of 5.005, $& is not so costly as the other two.
                       s aamecha.s a..a\u$&owag.print

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://58188]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2017-02-26 18:44 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (376 votes). Check out past polls.