So, rather than getting a value like my $dir = $site->dirs()->{'uploads'};, I could write my $dir = $site->upload_dir;
All well and good, except my object also has a couple of accessor methods that should return arrays rather than scalars, so my return from the accessor method looks like:
return @{$self->{$slot}} if wantarray; return $self->{$slot};
It must be noted I'm using closures in a loop to create the accessor methods, hence the conditional return. See my accessor generator at the bottom.
End of preamble - this is where I got tripped up and nearly went insane - in my calling script, I wanted to join the return from the 'upload_dir' method to something else, like so:
my $dir = join '/', $site->upload_dir, $user->id;
Now, it should be obvious given my preamble that because I have called the upload_dir method in list context (because join expects a list) my method will return (or try to return) a "listified" version of my scalar upload_dir value, and hence won't be what I'm looking for - in this case, it ended up returning an empty list, and messing everything up.But it took me over an hour to figure this out (I'm not too bright at times :).
What I can't figure out is why use strict; and use warnings; didn't throw me some kind of message about trying to turn my scalar (upload_dir) into an array to return from the method?
####################################################### # generate the generic accessor methods using closures: for my $property (keys %properties) { my $slot = __PACKAGE__ . "::$property"; no strict 'refs'; # so symbolic ref to typeglob works - see The + Camel p.338 *$property = sub { my $self = shift; if (@_) { if ( ref $_[0] ) { # arg is a ref, just store it $self->{$slot} = shift; } elsif ( $properties{$property} eq 'ARRAY' ) { # transla +te array to arrayref $self->{$slot} = [@_]; } elsif ( $properties{$property} eq 'HASH' ) { # translat +e hash to hashref $self->{$slot} = {@_}; } elsif ( $properties{$property} eq 'SCALAR' ) { # scalar +, no translation $self->{$slot} = shift; } else { die "Problem with $slot"; } } return @{ $self->{$slot} } if wantarray; return $self->{$slot}; }; }
In reply to A vexing list vs. scalar context question. by theguvnor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |