Mailing List Archive

split on a string
i believe if split's first argument is not a regexp, a warning should
be emitted.

------- Forwarded Message

Date: Wed, 25 Oct 95 08:44:27 PDT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: re: regular expression / split help
To: tchrist@mox.perl.com

Newsgroups: comp.lang.perl.misc
Subject: Re: [Help] Regular expression / split question
Summary:
Expires:
References: <465lsg$idg@dolphin.pst.cfmu.eurocontrol.be> <DGrA5u.7H@bcstec.ca.boeing.com> <46
jj06$hej@csnews.cs.colorado.edu>
Sender:
Followup-To:
Distribution:
Organization: The Boeing Company
Keywords:
Cc:

In article <46jj06$hej@csnews.cs.colorado.edu>,
Tom Christiansen <tchrist@mox.perl.com> wrote:
>:-> In comp.lang.perl.misc, ced@bcstec.ca.boeing.com (Charles DeRykus) writes:
>: @f = split("|");
>
>That's not going to work right. Split takes a regexp as its first argument:

>
> @f = split /\|/;
>
>mistakenly using a quoted string or a variable does *NOT*
>absolve you from the stringent requirement to escape things
>that would otherwise affect a regular expression, like a pipe
>symbol for pattern alternation.
>

Ooh, I was sloppy. You'd have to do something like:

@f = split "\\\|";

or

@f = split q{\|};

Not formatting split's 1st arg as a /PATTERN/ makes it easy
to forget you're dealing with a regexp and the need to escape
metacharacters... and it can grow ugly with backslashitis..

I see the light.

- -
Charles DeRykus
ced@carios2.ca.boeing.com





------- End of Forwarded Message
Re: split on a string [ In reply to ]
On Thu, 26 Oct 1995, Tom Christiansen wrote:

> i believe if split's first argument is not a regexp, a warning should
> be emitted.

What about split ' '? That's perfectly desirable and different from
split / /...

Mike

--
Mike Stok | The "`Stok' disclaimers" apply.
stok@pencom.com | Pencom Systems Administration (work)
stok@cybercom.net | Cyber Access (play)
http://www.cybercom.net/~stok/ | The inevitable WWW page (?)
Re: split on a string [ In reply to ]
>From: Tom Christiansen <tchrist@mox.perl.com>
>List-Name: perl5-porters
>
>i believe if split's first argument is not a regexp, a warning should
>be emitted.

I have always argued with larry over this, that a string instead of a RE
as the first argument of split should be use the string as the literal
separator and used index() instead of REs as the search method. Why
make something an error is there is a perfectly good meaning for it?

--
Mark Biggar
mab@wdl.loral.com
Re: split on a string [ In reply to ]
>On Thu, 26 Oct 1995, Tom Christiansen wrote:

>> i believe if split's first argument is not a regexp, a warning should
>> be emitted.

>What about split ' '? That's perfectly desirable and different from
>split / /...

then exempt it. the number of times i have to see and explain

split '.'
or
split '|'

being wrong is more than you can imagine.

--tom
Re: split on a string [ In reply to ]
: I forget exactly the subtle difference between ' ' ans / / but I imagine
: if the user supplied
:
: split '.' $foo;
:
: or
:
: split '|' $foo;
:
: this could be interpreted as
:
: split /\./ $foo;
:
: or
:
: split /\|/ $foo;

Okay, then how are you going to interpret this currently legal code?

split $pat, $foo;

At the moment it means the same thing as

split /$pat/, $foo;

We can't just up and break things like that...

Larry
Re: split on a string [ In reply to ]
: I forget exactly the subtle difference between ' ' ans / / but I imagine
: if the user supplied
:
: split '.' $foo;
:
: or
:
: split '|' $foo;
:
: this could be interpreted as
:
: split /\./ $foo;
:
: or
:
: split /\|/ $foo;

Okay, then how are you going to interpret this currently legal code?

split $pat, $foo;

At the moment it means the same thing as

split /$pat/, $foo;

We can't just up and break things like that...

Larry
Re: split on a string [ In reply to ]
>>On Thu, 26 Oct 1995, Tom Christiansen wrote:
>
>>> i believe if split's first argument is not a regexp, a warning should
>>> be emitted.
>
>>What about split ' '? That's perfectly desirable and different from
>>split / /...
>
>then exempt it. the number of times i have to see and explain
>
> split '.'
>or
> split '|'
>
>being wrong is more than you can imagine.
>
>--tom

A naive suggestion - what about making it legal just like ' ' is legal
and different from / /.

I forget exactly the subtle difference between ' ' ans / / but I imagine
if the user supplied

split '.' $foo;

or

split '|' $foo;

this could be interpreted as

split /\./ $foo;

or

split /\|/ $foo;

just an idea.

-GSM
Re: split on a string [ In reply to ]
>>On Thu, 26 Oct 1995, Tom Christiansen wrote:
>
>>> i believe if split's first argument is not a regexp, a warning should
>>> be emitted.
>
>>What about split ' '? That's perfectly desirable and different from
>>split / /...
>
>then exempt it. the number of times i have to see and explain
>
> split '.'
>or
> split '|'
>
>being wrong is more than you can imagine.
>
>--tom

A naive suggestion - what about making it legal just like ' ' is legal
and different from / /.

I forget exactly the subtle difference between ' ' ans / / but I imagine
if the user supplied

split '.' $foo;

or

split '|' $foo;

this could be interpreted as

split /\./ $foo;

or

split /\|/ $foo;

just an idea.

-GSM
Re: split on a string [ In reply to ]
On Thu, 26 Oct 1995, Larry Wall wrote:

> Okay, then how are you going to interpret this currently legal code?
>
> split $pat, $foo;
>
> At the moment it means the same thing as
>
> split /$pat/, $foo;
>
> We can't just up and break things like that...

I'm starting to feel an argument for having compiled regexps coming on...

> Larry


--
Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)
Re: split on a string [ In reply to ]
On Thu, 26 Oct 1995, Larry Wall wrote:

> Okay, then how are you going to interpret this currently legal code?
>
> split $pat, $foo;
>
> At the moment it means the same thing as
>
> split /$pat/, $foo;
>
> We can't just up and break things like that...

I'm starting to feel an argument for having compiled regexps coming on...

> Larry


--
Kenneth Albanowski (kjahds@kjahds.com, CIS: 70705,126)
Re: split on a string [ In reply to ]
Excerpts from the mail message of Larry Wall:
)
) Okay, then how are you going to interpret this currently legal code?
)
) split $pat, $foo;
)
) At the moment it means the same thing as
)
) split /$pat/, $foo;

Checking on this I find this in perlfunc.pod:
The pattern /PATTERN/ may be replaced with an
expression to specify patterns that vary at runtime.

But why is this stated? Does it provide any advantage over
simply using split(/$pat/,$foo) ?

I agree with others that splitting on a string, including
split($str,$foo), is often not meant to be the same as
split(/$str/,$foo), and so should be warned against.

This would allow a future change to have split($str,$foo) mean
split(/\Q$str/,$foo), which seems more correct though \Q does
make this less advantageous now [.and it would conflict a bit with
the existing split(' ') behavier]. With all of the those qualifiers
on the end of my previous sentence, I won't really miss such a
feature if it never gets added.

Perhaps there should be two steps, change the documentation to
discourage split($pat,$foo) then later add a warning for it.

Can we agree on that first step or is there an advantage I don't
see? [C, none of the above :]

) We can't just up and break things like that...
)
) Larry
--
Tye McQueen tye@metronet.com || tye@doober.usu.edu
Nothing is obvious unless you are overlooking something
http://www.metronet.com/~tye/ (scripts, links, nothing fancy)
Re: split on a string [ In reply to ]
>Okay, then how are you going to interpret this currently legal code?
>
> split $pat, $foo;
>
>At the moment it means the same thing as
>
> split /$pat/, $foo;
>
>We can't just up and break things like that...
>
>Larry

I knew there had to be some reason why some obvious functionality was
not being implemented. I imagine if one could go back in time, the
optimal choice might have been to require /'s around patterns and let
literals be literals.

No use wasting thinking about that. The transition from perl4 to perl5
has been fairly painless (except for my run in with '::') which is a
testament to the circumspect way in which improvements have been made.

-GSM
Re: split on a string [ In reply to ]
>Okay, then how are you going to interpret this currently legal code?
>
> split $pat, $foo;
>
>At the moment it means the same thing as
>
> split /$pat/, $foo;
>
>We can't just up and break things like that...
>
>Larry

I knew there had to be some reason why some obvious functionality was
not being implemented. I imagine if one could go back in time, the
optimal choice might have been to require /'s around patterns and let
literals be literals.

No use wasting thinking about that. The transition from perl4 to perl5
has been fairly painless (except for my run in with '::') which is a
testament to the circumspect way in which improvements have been made.

-GSM