Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

use of $_ is destroying @database ... why?

by princepawn (Parson)
on Sep 24, 2003 at 20:00 UTC ( [id://293990]=perlquestion: print w/replies, xml ) Need Help??

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

For some reason, my array @databaseis being set to two undef values after it is used in an innocent-looking for loop... can someone remedy this? I using Perl 5.8.0.
#!/usr/bin/perl use strict; use Data::Dumper; use DBIx::Connect; use DBIx::Recordset; my @database = (qw(horse1 SYNDB)); my $r = '/data/tbone/maldunn2'; my %tbl; $DBIx::Recordset::Debug = 2; *DBIx::Recordset::LOG = \*STDERR; sub tbl_srch { my ($fname, $lname) = @_; for my $db (@database) { while ( my ($table, $fields) = each %tbl ) { DBIx::Recordset->Search ({ '!DataSource' => DBIx::Connect->to($db), $fields->[2] => $fname, $fields->[3] => $lname }); } } } sub load_tbls { my $database = shift; open T, "$r/$database.tblmap-names-email-address"; while ( <T>) { my ($table, @field) = split; $tbl{$table} = \@field; } } warn Dumper(\@database); load_tbls $_ for (@database); die Dumper(\@database); tbl_srch 'John', 'Smith';

output

perl sql.pl $VAR1 = [ 'horse1', 'SYNDB' ]; $VAR1 = [ undef, undef ];

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality.

Replies are listed 'Best First'.
Re: use of $_ is destroying @database ... why?
by Abigail-II (Bishop) on Sep 24, 2003 at 20:11 UTC
    It's the
    while ( <T>) {
    in load_tbls. That modifies $_, which at that moment is an alias for the elements of @database.

    Add a 'local $_' in your load_tbls sub.

    Abigail

Re: use of $_ is destroying @database ... why?
by premchai21 (Curate) on Sep 24, 2003 at 20:15 UTC

    While I haven't tested this in detail, it would seem to me that your usage of while (<T>) in load_tbls is clobbering $_, which is aliased to an entry in @database. Thus, when the while loop exits, $_ is undefined (the exit condition).

Re: use of $_ is destroying @database ... why?
by dragonchild (Archbishop) on Sep 24, 2003 at 20:18 UTC
    Try adding local $_; to the beginning of load_tbls() and see if that fixes it.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://293990]
Approved by jdtoronto
Front-paged by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (1)
As of 2025-03-21 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When you first encountered Perl, which feature amazed you the most?










    Results (63 votes). Check out past polls.