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

Hi,

I'm working on a project which uses another guy's code and he had all warnings off so I've been trying to clean up his code in order to get a clean compile. I've managed to clean up all but two lines of code, which are being used in a peculiar way.

The code is as follows:

$var_cellsplit=split( //. $var_cellname ); $var_cdrcellsplit=split( //. $var_cdrcellname );
Now what he's trying to do is to get the length of $var_cellname and $var_cdrcellname by splitting into elements and counting the elements. It works, but it returns the error shown above.

Is there a way to make this cleaner? I'm assuming he went this way for a reason instead of using length().

Thanks,
Alatar

*EDIT* Roy and Dave, thanks for the explanation. The . was simply a typo as I'm accessing the web from a different PC than where I have the Code. I'm pretty sure now that he was doing this because he wasn't aware of the length() function.

Replies are listed 'Best First'.
Re: Use of implicit split to @_ is deprecated
by davido (Cardinal) on Jun 04, 2004 at 15:32 UTC
    The reason for this warning is that split is creating a list in @_, and then you're evaluating that @_ array in scalar context. This has the effect of counting @_'s elements, but has the side effect of wiping out whatever happened to be in @_ beforehand. Because this is a common cause of grief for people who didn't realize they're clobbering a subroutine's parameter list, a warning is issued to alert the programmer that (s)he may be creating a hidden bug. Moreover, the entire notion of splitting into @_, because of its inherent risks, has been deprecated (discouraged... dis-recommended).

    The obvious solution is to not use some contrived solution like this when Perl has a perfectly good length function. What we don't know from the code snippet you provided is if the original code actually relies on @_ containing the results of the split.

    You've got one other serious problem, and that's the use of a dot (concatenation operator) where you probably intended to use a comma (list operator). Because your code has a dot where a comma belongs, Perl silently translates what you've got there into the following (use "perl -MO=Deparse scriptname.pl" if you don't believe me):

    $var_cellsplit = split( ( // . $var_cellname ), $_, 0 );

    Why this isn't a syntax error is a little perplexing, but the result is that your "split" regexp looks pretty funky, and instead of splitting $var_cellname, you're splitting the contents of $_. This is a BIG hidden bug.


    Dave

Re: Use of implicit split to @_ is deprecated
by pbeckingham (Parson) on Jun 04, 2004 at 15:12 UTC

    How about:

    my $var_cell = length $var_cellname; my $var_cdr = length $var_cdrcellname;
    Are there any encoding deviations from good 'ole iso-8859-1?

Re: Use of implicit split to @_ is deprecated
by Roy Johnson (Monsignor) on Jun 04, 2004 at 15:14 UTC
    You've got dots here, where there should be commas. The obvious cleanup is to use length(), and I can't think of any reason he didn't. Another option would be to use tr///c.

    You could put no warnings 'uninitialized' in the block around this bit of code, or you could make it $var_cdrcellsplit = @{[split(...)]} if you want to retain the split for some reason.

    Update: or $var_cdrcellsplit = do { my @f = split(...) };
    or $var_cdrcellsplit = () = split(//, $var_cdrcellname, -1)
    Update 2: got careless. The latter is the same as using //g, which will match an extra time.
    Update 3: $var_cdrcellsplit = () = split(/(?<!^)(?!$)/, $var_cdrcellname, -1)


    The PerlMonk tr/// Advocate
      Update: or $var_cdrcellsplit = do { my @f = split(...) }; or $var_cdrcellsplit = () = split(//, $var_cdrcellname, -1)

      They are not equivalent. And you also may expect that leaving the -1 off doesn't make a difference. However, try to guess the output of the following program:

      #!/usr/bin/perl use strict; use warnings; my $str = "foobar"; my $c1 = do {my @f = split // => $str}; my $c2 = () = split // => $str => -1; my $c3 = () = split // => $str; print "$c1 $c2 $c3\n"; __END__
      Answer after the <readmore> tag.

      Abigail

Re: Use of implicit split to @_ is deprecated
by Abigail-II (Bishop) on Jun 04, 2004 at 15:13 UTC
    Is there a way to make this cleaner? I'm assuming he went this way for a reason instead of using length().

    If there's a hidden reason for doing it this way, how should be know whether there's a cleaner way? Any other way may break the hidden reason.

    I've managed to clean up all but two lines of code, which are being used in a peculiar way.
    Well, if you have cleaned up all but two lines of the code, you should know whether there's reason the previous coder did it the way he did.

    I'd just use length.

    Abigail