Gossamer Forum
Home : General : Perl Programming :

syntax question

Quote Reply
syntax question
i've searched my perl books but cannot find what this means:
split (/\Q$db_delim\E/o, $input)

what is \Q and \E ???
Quote Reply
Re: [delicia] syntax question In reply to
\Q -means anything after this will NOT be treated as a regex
\E - means that anything after this WILL be treated as a regex.

For example:

Code:
$var =~ s/\Q[my test one with square brackets]/[something to change to]/;

...would replace it fine

...but:

Code:
$var =~ s/[my test one with square brackets]/[something to change to]/;

...wouldn't work, as its trying to process [my test one with square brackets] as a list of charachters =)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [delicia] syntax question In reply to
Looks like Andy already answered this, but here's more info:

It does the equivalent of:
Code:
$db_delim = quotemeta($db_delim);
split(/$db_delim/o, $input);
which just backslashes all non-word characters. It's normally used so that characters in the string that are special characters in regexes aren't used inappropriately.

Adrian

Last edited by:

brewt: Mar 18, 2009, 1:25 AM
Quote Reply
Re: [brewt] syntax question In reply to
Quote:
which just backslashes all non-word characters

I think PHP uses a more logical version whereby only meta characters are replaced, hence the name quotemeta, and not all non-word characters like Perl.

Meta characters are:

. \ + * ? [ ^ ] ( $ )

Last edited by:

Wychwood: Mar 19, 2009, 3:35 AM
Quote Reply
Re: [Andy] syntax question In reply to
andy, when you say it "wouldn't work" do you mean it would give a syntax error, or that it wouldn't make the substitution? and was your example supposed to change the square brackets as well as the text? i guess by this i mean, it would only match the text if the text was enclosed in square brackets? syntax is making my head hurt!
Quote Reply
Re: [delicia] syntax question In reply to
Hi,

As Wychwood said, if you used one of these values in $db_delim, it would break your code:

. \ + * ? [ ^ ] ( $ )

...as they would be interpreted literally.

You would always just test it LOL

Code:
#!/usr/bin/perl

my $db_delim ='\';

my @values = split (/\Q$db_delim\E/o, qq|just testing this|);

#my @values = split (/$db_delim/o, qq|just testing this|); # without \Q and \E

etc

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!