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

Re: Re: Make things a *little* easier

by John M. Dlugosz (Monsignor)
on Sep 12, 2002 at 14:49 UTC ( #197238=note: print w/replies, xml ) Need Help??


in reply to Re: Make things a *little* easier
in thread a farewell to chop

the advantage this has over the 4-arg substr is that you don't need to put the number of chars to replace in the string (which would always be the absolute value of the negative offset in situations like this);

I noticed that. If you leave off the argument, it automatically takes the rest of the chars to the end. If you want to give a 4th argument, you can't leave off the 3rd.

But, shouldn't passing undef for the 3rd argument mean the same thing as leaving it off? That's what I would expect.

Replies are listed 'Best First'.
Re: Re: Re: Make things a *little* easier
by mdillon (Priest) on Sep 12, 2002 at 15:37 UTC
    Yes. Using undef as the third argument does have that effect. I suspect Perl 6 will improve on this somewhat by being able to do: substr $str, -1, replace => '';
    This may not seem like much of an improvement, but it gets rid of the undef, which could possibly be mysterious to the uninitiated (as could lvalue substr).

    Update: yikes! Using undef doesn't actually work like this (i.e. as the third arg in a Perl-5 substr). I had assumed it did, but it actually acts the same as using 0 as the third arg (at least with 5.8.0). Anyways, the lvalue behavior is stable and documented since 5.6 (possibly earlier); if undef works like this in any Perl version, I haven't seen it documented. Sorry for the foolish mistake (and for the unfounded assumption).

Re^3: Make things a *little* easier
by Aristotle (Chancellor) on Sep 12, 2002 at 16:06 UTC
    Now that would be something I'd like to see in Perl 6 (and which I have missed in the Perl 5 version of the function on a number of occasions).

    Makeshifts last the longest.

      You seem to be getting different results than mdillon. That's the problem with relying on undocumented details. If you write a sub starting with my ($a,$b,$c,$d)=@_; and then calculate defaults if undef or use the undef value for some special meaning, then leaving off args naturally gives the same results. But if you write if (scalar(@_) < 4) ... to do something different, then using undef doesn't give the same effect.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2019-07-24 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (32 votes). Check out past polls.

    Notices?