in reply to substitution (s///)

Whether you want to perform substitution on $line or $name is unclear (it is probably $name though), but the substitution expects the $_ variable to be initialized.

When you use s/// (and many other Perl constructs) in "void context" - w/o arguments, it assumes the $_ var is its argument. The regext in the if() doesn't insert anything into $_, so you get the warning.

It's best to be explicit... supply s/// with the var you want the substitution on, then you won't have to scratch your head for too long trying to understand bugs like this.

Replies are listed 'Best First'.
Re: Re: substitution (s///)
by djantzen (Priest) on Dec 02, 2002 at 12:57 UTC

    A nitpick: context refers to the calling environment and how any return values will be assigned. If those results are not utilized to populate a single variable (scalar context) or a list of variables (list context) but are merely tossed away, then the context is void (cf. wantarray).

    In this case, the substitution does occur in void context, however that's not why $_ is assumed to be the target; rather it's because the subroutine doesn't explicitly tell s/// what to operate on and thus perl fills that value in for you.