Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > switch case (/regex/) ... $1 - emtpy

Thread Tools

switch case (/regex/) ... $1 - emtpy

Petr Sezemsky
Posts: n/a

Why using the regex in switch - case doesn't fill the $1, $2, ..
variables? This code works as I expect:

$_ = "hello world";

if (/he(ll)o/) {
print "OK: $1\n";

but the next doesn't work as the previous:

use Switch;
$_ = "hello world";

switch ($_) {
case (/he(ll)o/) {
print "OK: $1\n";

Thank you for explanation the problem.

Reply With Quote
Uri Guttman
Posts: n/a
>>>>> "PS" == Petr Sezemsky <(E-Mail Removed)> writes:

PS> Why using the regex in switch - case doesn't fill the $1, $2, ..
PS> variables? This code works as I expect:

PS> #!/usr/bin/perl
PS> use Switch;

that module does source filtering. the actual code that compiles is not
what you put in the file. and because of that it is not recommended to
ever use that module.

PS> $_ = "hello world";

PS> switch ($_) {
PS> case (/he(ll)o/) {
PS> print "OK: $1\n";

you can't easily tell what the code is there and whether the regex is
run just before your print. it appears how you like it but it isn't

perl 5.10 has given/when built in (taken from perl6) which is a proper
switch statement and it supports smart matching as well. this works as
you expect:

perl -le 'use feature ":5.10"; given ("bar") { when( /(a)/ ) { print "$1\n" } }'

see the docs in perlsyn for the full syntax.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post returns an emtpy even if its currenet position is not atthe end js Python 2 04-22-2007 11:18 PM
[JSF] Blank out bean properties when assign HTML element is "emtpy" Christoph Dahlen Java 2 09-09-2006 07:48 AM
bridview doesn't appear if table is emtpy =?Utf-8?B?RGFiYmxlcg==?= ASP .Net 2 03-19-2006 08:44 PM
Can RangeValidator Chek emtpy value ad ASP .Net 1 07-15-2005 11:19 AM
Re: Olympus C-700 "battery emtpy" problem J?rg Preddimann Digital Photography 5 07-23-2004 03:09 AM