[comp.lang.perl] split litteral vs reg.exp.

usenet@carssdf.UUCP (John Watson) (03/20/90)

OK, so I'm a little dense, can one of you perl
'hackers' tell me the difference between
split('_'); and split(/_/);   When used on files
delimited by '_' like this:
First_Last_Address_City_State_Zip_etc...\n
Does it make any difference? Is one faster?
When would it make a difference?

John Watson   rutgers!carssdf!usenet

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (03/21/90)

In article <237@carssdf.UUCP> usenet@carssdf.UUCP (John Watson) writes:
: OK, so I'm a little dense, can one of you perl
: 'hackers' tell me the difference between
: split('_'); and split(/_/);   When used on files
: delimited by '_' like this:
: First_Last_Address_City_State_Zip_etc...\n
: Does it make any difference? Is one faster?
: When would it make a difference?

Semantically, there's no difference.  The /_/ form is preferred because
it will run faster.  Saying '_' works because it's an expression, so
it is evaluated at runtime.  Perl does recognize the specific case of
a single quoted string as something which won't change, so it doesn't
recompile the regular expression each time.  But it does miss out on
the optimization that happens on /_/ (which will bypass the regular
expression routines entirely, enabling a substantial speedup).

The one runtime pattern ' ' is a special case.  It actually gets translated
to /\s+/, with the additional feature of skipping leading whitespace like
awk does.

Larry