my and eval - '\@$n' fails

W

werwer

Here are two files that demonstrate the 'my' eval problem. "x.pl"
call two subs in "HPLC.pm". When the array @note is
'my'd, the sayNotes() eval creates no note array output.
The eval works without my, AND with 'our' or 'local'
As written below, 'my @note', the eval fails.

What's up? What'd I do wrong?
active 5.8.8, xp

#--------------------------- x.pl
use HPLC;
HPLC::demofill();
HPLC::sayNotes();


#--------------------------- HPLC.pm
package HPLC;
my $bug = 0;
$bug = 1;

BEGIN {
use Exporter ();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
$VERSION = 1.02;
@ISA = qw(Exporter);
@EXPORT = qw();
%EXPORT_TAGS = ();
@EXPORT_OK = qw();
}
our @EXPORT_OK;

@reagent;
@created;
@type;
@type2;
my @note;

@line_items;


sub
demofill {

$note[0] = "Note one.";
$note[1] = "Note two.";
$note[2] = "Note three.";
$note[3] = "Note four.";

}

#
------------------------------------------------------------------------
sub
sayNotes {
foreach my $n (qw(reagent created type type2 note)) {
$bug and print qq/array name: "$n"\n/;
foreach my $val (eval "\@$n") {
print qq/Value of notes $n is $val\n\n/;
}
}

print $note[0] . "\n";
#print $note[1] . "\n";
#print $note[2] . "\n";
#print $note[3] . "\n";

}
 
P

Paul Lalli

werwer said:
Here are two files that demonstrate the 'my' eval problem. "x.pl"
call two subs in "HPLC.pm". When the array @note is
'my'd, the sayNotes() eval creates no note array output.
The eval works without my, AND with 'our' or 'local'
As written below, 'my @note', the eval fails.

I cannot duplicate your results. When I run the below code, the output
I get is:
array name: "reagent"
array name: "created"
array name: "type"
array name: "type2"
array name: "note"
Value of notes note is Note one.

Value of notes note is Note two.

Value of notes note is Note three.

Value of notes note is Note four.

Note one.


Of course, the entire code is atrocious and should be thrown out in
favor of code that has strict and warnings enabled, and uses hashes and
real references rather than evals and symrefs. But I'll try to assume
for the moment you know all that and are simply trying this for an
academic reason. . .

Paul Lalli

What's up? What'd I do wrong?
active 5.8.8, xp

#--------------------------- x.pl
use HPLC;
HPLC::demofill();
HPLC::sayNotes();


#--------------------------- HPLC.pm
package HPLC;
my $bug = 0;
$bug = 1;

BEGIN {
use Exporter ();
our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);

You're not using strict, so what do you think this line is doing?
$VERSION = 1.02;
@ISA = qw(Exporter);
@EXPORT = qw();
%EXPORT_TAGS = ();
@EXPORT_OK = qw();
}
our @EXPORT_OK;

@reagent;
@created;
@type;
@type2;

Out of curiousity, what do you think these lines are doing?
my @note;

Regardless, this should be come:
my %hash;
$hash{$_} = [ ] for qw/reagent created type type2 note/;
@line_items;


sub
demofill {

$note[0] = "Note one.";
$note[1] = "Note two.";
$note[2] = "Note three.";
$note[3] = "Note four.";

}

#

foreach my $n (keys %hash) {
$bug and print qq/array name: "$n"\n/;
foreach my $val (eval "\@$n") {

foreach my $val (@{$hash{$n}})
print qq/Value of notes $n is $val\n\n/;
}
}

print $note[0] . "\n";
#print $note[1] . "\n";
#print $note[2] . "\n";
#print $note[3] . "\n";

}


Paul Lalli
 
W

werwer

what is with you? If I wanted commentary I'd dial in the fat man.
Anyways, thanks for
both the unsolicited and otherwise.

Anyone have this code fail? Fails on XP, perl 5.8.8 active
 
U

Uri Guttman

w> What's up? What'd I do wrong?

you don't want commentary, then don't post crappy code in public.

w> #--------------------------- HPLC.pm
w> package HPLC;

use strict
use warnings

w> my $bug = 0;
w> $bug = 1;

w> BEGIN {
w> use Exporter ();
w> our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
w> $VERSION = 1.02;
w> @ISA = qw(Exporter);
w> @EXPORT = qw();
w> %EXPORT_TAGS = ();
w> @EXPORT_OK = qw();
w> }
w> our @EXPORT_OK;

why all the exporter boilerplate? you don't export anything. and that
code is very dumb anyhow. you don't need to do it in a BEGIN block. you
can declare the variables and assign then in one statement. use base
will save you a line. otherwise, it is wonderful!

w> @reagent;
w> @created;
w> @type;
w> @type2;
w> my @note;

w> @line_items;


w> ------------------------------------------------------------------------
w> sub
w> sayNotes {
w> foreach my $n (qw(reagent created type type2 note)) {
w> $bug and print qq/array name: "$n"\n/;
w> foreach my $val (eval "\@$n") {
w> print qq/Value of notes $n is $val\n\n/;
w> }
w> }

other than curiosity's sake, why are you doing that?

and it works on my box.
array name: "reagent"
array name: "created"
array name: "type"
array name: "type2"
array name: "note"
Value of notes note is Note one.

Value of notes note is Note two.

Value of notes note is Note three.

Value of notes note is Note four.

Note one.

uri
 
P

Paul Lalli

werwer said:
what is with you?

Could you be more specific? Possibly by quoting some context when you
reply? You know, like everyone else has agreed is the proper way to
post a reply to Usenet for the past 2 decades?

Have you read the Posting Guidelines for this group yet?
If I wanted commentary I'd dial in the fat man.

You are under the very mistaken impression that I care what you want.
I do not post for you. I post for anyone who might benefit from what I
have to say. And if I've encouraged anyone who's reading my reply now
or in the future to not program with those horrible practices, then I
feel quite justified.
Anyways, thanks for both the unsolicited and otherwise.

You're welcome.
Anyone have this code fail? Fails on XP, perl 5.8.8 active

Works just fine on XP, perl 5.8.4 activestate

Paul Lalli
 
U

Uri Guttman

w> I think you need a girlfriend.

how did you know that? i am married and can use a girlfriend. my wife
says i can have one since she knows i couldn't handle her!

and your code still sucks and mine doesn't.

NYAH! NYAH! NYAH!!

<for others, that was very sarcastic>

your use of eval works on other boxes but yours so that means we can't
help you. too bad. my advice for you is to drop your girlfriend (of
course YOU have one), save your money from doing that and hire a perl
hacker to code for you.

have a nice buggy day (you will if you keep using eval for evil
reasons).

uri
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top