Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

(bbq) RE: Re: Another flatfile thingy

by BBQ (Curate)
on Aug 04, 2000 at 17:37 UTC ( [id://26185]=note: print w/replies, xml ) Need Help??


in reply to Re: Another flatfile thingy
in thread Another flatfile thingy

  • W/r/t your data structure: personally, I always prefer a hash of records, with each of those records being a hash reference of columns and values. Whereas yours is the other way round. Does that make sense? In other words, I'd prefer

    Yeah it makes sense, its just a matter of $hash{x}{y} vs. $hash{y}{x}. As you said, personal preference. I think of tables in a very visual way, so I just guess my feeling is for x first.

  • Returning a reference would be less memory-intensive.

    Forgive me for my dense question, but how do you return a reference of a hash if the hash is private to the subroutine? Can that be done?

  • When you return on an error, you don't have to explicitly return 0.

    Oh, that's just an office habit we have. We are always very explicit about everything, we even avoid using $_ when possible. Ex: saying foreach $v instead of just foreach. But thanks for the note anyway!

@{$hash->{$columns[0]}}{@headers} = @columns;Now that has to be one of the coolest things I've seen here lately... Congrats, dude. You hit the nail on the head it seems. Thanks for your views.

#!/home/bbq/bin/perl
# Trust no1!

Replies are listed 'Best First'.
RE: (bbq) RE: Re: Another flatfile thingy
by btrott (Parson) on Aug 04, 2000 at 20:10 UTC
    You wrote:
    > Forgive me for my dense question, but how do you return > a reference of a hash if the hash is private to the > subroutine? Can that be done?
    Don't think of the hash as "private" to the subroutine; yes, it's declared lexically within the sub, but that's not all that matters. Think of the hash as a piece of data *somewhere* that you've referenced lexically within the sub.

    Perl's garbage collection is based on reference counting; so each reference to such a piece of data increments the reference count. Once you've declared

    my $hash = {}
    the reference count of the underlying structure is incremented to one.

    Normally, you'd just have that one reference to the data, so once the lexical has gone out of scope--at the end of the subroutine--the data would be destroyed. But not here, because you've returned a reference to the data and assigned it somewhere else. Even though leaving the sub decrements the reference count, then, you re-increment it by assigning the return value of the sub. So the reference count is still one, and the data is still there, and valid.

    The data won't be destroyed until all references to it have gone away.

    BTW: this is, as far as I know, correct, but someone who knows more about Perl's internals is free to correct anything wrong. :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-04-19 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found