| Author |
Pattern matching against a "use constant" character
|
|
| Bernard Cosell 2004-05-20, 1:32 pm |
| I have a bunch of constants [field separators] and I need to remove
them from various strings before format them up. I'm clearly not
_quite_ understanding how \Q and \E work -- I tried something like:
perl -we 'use constant a => "*"; $b = "\Q".a."\E"; $c = qr($b);
warn "xxx" =~ /$c/'
Quantifier follows nothing before HERE mark in regex m/* << HERE / at
-e line 1.
Is there some way to do this other than the rather clunky one of
assigning the constant to a variable and interpolating that variable
into the pattern? Tnx...
/Bernie\
| |
| Ben Morrow 2004-05-20, 2:31 pm |
|
Quoth Bernard Cosell <bernie@rev.net>:
> I have a bunch of constants [field separators] and I need to remove
> them from various strings before format them up. I'm clearly not
> _quite_ understanding how \Q and \E work -- I tried something like:
>
> perl -we 'use constant a => "*"; $b = "\Q".a."\E"; $c = qr($b);
> warn "xxx" =~ /$c/'
> Quantifier follows nothing before HERE mark in regex m/* << HERE / at
> -e line 1.
\Q etc. only quote things that follow them inside a literal string: so
this works
$c = qr(\Q*\E);
but what you have doesn't. You are looking for quotemeta:
perl -Mconstant='a,*' -we'$b = quotemeta a; $c = qr/$b/;
warn "xxx" =~ $c'
Ben
--
The cosmos, at best, is like a rubbish heap scattered at random.
- Heraclitus
ben@morrow.me.uk
| |
| Dan Wilga 2004-05-21, 12:34 pm |
| Here's a related question. Assume the following snippet:
use constant MAGIC => 'FOO';
and I want to do:
/FOObar/;
using this constant, instead of the literal 'FOO' in the pattern. Can
this be done?
--
Dan Wilga dwilga-MUNGE@mtholyoke.edu
** Remove the -MUNGE in my address to reply **
| |
| Brian McCauley 2004-05-21, 1:31 pm |
| Dan Wilga <dwilga-MUNGE@mtholyoke.edu> writes:
> Here's a related question.
Related to what? You quote no context. Have you seen the posting
guidelines that are posted frequently?
> Assume the following snippet:
>
> use constant MAGIC => 'FOO';
>
> and I want to do:
>
> /FOObar/;
>
> using this constant, instead of the literal 'FOO' in the pattern. Can
> this be done?
Constants declared via 'use constant' are functions so although it may
not be immediately obvious[1] this is, in fact, FAQ: "How do I expand
function calls in a string?".
/@{[MAGIC]}bar/
Unfortunately perl does not spot that this interpolation can be done
at compile time.
[1] The fact that the FAQ uses the word 'expand' rather than the more
obvious word 'interpolate' does not help either.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
| |
| Uri Guttman 2004-05-21, 1:31 pm |
| >>>>> "DW" == Dan Wilga <dwilga-MUNGE@mtholyoke.edu> writes:
DW> Here's a related question. Assume the following snippet:
DW> use constant MAGIC => 'FOO';
DW> and I want to do:
DW> /FOObar/;
DW> using this constant, instead of the literal 'FOO' in the pattern. Can
DW> this be done?
a few ways but none are particularly great. perl6 has $() as the
scalar() equivilent and it can be interpolated with any expression
including constants inside (though the whole constant pragma will
probably go as perl6 supports a readonly trait).
so in perl5 you can use a temp variable:
my $foo = FOO ;
/${foo}bar/ ;
or
/${\FOO}bar/ ;
or there is an interpolate.pm module that supports $() but i don't know
it well.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Ben Morrow 2004-05-21, 1:31 pm |
|
Quoth Dan Wilga <dwilga-MUNGE@mtholyoke.edu>:
> Here's a related question. Assume the following snippet:
>
> use constant MAGIC => 'FOO';
>
> and I want to do:
>
> /FOObar/;
>
> using this constant, instead of the literal 'FOO' in the pattern. Can
> this be done?
Of course :). TMTOWTDI:
my $magic = MAGIV;
/${foo}bar/;
/@{[MAGIC]}bar/;
$_ =~ MAGIC;
Ben
--
I must not fear. Fear is the mind-killer. I will face my fear and
I will let it pass through me. When the fear is gone there will be
nothing. Only I will remain.
ben@morrow.me.uk Frank Herbert, 'Dune'
| |
| Anno Siegel 2004-05-23, 1:37 pm |
| Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
>
> DW> Here's a related question. Assume the following snippet:
> DW> use constant MAGIC => 'FOO';
>
> DW> and I want to do:
>
> DW> /FOObar/;
>
> DW> using this constant, instead of the literal 'FOO' in the pattern. Can
> DW> this be done?
>
> a few ways but none are particularly great. perl6 has $() as the
> scalar() equivilent and it can be interpolated with any expression
> including constants inside (though the whole constant pragma will
> probably go as perl6 supports a readonly trait).
>
> so in perl5 you can use a temp variable:
>
> my $foo = FOO ;
> /${foo}bar/ ;
>
> or
> /${\FOO}bar/ ;
>
> or there is an interpolate.pm module that supports $() but i don't know
> it well.
You could do
use constant MAGIC => \ 'FOO';
/${+MAGIC}bar/;
....and come close, if it weren't for the pesky "+". Of course, then
you'd have to say ${+MAGIC} each time you need the bare string, so if
you need the bare string often, that's out too.
It is the big drawback in Perl's constants that they don't interpolate.
That goes for anything double-quotish, not only regexen, and I think
that's why their use has never become a matter of course.
Anno
|
|
|
|