my $x; my $str = sub { "hello ".${\$x} }; $x = 'kevin'; print $str->(); $x = 'chickenman'; print $str->(); #### {package XYZ; sub new { my $class = shift; my $self = { homedir => '' }; return bless $self => $class } sub homedir { my $self = $_[0]; if( defined $_[1] ){ $self->{homedir} = $_[1] } return $self->{homedir} } sub executable { my $self = $_[0]; return $self->homedir()."/xyy/abc" } 1; } my $xyz = new XYZ(); $xyz->homedir("ahah"); print "homedir: ".$xyz->homedir()."\n"; print "exec: ".$xyz->executable()."\n"; # note, when dealing with paths use File::Spec->catdir()