A doubt about m//'s related variables

M

Michele Dondi

I have a minor doubt about predefined variables related to pattern
matching: in particular is it safe to do something like this?

if ( /\{([\w:]+)\}/ ) {
do_something($`, $', split /:/, $1);
}

I mean: are $`, $' and $1 those coming from the
*first* pattern matching in all cases?


Michele
--
#!/usr/bin/perl -lp
BEGIN{*ARGV=do{open $_,q,<,,\$/;$_}}s z^z seek DATA,11,$[;($,
=ucfirst<DATA>)=~s x .*x q^~ZEX69l^^q,^2$;][@,xe.$, zex,s e1e
q 1~BEER XX1^q~4761rA67thb ~eex ,s aba m,P..,,substr$&,$.,age
__END__
 
J

Jeff 'japhy' Pinyan

I have a minor doubt about predefined variables related to pattern
matching: in particular is it safe to do something like this?

if ( /\{([\w:]+)\}/ ) {
do_something($`, $', split /:/, $1);
}

I mean: are $`, $' and $1 those coming from the
*first* pattern matching in all cases?

You could have determined that on your own with testing. As it stands,
yes they are. I'm a little surprised about $` and $', but that's because
split() doesn't affect them. Had you done:

if ( /\{([\w:]+)\}/ ) {
do_something($`, $', $1 =~ /[^:]*/g);
}

you would get rather unexpected results.

I also advise against using $` and $' and $& if you can avoid them.

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
RPI Corporation Secretary % have long ago been overpaid?
http://japhy.perlmonk.org/ %
http://www.perlmonks.org/ % -- Meister Eckhart
 
M

Michele Dondi

You could have determined that on your own with testing. As it stands,

Yes, indeed I could have! :p

But for once my judgement was to better express my laziness asking on
clpmisc...
yes they are. I'm a little surprised about $` and $', but that's because
split() doesn't affect them. Had you done:

if ( /\{([\w:]+)\}/ ) {
do_something($`, $', $1 =~ /[^:]*/g);
}

you would get rather unexpected results.

Well, in this case I'd say "expected unexpected results"!
I also advise against using $` and $' and $& if you can avoid them.

Actually I can hardly remember having used them in more than two or
three cases before. But in a particular script I have, it happens that
I "have to", for a particularly relaxed acceptation of "to have to",
i.e. as it is now, I'm not using them (an the script does work), or
better, I'm using them in an undoubtedly safe form, but to use them as
hinted above would really come handy.


Michele
 
B

Brian McCauley

Jeff 'japhy' Pinyan said:
I have a minor doubt about predefined variables related to pattern
matching: in particular is it safe to do something like this?

if ( /\{([\w:]+)\}/ ) {
do_something($`, $', split /:/, $1);
}

I mean: are $`, $' and $1 those coming from the
*first* pattern matching in all cases?

You could have determined that on your own with testing.

Actually that would only determine if they do in the current version
of Perl. I suspect it is _unlikely_ that split() will ever change the
values of $& et al but until and unless it get's put into the
documentation of split() I wouldn't want to rely on it.
I also advise against using $` and $' and $& if you can avoid them.

Seconded. I also advise against using any of the regex-related
variables as arguments to a subroutine without double quoting them to
avoid the undesirable aliasing.

sub do_something {
"Unexpected results" =~ /$/;
print shift,"\n";
}

"Expected results" =~ /$/;
print $`,"\n"; # Expected results
do_something($`); # Unexpected results
do_something("$`"); # Expected results

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top