I notice that, in all your examples of what you want to do, the object you're working with is $self. If that's also true of your actual use case, then you can take advantage of the fact that your object is (almost certainly) implemented as a blessed hash reference and make use of a hash slice to extract values en masse from the underlying hash:
#!/usr/bin/env perl use strict; use warnings; use 5.010; package Foo; sub new { return bless $_[1] } sub show_properties { my $self = shift; my ($foo, $bar, $baz) = @$self{qw( foo bar baz )}; say "foo: $foo, bar: $bar, baz: $baz"; } package main; my $foo = Foo->new( { foo => 1, bar => 2, baz => 3 } ); $foo->show_properties;
Note that, because this accesses the underlying data without using any accessors, then you may get incorrect results if your accessors do anything more than just forward the underlying data to the caller (e.g., calculated properties).

Generally speaking, you can also do this to suck property values out of other objects[1], but that's a blatant violation of encapsulation and ties you directly to the internal implementation of the other class, so it's probably not a particularly good idea. (Doing this with $self similarly ties you to the internal implementation as well, of course, but, since it's within the same class, I don't consider it that big a deal. Others disagree quite strongly, and will maintain that accessors should always be used, without exception, even within the same class.)

[1] ...because almost all objects in Perl are blessed hashrefs and because "Perl doesn't have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun."


In reply to Re: Method for reducing tedium of transferring object properties to scalars by dsheroh
in thread Method for reducing tedium of transferring object properties to scalars by nysus

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.