Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Passing match regexs as arguments to subroutines

by ton (Friar)
on Jul 24, 2001 at 06:03 UTC ( [id://99214]=perlquestion: print w/replies, xml ) Need Help??

ton has asked for the wisdom of the Perl Monks concerning the following question:

Look at the following code fragment and try and guess what the output will be:
use strict; my $foo = "Hello World!"; _Printer(0, 1, 2, 3, ($foo =~ m/zoom/), 5); sub _Printer { my $foo; my $i = 0; while (scalar(@_)) { $foo = shift; print "$i: $foo\n"; $i++; } }
If you thought the output should look like the following, you're wrong:
0: 0 1: 1 2: 2 3: 3 4: 5: 5
The actual output (on my 5.6 Linux version of perl) is the following:
0: 0 1: 1 2: 2 3: 3 4: 5
Apparently, Perl doesn't pass in a false or undef value for a failed regular expression match; instead, it completely ignores the argument. A passing regex match does get passed in, with a value of 1.

Anybody want to take a shot at explaining this?

-Ton

-----
Be bloody, bold, and resolute; laugh to scorn
The power of man...

Replies are listed 'Best First'.
Re: Passing match regexs as arguments to subroutines
by japhy (Canon) on Jul 24, 2001 at 06:15 UTC
    Your regex is evaluated in list context. In list context, a pattern match ($str =~ /pat/) behaves as follows:
    • failed? return the empty list
    • succeeded and has ()'s? returns captured subpatterns
    • succeeded? return 1

    _____________________________________________________
    Jeff japhy Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Oddly enough, it still won't work if you remove the parentheses; you have to force scalar on it. Guess regexs are in list context by default. Learn something new every day...

      Thanks!

      -Ton
      -----
      Be bloody, bold, and resolute; laugh to scorn
      The power of man...

        You seem to be subtly misinterpreting the way context works in perl. There is no such thing as "default" context for a given operation or expression. An expression is either in list, scalar, or void context depending upon how it is being used.

        Furthermore, the placement of parens around an expression doesn't put that expression into list context. The only time parens determine context is when they are placed around the left side of an assignment, thereby providing list context for the right side of the assignment.

        Arguments to unprototyped user subroutines are always evaluated in list context, which is what you observed in your example. However, had you prototyped your subroutine as sub _Printer ($$$$$$) { ... }, you would have seen your regex evaluated in a scalar context, regardless of whether or not there were parens around it.

        Basically, no function/operator has any say as to what context its result is provided in. The causality works in the other direction. When you observed that you had to force scalar on your regex, it was not because the regex had been in list context by default, but because you were using the regex in list context.

           MeowChow                                   
                       s aamecha.s a..a\u$&owag.print
(MeowChow) Re: Passing match regexs as arguments to subroutines
by MeowChow (Vicar) on Jul 24, 2001 at 06:18 UTC
    Context strikes again. From perlop:
      
    If the /g option is not used, m// in list context returns a list consi +sting of the subexpressions matched by the parentheses in the pattern +, i.e., ($1, $2, $3...). (Note that here $1 etc. are also set, and th +at this differs from Perl 4's behavior.) When there are no parenthese +s in the pattern, the return value is the list (1) for success. With +or without parentheses, an empty list is returned upon failure.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://99214]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2024-03-28 14:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found