Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

(crazyinsomniac: perlref) Re: Problem with a Ref to a global variable while using XML::Parser

by crazyinsomniac (Prior)
on May 29, 2002 at 00:24 UTC ( #169943=note: print w/replies, xml ) Need Help??


in reply to Problem with a Ref to a global variable while using XML::Parser

First, I reccomend you sit down and read perldata,perlref, and skim through perllol and perldsc, and then consider this piece of code
#!/usr/bin/perl -w use strict; $\="\n"; my %jar = qw( a b c d); my @ppp = ( \%jar ); print a => $jar{a}; print a => $ppp[0]->{a}; print c => $jar{c}; print c => $ppp[0]->{c}; $jar{a}='gork'; $jar{c}='gork'; push @ppp, \%jar; print a => $jar{a}; print a => $ppp[0]->{a}; print a => $ppp[1]->{a}; print c => $jar{c}; print c => $ppp[0]->{c}; print c => $ppp[1]->{c}; { my %jar = ( a => shark => c => shark => 1 => 2 ); push @ppp, \%jar; } print a => $jar{a}; print a => $ppp[0]->{a}; print a => $ppp[1]->{a}; print a => $ppp[2]->{a}; print c => $jar{c}; print c => $ppp[0]->{c}; print c => $ppp[1]->{c}; print c => $ppp[2]->{c};
Basically, all you're pushing onto your array, is the same hashref over and over and over again, whose values you redefine, over and over and over and over again.

Now your hash is not a global, even though you kind of treat it as such (globals live in the package namespace, they aren't declared with my, they're declared with use vars '%global', or our(%global), or %package::name::global).

Before you push @stories, \%story, you could simply create a new hash, and push it's reference onto @stories. Example

use strict; $\="\n"; my %f = 1..4; print \%f; my %b = %f; print \%b; print they => are => not => equal => chr(33) if \%b ne \%f ;

 
______crazyinsomniac_____________________________
Of all the things I've lost, I miss my mind the most.
perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

  • Comment on (crazyinsomniac: perlref) Re: Problem with a Ref to a global variable while using XML::Parser
  • Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://169943]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2019-07-18 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?