V 5.8 specific problem?

M

Marty Landman

I'm running the same software on three different servers. All 3 are
FBSD running Perl v5.005_03. Recently I ported to a RH9 Linux server
which runs Perl v5.8.0. One part of the software is causing the
following in my Apache error log:

"Attempt to bless into a reference at..."

the referenced line contains

my $quiz = bless { _q => $_q, _a => $_a },$Quiz;

I've compared the copy on this machine and one of the 3 where it
works. Also if I just comment out the call that caused this package to
be used everything works just fine.

While it may obviously be something just not thought of by me (imagine
that) given the difference in Perl versions I'm wondering if this is a
feature or bug of the 5.8 release. Since the Linux box belongs to me
and is a sandbox I could just rpm -e Perl 5.8 and install 5.0x from a
cpan distro. But then again maybe it's something I need to pay better
attention to.

Any help, ideas etc.. most appreciated.

Thanks in advance,


Marty Landman Face 2 Interface Inc 845-679-9387
This Month's New Quiz --- Past Superbowl Winners
Make a Website: http://face2interface.com/Home/Demo.shtml
 
E

Eric Schwartz

I'm running the same software on three different servers. All 3 are
FBSD running Perl v5.005_03.

This is truly ancient (nearly 5 years old), and has all sorts of nasty
bugs (security-related and otherwise). It's a good thing you're
upgrading.
Recently I ported to a RH9 Linux server which runs Perl v5.8.0. One
part of the software is causing the following in my Apache error
log:

"Attempt to bless into a reference at..."

the referenced line contains

my $quiz = bless { _q => $_q, _a => $_a },$Quiz;

Check the documentation on Perl diagnostics with 'perldoc perldiag'
and read the entry your message refers to. If you don't understand
something about that answer, please let us know what it is, and we'll
try to help.

$Quiz (as a personal aside, I hate it when a line of code includes two
variables named identically except for case) is probably not what you
think it is. You probably should print it out and see what you're
trying to bless that anon hashref into.

-=Eric
 
M

Marty Landman

Eric Schwartz said:
Check the documentation on Perl diagnostics with 'perldoc perldiag'
and read the entry your message refers to.

Got it. You can see I'm new to OOPing and apparently owing to the Perl
versions was able to get away with an incorrect way of doing things.

I have been blessing into either the package string, or if the package
had already been instantiated with a hash ref would use that. But by
just changing the line of code to bless into the package name the hash
ref is automagically associated. Guess that's part of the point,
right?
$Quiz (as a personal aside, I hate it when a line of code includes two
variables named identically except for case) is probably not what you
think it is.

No, I knew exactly what it was and hope I explained clearly above.
Just that I didn't realize this was the wrong way to do this.

TMTOWTDI aside, I wonder how bad my variable naming style is. Although
what I do privately is obviously my own business, I don't want to
develop habits that are unfriendly to others, y'know?

To me it makes more sense to have e.g.

my ($quiz, @quiz, %quiz)

than to have

my ($quiz_sca, @quiz_ary, %quiz_hsh);

Well ok, not more sense but just easier for me to read the code.


Marty Landman Face 2 Interface Inc 845-679-9387
This Month's New Quiz --- Past Superbowl Winners
Make a Website: http://face2interface.com/Home/Demo.shtml
 
B

Ben Morrow

Got it. You can see I'm new to OOPing and apparently owing to the Perl
versions was able to get away with an incorrect way of doing things.

I have been blessing into either the package string, or if the package
had already been instantiated with a hash ref would use that. But by
just changing the line of code to bless into the package name the hash
ref is automagically associated. Guess that's part of the point,
right?

I'm not sure I follow you here... blessing an object associates it
with a package. ref $obj will return that package. So if $obj has been
blessed into package Package, the code

my $newobj = bless {...}, ref $obj;

will also bless $newobj into package Package. There is no association
between $obj and $newobj beyond the fact they are both blessed into
the same package.
TMTOWTDI aside, I wonder how bad my variable naming style is. Although
what I do privately is obviously my own business, I don't want to
develop habits that are unfriendly to others, y'know?

To me it makes more sense to have e.g.

my ($quiz, @quiz, %quiz)

My 2d:

Temporary variables should be in a small enough scope that essentialy
it doesn't matter what they're called, as you can see all uses of the
variable at once; thus they should have short, concise names. I do
sometimes use $foo and @foo; though there are often better names, like
@quizzes and $curquiz.

Globals (in which class I include lexicals whose scope covers more
than one sub, as well as package globals) should be meaningfully
named, and clearly rather than concisely. They are as much a part of
the interface to a given section of code as its sub names are. I tend
to name globals with an initial cap, just to distinguish them.

The only time I would use two variables with the same name in
different cases is where I have a scalar naming a file and a FH open
on that file, e.g.

my $config = $ENV{MY_CONFIG} || "$ENV{HOME}/.myconfig";
open my $CONFIG, '<', $config or die 'horribly';

Ben
 
E

Eric Schwartz

To me it makes more sense to have e.g.

my ($quiz, @quiz, %quiz)

than to have

my ($quiz_sca, @quiz_ary, %quiz_hsh);

Well ok, not more sense but just easier for me to read the code.

I'm not suggesting Hungarian notation, heaven forfend (though I've
been known to suffix scalar refs with _ref to remind myself that it's
not a real scalar), but just that you pick maybe more useful variable
names.

For instance, you have a data structure that is a quiz. Well, it's
unlikely to be a scalar, but could be an array or hash, depending on
how you build it. So instead of $quiz, pick something more useful,
like $question or $entry or some such. And you shouldn't need @quiz
and %quiz both. I have no idea how you structure your code, but
here's how I might dynamically build a quiz:

my %quiz = (title => "Countries in Europe",
creator => "Eric",
entries => []);

my @questions =( { q => "Is France in Europe?", a => "yes" },
{ q => "Is China in Europe?", a => "no" } );

foreach my $question (@questions) {
push @{$quiz{entries}}, $question
}

Of course, there are good reasons to use $foo and %foo and @foo in the
same program, and I'm not saying you should always avoid it. But I
find programs are easier to maintain if I don't do that very much.
Mostly I do things like above, where I have the aggregate @questions,
and alias $question to each member of it.

-=Eric
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top