perl.plugin.zsh
1783 bytes
1# https://github.com/dbbolton
2#
3# Below are some useful Perl-related aliases/functions that I use with zsh.
4
5
6# Aliases ###################################################################
7
8# perlbrew ########
9alias pbi='perlbrew install'
10alias pbl='perlbrew list'
11alias pbo='perlbrew off'
12alias pbs='perlbrew switch'
13alias pbu='perlbrew use'
14
15# Perl ############
16
17# perldoc`
18alias pd='perldoc'
19
20# use perl like awk/sed
21alias ple='perl -wlne'
22
23# show the latest stable release of Perl
24alias latest-perl='curl -s https://www.perl.org/get.html | perl -wlne '\''if (/perl\-([\d\.]+)\.tar\.gz/) { print $1; exit;}'\'
25
26
27
28# Functions #################################################################
29
30# newpl - creates a basic Perl script file and opens it with $EDITOR
31newpl () {
32 # set $EDITOR to 'vim' if it is undefined
33 [[ -z $EDITOR ]] && EDITOR=vim
34
35 # if the file exists, just open it
36 [[ -e $1 ]] && print "$1 exists; not modifying.\n" && $EDITOR $1
37
38 # if it doesn't, make it, and open it
39 [[ ! -e $1 ]] && print '#!/usr/bin/perl'"\n"'use strict;'"\n"'use warnings;'\
40 "\n\n" > $1 && $EDITOR $1
41}
42
43
44# pgs - Perl Global Substitution
45# find pattern = 1st arg
46# replace pattern = 2nd arg
47# filename = 3rd arg
48pgs() { # [find] [replace] [filename]
49 perl -i.orig -pe 's/'"$1"'/'"$2"'/g' "$3"
50}
51
52
53# Perl grep, because 'grep -P' is terrible. Lets you work with pipes or files.
54prep() { # [pattern] [filename unless STDOUT]
55 perl -nle 'print if /'"$1"'/;' $2
56}
57
58# If the 'perlbrew' function isn't defined, perlbrew isn't setup.
59if [[ $ZSH_PERLBREW_ACTIVATE != false ]] && (( ! $+functions[perlbrew] )); then
60 local _perlbrew="${PERLBREW_ROOT:-${HOME}/perl5/perlbrew}"
61 if [[ -f "${_perlbrew}/etc/bashrc" ]]; then
62 source "${_perlbrew}/etc/bashrc"
63 fi
64 unset _perlbrew
65fi