Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Q: // and "magic"

Reply
Thread Tools

Q: // and "magic"

 
 
J Krugman
Guest
Posts: n/a
 
      04-05-2005


In perlre I found these puzzling lines:

@chars = split //, $string; # // is not magic in split
($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /

I don't understand the comments. What's all the "magic" about?

In an attempt to understand the first comment, I consulted perldoc
-f split, which made matters worse. I found no mention at all of
"magic", but I came across this:

Using the empty pattern "//" specifically matches
the null string, and is not be confused with the
use of "//" to mean "the last successful pattern
match".

Now I'm hopelessly confused. I understand that "//" matches the
null string, but I have no idea what the last sentence above (about
the "other" use of "//") is talking about. Any help sorting this
out would be greatly appreciated.

TIA!

jill



--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-05-2005
J Krugman <> wrote in news:d2u5j6$a4j$1
@reader1.panix.com:

> In perlre I found these puzzling lines:
>
> @chars = split //, $string; # // is not magic in split
> ($whitewashed = $string) =~ s/()/ /g; # parens avoid magic s// /
>
> I don't understand the comments. What's all the "magic" about?
>
> In an attempt to understand the first comment, I consulted perldoc
> -f split, which made matters worse. I found no mention at all of
> "magic", but I came across this:
>
> Using the empty pattern "//" specifically matches
> the null string, and is not be confused with the
> use of "//" to mean "the last successful pattern
> match".
>
> Now I'm hopelessly confused. I understand that "//" matches the
> null string, but I have no idea what the last sentence above (about
> the "other" use of "//") is talking about.


In the context of the split function, // matches the empty string.

Elsewhere, // means the last successful pattern match.

IMHO, the passage above is very clear, but here is the relevant section
from perldoc perlop (where m// is being discussed):

If the PATTERN evaluates to the empty string, the last
*successfully* matched regular expression is used instead. In
this case, only the "g" and "c" flags on the empty pattern is
honoured - the other flags are taken from the original pattern.
If no match has previously succeeded, this will (silently) act
instead as a genuine empty pattern (which will always match).

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-05-2005
"A. Sinan Unur" <> wrote:
>
> In the context of the split function, // matches the empty string.
>
> Elsewhere, // means the last successful pattern match.
>
> IMHO, the passage above is very clear, but here is the relevant section
> from perldoc perlop (where m// is being discussed):
>
> If the PATTERN evaluates to the empty string, the last
> *successfully* matched regular expression is used instead. In
> this case, only the "g" and "c" flags on the empty pattern is
> honoured - the other flags are taken from the original pattern.
> If no match has previously succeeded, this will (silently) act
> instead as a genuine empty pattern (which will always match).


So, does anyone find this behavior useful? I've never intentionally used
it, and I can't imagine doing so in the future.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-05-2005
wrote in news:20050405125614.353$:

> "A. Sinan Unur" <> wrote:
>>
>> In the context of the split function, // matches the empty string.
>>
>> Elsewhere, // means the last successful pattern match.
>>
>> IMHO, the passage above is very clear, but here is the relevant
>> section from perldoc perlop (where m// is being discussed):
>>
>> If the PATTERN evaluates to the empty string, the last
>> *successfully* matched regular expression is used instead. In
>> this case, only the "g" and "c" flags on the empty pattern is
>> honoured - the other flags are taken from the original pattern.
>> If no match has previously succeeded, this will (silently) act
>> instead as a genuine empty pattern (which will always match).

>
> So, does anyone find this behavior useful? I've never intentionally
> used it, and I can't imagine doing so in the future.


At the risk of sounding like an AOLer, I am curious as well. I tried
thinking of a way to use this feature. Couldn't think of anything, but
that is probably a reflection of my limitations

I have a feeling Abigail might contribute some magic.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      04-05-2005
A. Sinan Unur wrote:

> wrote in news:20050405125614.353$:
>
>> "A. Sinan Unur" <> wrote:
>>>
>>> In the context of the split function, // matches the empty string.
>>>
>>> Elsewhere, // means the last successful pattern match.
>>>
>>> IMHO, the passage above is very clear, but here is the relevant
>>> section from perldoc perlop (where m// is being discussed):
>>>
>>> If the PATTERN evaluates to the empty string, the last
>>> *successfully* matched regular expression is used instead. In
>>> this case, only the "g" and "c" flags on the empty pattern is
>>> honoured - the other flags are taken from the original pattern.
>>> If no match has previously succeeded, this will (silently) act
>>> instead as a genuine empty pattern (which will always match).

>>
>> So, does anyone find this behavior useful? I've never intentionally
>> used it, and I can't imagine doing so in the future.

>
> At the risk of sounding like an AOLer, I am curious as well. I tried
> thinking of a way to use this feature. Couldn't think of anything, but
> that is probably a reflection of my limitations
>
> I have a feeling Abigail might contribute some magic.
>

If you have an "untaint this" regexp, you might wind up using several
times in a row on several variables. But mostly, I think this was
Larry getting a little overenthusiastic in "save the programmer
keystrokes" mode. And once it was around for awhile, of course it
couldn't be taken out because it would break stuff.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-05-2005
wrote in news:20050405125614.353$:

> "A. Sinan Unur" <> wrote:
>>
>> In the context of the split function, // matches the empty string.
>>
>> Elsewhere, // means the last successful pattern match.
>>
>> IMHO, the passage above is very clear, but here is the relevant
>> section from perldoc perlop (where m// is being discussed):
>>
>> If the PATTERN evaluates to the empty string, the last
>> *successfully* matched regular expression is used instead. In
>> this case, only the "g" and "c" flags on the empty pattern is
>> honoured - the other flags are taken from the original pattern.
>> If no match has previously succeeded, this will (silently) act
>> instead as a genuine empty pattern (which will always match).

>
> So, does anyone find this behavior useful? I've never intentionally
> used it, and I can't imagine doing so in the future.


I can think of one situation where this feature might be useful.

Consider the following:

#! perl

use strict;
use warnings;

my $s = 'one two three onetwo three one two three four';
my %count;

if( $s =~ /\b(one)\b/ or $s =~ /\b(two)\b/ ) {
++$count{$1} while( $s =~ //g );
}
__END__

Here, I am interested in counting the number of times the word 'one' in
the text. If there are no 'one's, then I want to count the number of
times 'two' occurs.

I think this is the most succint way of expressing the intent above. I
do not know if it would offer any speed advantages over other methods of
doing the same thing.

The construct might allow the programmer to more naturally avoid
alternation in regular expressions in favor of or tests in the
conditional and that might result in a performance benefit as well.

All this is speculation, however.

Sinan

--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
J Krugman
Guest
Posts: n/a
 
      04-05-2005
In <20050405125614.353$> writes:

>"A. Sinan Unur" <> wrote:
>>
>> In the context of the split function, // matches the empty string.
>>
>> Elsewhere, // means the last successful pattern match.
>>
>> IMHO, the passage above is very clear, but here is the relevant section
>> from perldoc perlop (where m// is being discussed):
>>
>> If the PATTERN evaluates to the empty string, the last
>> *successfully* matched regular expression is used instead. In
>> this case, only the "g" and "c" flags on the empty pattern is
>> honoured - the other flags are taken from the original pattern.
>> If no match has previously succeeded, this will (silently) act
>> instead as a genuine empty pattern (which will always match).


>So, does anyone find this behavior useful? I've never intentionally used
>it, and I can't imagine doing so in the future.


I think this may have something to do with my confusion: I have
never seen // used in any situation in which it wasn't clearly
intended to match the empty string, as in split //, ... . It would
be great to se a meaningful example.

jill
--
To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      04-05-2005
Chris Mattern <> wrote in comp.lang.perl.misc:
> A. Sinan Unur wrote:
>
> > wrote in news:20050405125614.353$:
> >
> >> "A. Sinan Unur" <> wrote:
> >>>
> >>> In the context of the split function, // matches the empty string.
> >>>
> >>> Elsewhere, // means the last successful pattern match.
> >>>
> >>> IMHO, the passage above is very clear, but here is the relevant
> >>> section from perldoc perlop (where m// is being discussed):
> >>>
> >>> If the PATTERN evaluates to the empty string, the last
> >>> *successfully* matched regular expression is used instead. In
> >>> this case, only the "g" and "c" flags on the empty pattern is
> >>> honoured - the other flags are taken from the original pattern.
> >>> If no match has previously succeeded, this will (silently) act
> >>> instead as a genuine empty pattern (which will always match).
> >>
> >> So, does anyone find this behavior useful? I've never intentionally
> >> used it, and I can't imagine doing so in the future.

> >
> > At the risk of sounding like an AOLer, I am curious as well. I tried
> > thinking of a way to use this feature. Couldn't think of anything, but
> > that is probably a reflection of my limitations
> >
> > I have a feeling Abigail might contribute some magic.
> >

> If you have an "untaint this" regexp, you might wind up using several
> times in a row on several variables. But mostly, I think this was
> Larry getting a little overenthusiastic in "save the programmer
> keystrokes" mode. And once it was around for awhile, of course it
> couldn't be taken out because it would break stuff.


I'm inclined to believe it was at some stage meant to be the last
successfully *compiled* regex that set //. That would make much more
sense as a keystroke-saver, though still somewhat obscure.

As it is, it could be used to choose a regex from a selection by
matching them against a test string (or more), then using //. There
are clearer, not much longer ways to do that, even without qr//.
It's a misfeature and no one uses it.

Anno
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      04-05-2005
wrote:
> So, does anyone find this behavior useful? I've never intentionally used
> it, and I can't imagine doing so in the future.


I have used it and have seen it used before, but only in the context of
Perl Golf to save some chars. Of course, in real production code, I
would strongly advise against using it since it can easily lead to
confusion and has no real advantage.

--Ala
 
Reply With Quote
 
Alex Hart
Guest
Posts: n/a
 
      04-06-2005
> So, does anyone find this behavior useful? I've never intentionally
used
> it, and I can't imagine doing so in the future.


I use this all the time.

This can be used instead of the "o" option. Meaning the regex will not
be recompiled each time perl sees it. If perl sees a string inside a
regex, it will recompile it each time, even if the string hasn't
changed. If you set the "o" option, then it is fixed for the whole
program, once it is compiled. Using // can avoid perl recompiling each
time, but the string can still change later.


Here's an example


sub Search { # search a list of names for a string
my ($string) = @_;
$string =~ /$string/i;
foreach (@list_of_names) {
if (//) {
push @found, $_;
}
}
}

Now, the regex is only compiled once each time the function is called.
With the "o" flag, running Search() twice would search for the same
string twice.

There are other ways to achieve the same thing, but I like //.

Hope that makes sense.

- Alex Hart

 
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
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 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