in reply to Re: How to detect a hash container key from a referenced sub value
in thread How to detect a hash container key from a referenced sub value

First, I want to say, I've made some typos in my code. I've used '=' instead of '=>' in hashes and I've forgot to apply a value in the subroutine. I fixed it now.

Anyway.

The obvious, as you say, yes. That's how I usually do it. I, usually, create a sub, supply the values explicitly to it and get the result. I just wanted to see, if it's possible to do it implicitly, also, I need that total value in the variable, because I am going to sort it by qtyTotal in dec order, by using a sort function. If it's not going to be automatically updated, I figure that I have to create an other temp variable, every time I use that sort function. I just wanted to see, if could it be done in a more elegant way. Also, it will eliminate any bugs, that could be caused by forgetting of updating a qtyTotal in that variable.

I guess, you are right about creating object instead of just using a regular variable.

I was hesitant with that because, the only time, I've used objects is when I refer to other libraries or modules and what always stopped me was that, i might be wrong, the requirement to create a module, describing the object in the separate file instead of just ability to make an object in the same file as the script. :(
  • Comment on Re^2: How to detect a hash container key from a referenced sub value

Replies are listed 'Best First'.
Re^3: How to detect a hash container key from a referenced sub value
by almut (Canon) on Dec 03, 2009 at 11:52 UTC
    what always stopped me was that, i might be wrong, the requirement to create a module, describing the object in the separate file instead of just ability to make an object in the same file as the script.

    You don't necessarily need separate files.  Separate packages, yes, but you can have more than one package in the same file (if you really want):

    #!/usr/bin/perl package Foo; sub new { my $class = shift; return bless { @_ }, $class; } # ... package Bar; sub new { my $class = shift; return bless { @_ }, $class; } # ... package main; # the actual script my $foo = Foo->new( myattr => "foo" ); # create an obj use Data::Dumper; print Dumper $foo; __END__ $VAR1 = bless( { 'myattr' => 'foo' }, 'Foo' );