Unknown Warning

Paul Fenwick pjf at perltraining.com.au
Mon Oct 21 00:18:45 CDT 2002

G'day Scotty / Mongers,

On Mon, Oct 21, 2002 at 02:56:30PM +1000, Scott Penrose wrote:

> for (my $i = scalar(split(/ /, $fred)); $i > 0; $i--) {
>         print $i . "\n";
> }


> a) A bug (5.6 and 5.8 do the same thing)
> b) Something special about 'scalar'

Split in a scalar context will split into @_ (Camel, first ed, p185).
In Perl 4, using ?? as pattern delimiters would do the same thing in
array context.  Perl 5 will still clobber @_ if you use split in a
scalar context, but it's considered deprecated, hence the warning.
Clobbering @_ probably isn't what you want, anyway.

If you're splitting on a single character, you can use tr/// to
count the occurances of that character, which will be faster than

If you're using a more complex pattern, you can force into an array
context, but the return into a scalar:

	my $i = @{[split(/ /, $fred)]};

I'm sure there are other solutions using regexpes.  TMTOWTDI.



Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

More information about the Melbourne-pm mailing list