[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] make function local

From: Linda Walsh
Subject: Re: [Help-bash] make function local
Date: Fri, 17 Apr 2015 13:50:28 -0700
User-agent: Thunderbird

Greg Wooledge wrote:
The problem is, ALL function definitions (and un-definitions) are global.
If there was a "func" defined at the global scope, you've obliterated it.
I don't understand why you don't understand this.
        I don't understand why you think I don't understand this.

I showed an example where I wanted to use a local function in a stand-alone script -- there were no libraries involved. There was no previous definition. The function was deleted if it was called
once.  No other interactions were specified.

        I don't get why you toss in all sorts of other possible interactions
that I didn't specify nor that existed.  Sure, if I pull the plug from
the wall all of your examples on your website won't work.  So should
I question your examples because they don't work under all possible
circumstances? That's ridiculous.

Doing trickery to make it LOOK LIKE you have a local function just leads
to confusion.  The person who inherits this script from you after you're
gone may think the function is local, when it isn't.
        I'm quite clear about how I use "throw-away" functions.

So, what is the point of doing this convoluted trickery, when it
accomplishes nothing (it doesn't create a local function, and it doesn't
prevent contamination of the global function namespace)?  Just to
see how "clever" you can be?
        No, as an example where a local function would be of use --
and the "clever trickery" (I said it as a "trite example") involved.

        If I wanted clever trickery, I'd use a function generator
that generated function names with a GUID ending -- then test if that
exists and repeat if there is a collision.  That would be more my idea
of clever trickery, since "Guids" are considered to be "Globally Unique"
(even though they really aren't), it would be as solid as a true local
functions as much as GUIDS are really globally unique -- sorta something

sub uuid_name {
 my uuid=$(uuidgen)
 echo "${uuid//-/_}"

sub filterarray {
 my arrayname=${1:?}
 my filter=${2:?}
 my t=$(type -t "$filter")
 if [[ $t!=function && $t!=alias ]]; then
   my uname="F_$(uuid_name)"
   eval "sub $uname { $filter }"
Would be more my idea of "clever trickery" -- and would be just
as globally unique as GUID's are taken to be (regen GUID's until
you find one that doesn't collide (note the above code doesn't
loop on a test, but was a random scriplet written a year ago
to capture the idea of wrapping some 'filter' code in a sub
if it wasn't already an alias or a sub.

Of course, I'm sure you already thought of creating function
names on the fly using GUID's to ensure they don't collide
globally, tricky guy that you are...  ;-)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]