Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A doubt about m//'s related variables

Reply
Thread Tools

A doubt about m//'s related variables

 
 
Michele Dondi
Guest
Posts: n/a
 
      07-07-2004
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__
 
Reply With Quote
 
 
 
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      07-07-2004
On Wed, 7 Jul 2004, Michele Dondi wrote:

>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


 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      07-08-2004
On Wed, 7 Jul 2004 16:38:17 -0400, Jeff 'japhy' Pinyan
<> wrote:

>>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, 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
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      07-09-2004
Jeff 'japhy' Pinyan <> writes:

> On Wed, 7 Jul 2004, Michele Dondi wrote:
>
> >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\\
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
Defining variable in C header file related doubt whirlwindkevin C Programming 8 06-11-2008 09:09 AM
doubt related to static library sam_cit@yahoo.co.in C Programming 2 04-25-2007 01:04 PM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
doubt related to string pointers. junky_fellow@yahoo.co.in C Programming 8 05-22-2005 12:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57