[ale] One for the perl hackers

JD jdp at algoloma.com
Thu Oct 3 17:57:22 EDT 2013


On 10/03/2013 05:22 PM, Robert L. Harris wrote:
> 
> *Sorry for the OT but my only perl lists have gone dark a long time ago*
> 
> 
> $String='component1,component2,"This is my, test string", component4';
> 
> ($C1, $C2, $Str, $C4) = split(',', $String);
> 
> I'm only getting "This is my" in $Str and $C4 does not contain "component4".  Is
> there a graceful way of handling this?  It's a 1 in 10000 chance it'll break but
> accuracy counts.

Doesn't escaping the separator character work?  Or you could swap the single and
double quotes around.  I didn't test it myself.  Why are their quotes around 1
element but not all the elements?  The quotes are supposed to be part of the
resulting $Str, right.  It is best to escape all non-standard characters inside
something like this anyway.  The only assumption to be made about the characters
is that 1 line will be read at a time and there should only be 3 commas and any
',' characters used **must** be escaped by the input tool.  If you have control
over the separator, either a {tab} or a '|' could be better choices unless all
strings are quoted.

Also, if I know there are 4 fields, I put that into the split call (3rd parameter).

There are lots of perl resources ... pm.org - search for "perl monger" or "perl
monks" for more.  "Modern Perl" is a great, free, book if you want to use ... er
... modern perl techniques. When I'm coding Perl, the O'Reilly Perl Bookshelf is
open in a 2nd window as reference for this sort of stuff.

Plus if you like the idea of Ruby on Rails, but just find Ruby to be a resource
hog and/or slow, then Perl has something called "Dancer" that is modeled after
the other web toolkits.  A single DB table can be CRUD-ified with less than 5
lines of code in Dancer.  Clearly, it won't be secure, no login, and only a
default interface will be provided, but still, it is possible to make an example
"blog" app that made RoR so very popular.


More information about the Ale mailing list