vladb has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

It's my first time playing with Text::Balanced module. From the code below (a quick test script I wrote..), shouldn't $next contain '#name#' after the call to extract_delimited() sub?

As of now, the subroutine simply sets $@ indicating this: "Not a delimited pattern, detected at offset 0".

My thinking was that the extract_delimited() sub would pull out 'first' delimited substring inside a given larger string. This is not the case, however ;-). So, for that matter, how do I do it using this module? I'm not sure which one of the imported methods I should use to achive the desired result (say, pull out next '#' delimited substring).
use Text::Balanced qw ( extract_delimited extract_bracketed extract_quotelike extract_codeblock extract_variable extract_tagged extract_multiple gen_delimited_pat gen_extract_tagged ); my $source = 'Hello World, #name#!'; my ($cfml_expr_match, $next); #eval { $cfml_expr_match = gen_delimited_pat(q{#},q{#}); $next = extract_delimited($source, '#', '', '#'); #}; if ($@) { print "Hmm.. got this: $@\n"; } # some code here... print "\n";


Note: I've uncommented eval{...} since it would clear the $@ variable, therefore, not letting me to check on the status of a call to a Text::Balanced method. Any idea why this is a case? Sorry for yet another loosely related question ;-).

"There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith

Edit kudra, 2002-01-10 Changed Delimited to Balanced in title per ntc request

Replies are listed 'Best First'.
(crazyinsomniac) Re: using Text::Delimited...
by crazyinsomniac (Prior) on Jan 05, 2002 at 11:33 UTC
    I think you misunderstood the pod. From what I understand, the snippet above would extract stuff like ##. I think this is the behaviour your want (even note the eval dealy. Since there is only one $@ variable, and all eval calls set it, when you got nested ones, you gotta check $@ inside your eval). Enjoy.
    #!/usr/bin/perl -w use strict; use Text::Balanced qw ( extract_delimited extract_bracketed extract_quotelike extract_codeblock extract_variable extract_tagged extract_multiple gen_delimited_pat gen_extract_tagged ); my $source = 'Hello World, #name#!'; my ($cfml_expr_match, $next); eval { $cfml_expr_match = gen_delimited_pat(q{#},q{#}); $next = extract_delimited($source, '#','[^#]+','#'); print "There is only one \$@, so Inside my eval, .. got this: $@\n" if + $@; }; if ($@) { print "Hmm.. got this: $@\n"; } else { print "CFML: $cfml_expr_match\n"; print "next: $next\n"; } # some code here... print "\n"; __END__ CFML: (?:\#(?:[^\#]*(?:(?:\#\#)[^\#]*)*)\#) next: #name# ## If I use your '#','','#' params, the error i'd get is inside my eval, Hmm.. got this: Not a delimited pattern, detected at o +ffset 0

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"