Switch.pm affecting < > operator

P

Paul Lalli

Greetings.

I'm noticing some bizzare behavior when use'ing the Switch.pm module.
It seems to be affecting how the < > operator behaves when the
filehandle enclosed is stored in a lexical reference. The behavior is
seen using perl v5.8.0 on solaris, with Switch.pm v2.09. An example:

#!/usr/bin/perl
use Switch;
use strict;
use warnings;

open my $fh, '<', $ARGV[0] or die "Cannot open $ARGV[0]: $!";
while (my $line = <$fh>){
print "Read: $line";
}
close $fh;

my $foo = 1;
switch ($foo){
case 1{
print "foo =1\n";
}
case 2 {
print "foo = 2\n";
}
}
__END__

Run this script passing any text file on the command line. (I cannot
post a self-contained example using __DATA__, as you'll see in a
moment). The output is not the lines of the file, but rather a GLOB
reference:

Read: GLOB(0x2496c)foo =1

Running the example through B::Deparse shows the reason for the odd
output:

use Switch;
BEGIN {${^WARNING_BITS} = "UUUUUUUUUUUU"}
use strict 'refs';
die "Cannot open $ARGV[0]: $!" unless open my $fh, '<', $ARGV[0];
use File::Glob ();
while (defined(my $line = glob(' ' . $fh))) {
print "Read: $line";
}
close $fh;
[snip remaining code]

Why would C<<$fh>> be treated as C<glob(' ' . $fh)> rather than
C<readline($fh)>?

The example works fine when the use Switch; line and switch($foo) block
are all commented out. It will manifest the problem if the switch block
is commented out (either through POD blocks or multiple #'s) but use
Switch; remains. It will NOT manifest the problem if the switch block
is actually removed from the file.

If we change the example to use bareword filehandles instead of lexical
referenced filehandles, the problem does not manifest itself:

#!/usr/bin/perl
use Switch;
use strict;
use warnings;

open FH, '<', $ARGV[0] or die "Cannot open $ARGV[0]: $!";
while (my $line = <FH>){
print "Read: $line";
}
close FH;

my $foo = 1;
switch ($foo){
case 1{
print "foo =1\n";
}
case 2 {
print "foo = 2\n";
}
}

__END__

This code works fine.

Note that I do have access to another solaris system running perl 5.8.2
and Switch.pm v2.10, which does not manifest this issue, so I'm content
to call it a bug fix. The problem is that I have found no references to
this issue in the Switch.pm docs nor the perldelta's for 5.8.1 or 5.8.2.
Has anyone else ever seen this?

Thank you for your time,
Paul Lalli
 
A

A. Sinan Unur

I'm noticing some bizzare behavior when use'ing the Switch.pm module.
....
The problem is that I have found no references to this issue in the
Switch.pm docs nor the perldelta's for 5.8.1 or 5.8.2. Has anyone
else ever seen this?

Paul:

I have no clues to offer, but just the observation that your test script
ran as expected on my system, Win XPSP2 and

D:\Home\asu1\UseNet\clpmisc> perl -v

This is perl, v5.8.6 built for MSWin32-x86-multi-thread

Ditto for:

asu1:~ > perl -v
This is perl, v5.8.6 built for i386-freebsd-64int

Sorry, can't be of more help.

Sinan.
 
S

Steven Sommer

Greetings.

I'm noticing some bizzare behavior when use'ing the Switch.pm module.
It seems to be affecting how the < > operator behaves when the
filehandle enclosed is stored in a lexical reference. The behavior is
seen using perl v5.8.0 on solaris, with Switch.pm v2.09. An example:

Has anyone else ever seen this?

Thank you for your time,
Paul Lalli

I have also experienced seemingly random problems with Switch.pm,
using ActiveState Perl 5.8.6 on Win32. Sometimes the compiler will
produce an error message stating that it doesn't recognize "case,"
even though the code is valid. I can usually prevent this error
message by randomly inserting comment lines before the Switch
statement and recompiling, until the error finally goes away.
Sometimes I have to experiment to find the right place to put the
comments.

I have tried to create a small test file that reproduces this error,
but because there is an element of randomness in the problem, the
error goes away when I pare away the extraneous code. I suspect that
if I posted the unmodified code that exhibited the problem, it would
work fine when others tried it.

The problem is frustrating, but it occurs rarely enough that I haven't
yet given up on Switch.pm and replaced it with if ... elsif ... else
constructs. I have experienced this problem ever since version 5.8.0
(and I don't think I used Perl very much before that).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top