Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Playing with code found on Perlmonks

by oko1 (Deacon)
on Apr 13, 2008 at 16:10 UTC ( #680105=snippet: print w/ replies, xml ) Need Help??

Description:

When I answer questions here, I often want to see exactly what kind of errors the posted code is going to throw - so I copy it, open a file in 'vi', paste it in, and (following a careful look at it to make sure that it's not going to do anything nasty to me) run it. The additional bits - e.g., adding on a Perl shebang and running 'chmod +x' on the file - are already shortcuts in my 'vi', but I thought that it would be nice to automate this part, at least. I've got "pmedit" linked to an icon on my Gnome toolbar, so all I have to do now is select the code and click the icon. The displayed file will already contain the code that I highlighted.

The following is a Bourne shell script, and requires 'Xdialog'. Please feel free to modify for other OSes and situations. Constructive comments are highly welcomed. :)

#!/bin/sh
# Created by Ben Okopnik on Sun Apr 13 11:22:45 EDT 2008

cd /tmp
label="New filename:"
while :
do
    fname=`Xdialog --stdout --inputbox "$label" 7 40`
    # Weird: '-f' doesn't handle '~', so we'll do it by hand
    fname=`echo $fname|sed 's/~/\/home\/ben/g'`

    if [ -f "$fname" ]
    then
        label="\"$fname\" already exists. New name:"
    else
        [ "$fname" = "" ] && exit
        xclip -o > "$fname"
        xterm -e vi "$fname"
        break
    fi
done

Comment on Playing with code found on Perlmonks
Download Code
Re: Playing with code found on Perlmonks
by ysth (Canon) on Apr 14, 2008 at 03:30 UTC
    I'm surprised Xdialog doesn't have a specific direct filename mode. I installed it to try it out, and (for me, anyway) it dragged in gtk 1.2 libraries, so I poked around for a gtk 2.0 equivalent and found zenity, which has handy "--file-selection --save" options.

      That's really odd - I thought (based on what it looks like) that 'Xdialog' was one of the old guard; the Athena Toolkit or something like that. Bleh. :(

      On the other hand, modifying it to use none of those libraries, at least for the dialog end, is trivial: change 'Xdialog' to 'dialog', remove the 'xterm -e' line from the 'vi' invocation, and launch the whole script as 'xterm -e pmedit'.

      
      -- 
      Human history becomes more and more a race between education and catastrophe. -- HG Wells
      
Re: Playing with code found on Perlmonks
by skx (Parson) on Apr 14, 2008 at 11:53 UTC

    This could be rewritten:

    fname=`echo $fname|sed 's/~/\/home\/ben/g'`

    Perhaps:

    fname=`echo $fname|perl -pe "s#~#$HOME#g"`
    Steve
    --

      Oh - I hadn't realized we were golfing. In Unix, as in Perl, TMTOWTDI.

      # Bourne shell and derivatives fname=`eval echo $fname` # Modern shells (e.g., Bash) fname=${fname/\~/$HOME}

      Your turn, m'sieu? :)

      
      -- 
      Human history becomes more and more a race between education and catastrophe. -- HG Wells
      

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://680105]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-27 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls