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

For some reason, I'm having a bit of trouble with this one. I just want to find out the easy way to calculate the number of bits of stuff that split will return, preferably without actually returning them or coughing up the following complaint:
Use of implicit split to @_ is deprecated at Foo.pm line XIV.
Here's a bit of code that causes that particular problem:
my $foo = split (/,/, $bar);
It would seem that since the split() call is being assigned to something that the default behaviour would not kick in, yet this is clearly not the case.

One way around this is to avoid that function and use only the required regex, which seems to Benchmark at least 5 times faster than any equivalent split:
my $foo = ($bar =~ /,/) + 1;
Yet this is only an approximation, considering it erroneously returns 1 when $bar is empty. So, more formally:
my $foo = defined($bar) && length($bar) && (($bar =~ /,/) + 1);
Yet, this does seem to be going to quite the trouble just to get a simple answer.

Any ideas?

Replies are listed 'Best First'.
Re: Idiomatic Split to Scalar Conversion
by jmcnamara (Monsignor) on Apr 29, 2002 at 22:47 UTC

    Perlfunc on split says that "If not in list context ... splits into the @_ array". So I guess that the warning is valid.

    If you are just counting you could do:     my $foo = $bar =~ tr/,/,/ + 1; To stick with split you could do this:

    my $foo = my @a = split /,/, $bar; # Or this $foo++ for split /,/, $bar; # But why doesn't this work? my $foo = () = split /,/, $bar;

    --
    John.

      > # But why doesn't this work? > my $foo = () = split /,/, $bar;

      Because split is DWIM-happy (or DWYTIM -- Do What You Think I Mean), and it's told the length of the array it is assigning into so it can stop looking after it fills your array.

      my $foo = () = split(/,/, $bar, -1) almost works, but it doesn't strip trailing undefs like it would in list context without the third param (try it with "a,b,,").

      If you don't mind wiping out @_, then just be explicit about it:

      my $foo = @_ = split /,/, $bar

        interestingly enough,

        my $foo = @{[]} = split( /,/, $bar );
        seems to work ( and this one's strict- and warnings-happy :-)

        ~Particle *accelerates*

Re: Idiomatic Split to Scalar Conversion
by japhy (Canon) on Apr 29, 2002 at 22:46 UTC
    You could use $count = $str =~ s/(pat)/$1/g, although that matches and replaces uselessly.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a (from-home) job
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Idiomatic Split to Scalar Conversion
by stephen (Priest) on Apr 29, 2002 at 22:59 UTC
    my $foo = @{ [ split(/,/, "foo,bar,baz") ] };

    stephen

Re: Idiomatic Split to Scalar Conversion
by samtregar (Abbot) on Apr 29, 2002 at 22:50 UTC
    You want to count the occurences of a character in a string, right? How about:

    my $count = $string =~ tr/,//;

    -sam

Re: Idiomatic Split to Scalar Conversion
by particle (Vicar) on Apr 29, 2002 at 22:43 UTC
    $foo=( split(/,/, $bar) ); is one way to do it.

    ~Particle *accelerates*

      Try that with '-w' and you get warned. No fun, that.

      What I meant by idiomatic was "better, faster, cheaper" in terms of clarity, execution speed, and number of variables. As in, could be used in this particular circumstance with great confidence.

      The return value has to be the same as an equivalent split call, not just close, otherwise it's not really the same.