In my latest folly, I changed an object to use several accessor methods that would return a scalar, rather than one accessor that would return a hash ref (that had to be dereferenced and I figured was an extra hassle for any script using the module).

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.