Click on a member, and get the expression

Tips
2002-08-17
2013-05-02
  • Andrew E. Page

    Andrew E. Page - 2002-08-17

    Here's a new feature for ptkdb, that you can add through the .ptkdbrc
    file.  This will add a binding to the expression list so that when you
    click on an expression or a sub member of an expression it will insert
    the expression into the "Enter Expr:" field

    For example.  Say you have the following structure:

    ##
    ## Example of an Hash of Hashes from
    ## Programming Perl 3rd Edition by Larry Wall pg 279
    ## O'Reilly & Assoc
    ##
    %HoH = ( flintstones => {
                              husband => "fred",
                              pal     => "barney",
                             },
             jetsons     => {
                             husband   => "george",
                             wife      => "jane",
                             "his boy" => "elroy",   # Key quotes needed.
                           },
             simpsons    => {
                             husband  => "homer",
                             wife     => "marge",
                             kid      => "bart",
                 friends  => [ qw/lenny carl/, { bart => [ qw/nelson millhouse martin/ ]  } ]
                            },
            ) ;

    If enter %HoH into the "Enter Expr" field and then click on the 'martin'
    entry.  The following expression will appear in the "Enter Expr"
    field:

    $HoH{'simpsons'}->{'friends'}->[2]->{'bart'}->[2]

    To add this capability, add the following to your .ptkdbrc file:

    ##
    ## convert an hlist path
    ##
    sub hlist_selection_to_expr {
        my($entry, $hlist) = @_ ;
        my($path) = $hlist->infoSelection() ;
        my($array_context, $hash_context, $expr, $e) ;

        my($root_expr, @elems) = split /$Devel::ptkdb::pathSep/, $path ;

        return unless $root_expr ;

        ($array_context, $hash_context) = $root_expr =~ /^(\s*@)|^(\s*%)/ ;

        $root_expr = '(' . $root_expr . ')' if( $root_expr =~ / /) ;

        $expr = $root_expr ;

        if( @elems && ($array_context || $hash_context) ) {
            $expr =~ s/\s*[@%]/\$/ ;
            if( $array_context ) {
                $expr .= shift @elems ; # it will be a [idx] expr
            } elsif ( $hash_context ) {
                $expr .= "{\'" . shift(@elems) . "\'}" ;
            }
        }

        for( @elems ) {
            $expr .= '->' ;
            if( /\[[0-9]+\]/ ) {
                $expr .= $_ ;
            }
            else {
                $expr .= "{\'" . $_ . "\'}" ;
            }

        }
       
        $entry->delete(0, 'end') ;
        $entry->insert(0, $expr) ;
        $entry->selectionRange(0, 'end') ;
        $entry->focus ;
    }

    sub install_hlist_selection_to_expr {
        my($dbwin) = $DB::window ; # ptkdb window
        my($hlist) = $dbwin->{'data_list'} ;

        $Devel::ptkdb::pathSep = "\xff" ; # correction for ptkdb hlist bug
        $hlist->configure( 'separator' => $Devel::ptkdb::pathSep) ;

        $hlist->bind('<Button-1>', sub { hlist_selection_to_expr(@$dbwin{qw/entry data_list/}) }) ;
    }

    &install_hlist_selection_to_expr ;

     
    • sand2snowice

      sand2snowice - 2005-12-22

      Awesome! nice feature.  Prior to this I kept an editor open just for saving and reusing variable expressions I wanted to track. 

      BTW:
      you may want to attach a link to download this file as the browser has misinterpreted several intended characters of the cut-n-paste code

      left arrow became the literal &lt;
      right arrow became the literal &gt;
      ampersand became the literal &amp;
      quotes became the literal &quot;

      Anywise once I made the above fixes this feature worked great

       

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks