Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found