Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Function Split, bug or error in the documentation?

by bojinlund (Monsignor)
on Jul 04, 2020 at 11:15 UTC ( [id://11118906]=note: print w/replies, xml ) Need Help??


in reply to Function Split, bug or error in the documentation?

I am trying to understand split and its documentation. To do this I have implement split by using m{} and the variables @- and @+. By this I have found some problems

This script shows some of them:

use strict; use warnings; use 5.010; use Data::Dump qw(dump dd ddx); sub split_e { } split_e( // ); # Warning: Use of uninitialized value $_ in pattern match (m//) at pm_ +1.pl ... my @rv = split( //, '' ); # Warning: none my $str = '1-10,20'; my $pat = '(-)|(,)'; @rv = split( $pat, $str ); warn dump @rv; # (1, "-", undef, 10, undef , ",", 20) @rv = $str =~ m{$pat}g; warn dump @rv; # ("-", undef, undef, ",") while ( my $rv = $str =~ m{$pat}gc ) { for my $ix ( 0 .. 99 ) { if ( defined $-[$ix] ) { say sprintf '$ix= %d (%d,%d)<%s>', $ix, $-[$ix], $+[$ix], substr $str, $-[$ix], $+[$ix] - $-[$ix]; } } } say "Strawberry Perl $^V"; say $^O; __DATA__ output: $ix= 0 (1,2)<-> $ix= 1 (1,2)<-> $ix= 0 (4,5)<,> $ix= 2 (4,5)<,> Strawberry Perl v5.30.1 MSWin32

Questions

Magic in split

This split_e( // ); gives the warning: Use of uninitialized value $_ in pattern match (m//) at pm_1.pl ... but this split( // ); does not.

Is there some magic in split indicate by the ‘/’s in “/PATTERN/ in the documentation?

Bug in the variables @- and @+

I find that the result from split, m{} and when using $-[$ix] $+[$ix] are inconsistent.

Split gives (1, "-", undef, 10, undef , ",", 20).

m{} gives ("-", undef, undef, ",").

Using $-[$ix] $+[$ix] does not include the undefs

Is this a bug in the last case?

Replies are listed 'Best First'.
Re^2: Function Split, bug or error in the documentation?
by ikegami (Patriarch) on Jul 31, 2020 at 14:30 UTC

    split_e( // ), short for split_e( $_ =~ m// ), performs a match operation. You want split_e( qr// ).

    split is an operator. And like all operators, it has full control over the syntax of its operands. split /.../, ... is functionally equivalent to split qr/.../, .... Keep in mind that split predates qr//.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-25 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found