How do you call a custom function from another module?

1,227 views
Skip to first unread message

KomodoDave

unread,
Nov 29, 2012, 11:37:01 AM11/29/12
to puppet...@googlegroups.com
I have a utils module that contains common utilities. Most are defined types.

However, I've just added the first custom function in utils/lib/puppet/parser/functions/basename.rb .

It seems this cannot be referenced in the intuitive fashion from another module, namely:

  utils::basename(args)

Is it possible to call a custom function from another module? If so, what is the syntax? If not, is there a workaround besides copying the custom function to the referencing module?

Sincere thanks,

David

KomodoDave

unread,
Nov 29, 2012, 11:58:09 AM11/29/12
to puppet...@googlegroups.com
Ah, I've got it: simply include the module then call the function unqualified:

include utils
# ...
basename(args)

David

KomodoDave

unread,
Nov 29, 2012, 12:04:53 PM11/29/12
to puppet...@googlegroups.com
Hmm.. actually there's still something amiss.

Doing the include as described above changed the error from:

Error 400 on SERVER: Unknown function utils::basename

to:

Error 400 on SERVER: Function 'basename' does not return a value

..which made me think it was now being found.

I then fixed the bug by adding to basename definition the required :type => :rvalue .

However, instead of solving the issue I now see the message:

Error 400 on SERVER: undefined method `basename'

Any suggestions would be most welcome at this point!

Thank you.

David

KomodoDave

unread,
Nov 29, 2012, 12:37:54 PM11/29/12
to puppet...@googlegroups.com
I fixed the problem by prefixing the one-line definition of basename with a return.

Should I infer Puppet doesn't support the standard Ruby mechanism of returning the last evaluated result within a function?

I did have:

module Puppet::Parser::Functions
    newfunction(:basename, :type => :rvalue) do |args|
        File.basename(*args)
    end
end

..which resulted in:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: undefined method `basename'

I now have:

module Puppet::Parser::Functions
    newfunction(:basename, :type => :rvalue) do |args|
        return File.basename(*args)
    end
end

..which works like a charm.

Cheers,

David
Reply all
Reply to author
Forward
0 new messages