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

Monks:

It was my understanding that
$/ = undef;
will set the default delimeter to undefined (as opposed to \n). If it is, then my regex is not working like I thought it would.
#!/usr/bin/perl use strict; use warnings; local $/ = undef; my $info = <DATA>; while ( $info =~ /<tag>(.+?)<\/tag>/g ) { print "$1\n"; } __DATA__ <tag>This is a line</tag> <tag>This one goes over multiple lines</tag> __END__
Output:
This is a line

It's been a while since I've done anything beyond the fledgling-idiot level perl-wise, so it's likely I'm forgetting the basics. From what I read searching $/, I thought the above would work.

Replies are listed 'Best First'.
Re: $/ = undef question
by Zaxo (Archbishop) on Jun 28, 2004 at 04:13 UTC

    To match '.' with "\n", you need the /s modifier on your match operation. Regexen ignore $/ for that, it is only for delimiting "lines" in IO*. You're using it correctly for that.

    * Strictly speaking, $/ is the delimiter only for input.

    After Compline,
    Zaxo

      Fantastic -- that did it!
Re: $/ = undef question
by BUU (Prior) on Jun 28, 2004 at 07:38 UTC
    Not addressing your direct question, but since you seem to be trying to parse something that looks vaugely like XML, have you considered using a real parser?

      Indeed. Or even a simpler parser:

      #!/usr/bin/perl -l use strict; use warnings; use XML::Simple; my $xml = XMLin(\*DATA); print for @{ $xml->{tag} }; __DATA__ <xml> <tag>This is a line</tag> <tag>This one goes over multiple lines</tag> </xml>

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: $/ = undef question
by skyknight (Hermit) on Jun 28, 2004 at 13:13 UTC
    Just as an aside, it seems like you're using "local" as part of a religious adherance to habit. Specifying "local" when you're in the highest level scope possible isn't going to do anything useful, since when you leave this scope it is because your program is exiting. :-)