Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This is the only real example I could find in my own code, essentially it is a method on a base collection class to cause the collection to appear to be sorted on an arbitrary attribute (or attributes) of the objects in the collection. It uses eval to dynamically create a sort sub from a list of comparison specifications passed as an argument. Okay it doesn't fit the the 'small' criteria but I think it is illustrative of the kind of use string eval is good for:

sub sort { my ( $self, $sort_spec ) = @_; my $sort_done = 0; if ( defined $sort_spec ) { my @sort_objs; foreach my $obj ( @{$self->{_data}} ) { my $sv = []; push @{$sv}, $obj->factory_index(); foreach my $spec ( @{$sort_spec} ) { my $meth = $spec->{Key}; push @{$sv}, $obj->$meth(); } push @sort_objs, $sv; } my @comparisons = (); my $index = 0; foreach my $spec ( @{$sort_spec} ) { $index++; my $comp = ''; my ($left_arg, $right_arg) = ('$a','$b'); if ( exists $spec->{Direction} and $spec->{Direction} eq '-' ) { ( $left_arg, $right_arg) = ('$b', '$a'); } if ( $spec->{Compare} eq 'cmp' || $spec->{Compare} eq '<=>' ) { $comp = "${left_arg}->[$index] $spec->{Compare} ${right_arg} +->[$index]"; } else { $comp = "$spec->{Compare}(${left_arg}->[$index],${right_arg}- +>[$index])"; } push @comparisons, $comp; } if ( $index ) { my $sub = join ' || ' , @comparisons; $sub = "sub { $sub };"; *sortsub = eval $sub; $self->{_sort_index} = []; @{$self->{_sort_index}} = map { $_->[0] } sort sortsub @sort_objs; $sort_done = 1; my $index = 0; foreach my $obj ( $self->list() ) { $obj->factory_index($index++); } } } return $self->{_sorted} = $sort_done; }

/J\


In reply to Re: Small examples of string eval by gellyfish
in thread Small examples of string eval by spurperl

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (10)
    As of 2014-10-23 08:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (125 votes), past polls