Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

slice of ptr-based-output routines?

by perl-diddler (Hermit)
on Aug 01, 2012 at 06:31 UTC ( #984705=perlquestion: print w/replies, xml ) Need Help??
perl-diddler has asked for the wisdom of the Perl Monks concerning the following question:

I know how to do slices of arrays and hashes, but in writing a debug statement, I was having to do too much repetitive typing:
Pe "create_lv_snap,name=%s, path=%s, vgname=%s,mp=%s,size=%s", $snap_lvh->lv_name, $snap_lvh->lv_path, $snap_lvh->vg_name, $snap_lvh->_mp, $snap_lvh->lv_size;
What I would have liked to have done was something like:
Pe "create_lv_snap,name=%s, path=%s, vgname=%s,mp=%s,size=%s", &{$snap_lvh}{lv_name, lv_path, vg_name,_mp, lv_size};
Where the output's of each subroutine call (a variable) would be passed as args to the print statement... Is there something like that already in perl that I'm not aware of?

Remember, this was a debug statement -- so minimizing time to type it in was typical of this type of problem.....

Ideas? Clue sticks? ;-)

Replies are listed 'Best First'.
Re: slice of ptr-based-output routines?
by Athanasius (Chancellor) on Aug 01, 2012 at 07:10 UTC

    Hello perl-diddler, will this do?

    #! perl use strict; use warnings; package Gromit; sub new { my ($class) = @_; my %self; return bless \%self, $class; } sub lv_name { print "in lv_name()\n"; } sub lv_path { print "in lv_path()\n"; } sub vg_name { print "in vg_name()\n"; } sub _mp { print "in _mp()\n"; } sub lv_size { print "in lv_size()\n"; } package main; my $snap_lvh = Gromit->new(); $snap_lvh->$_() for qw(lv_name lv_path vg_name _mp lv_size);


    in lv_name() in lv_path() in vg_name() in _mp() in lv_size()


    Athanasius <°(((><contra mundum

Re: slice of ptr-based-output routines?
by sundialsvc4 (Abbot) on Aug 01, 2012 at 13:51 UTC

    Sigh...   Copy the string $snap_lvh-> to the clipboard, then paste it each time.   If the set of fields is never going to change during the life of the program, “you only have to type it once.”

    If the list of fields is truly variable, then the list of field-names can be put into an array or list and then foreach my $foo (@list) you can use the syntax:   $lv_name->{$foo}.

    But in any case your aim should be perfect clarity and maintainability; not a momentary ease of effort for your fingers.   The day may well come when one of those output statements must be slightly different from the rest.   Beware of unwanted “coupling” between one occurrence of a statement and the next.

      Are you saying a slice is less clear than spelling out the pointer with each element?

      I don't think I emphasized the point clearly enough -- the above was a transitory debug line put in to look at those values. I didn't want a loop or supporting routines... just wanted to print. Pe is a shortcut routine all in of itself, It' alternate of P, which prints to STDOUT by default (or to a string, in string context). Auto traps/handles undef's in printed args and auto deals with linefeed (adds as final char going to a file handle if needed).

      Each item I am printing is a 'class variable' and all of those subs are 'accessor routines'...writing additional routines just to print the value of accessor routines is way overkill for a debug statement.

      It's all about easy of programming... computers were designed to be the servants of humans -- not the otherway around.

      I just thought a slice like statement where each accessor is called off the same pointer would be more clear than retyping the accessor 5 times. Aren't we supposed to avoid duplication in code?

      Sorry if I was unclear, but I did try to stress it was only debugging code.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://984705]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-01-21 06:44 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (182 votes). Check out past polls.