in reply to How to preserve values outside a sub
Also, note that $self is kind of reserved (although not enforced) for a blessed object. The way you're assigning $self is and will be very confusing to people familiar with OO programming in Perl. If you are not using OO, don't use $self as the variable name. Here's an example using $self in proper context:
use warnings; use strict; package Blah; { sub new { my ($class, $params) = @_; my $self = bless $params, $class; return $self; } sub display { my ($self) = @_; # should be using getters, but I digress for (keys %{ $self->{params} }){ print "$_: $self->{params}{$_}\n"; } } } package main; { my $hashref = { num1 => 1, num2 => 2 }; my $obj = Blah->new({params => $hashref}); $obj->display; }
|
|---|