is_numeric

35 views
Skip to first unread message

Helmut Schneider

unread,
Aug 6, 2018, 11:28:50 AM8/6/18
to puppet...@googlegroups.com
Hi,

I want to check if a variable is numeric. The manpage says is_numeric
is deprectaed and I shall user validate_legacy. But

<% if validate_legacy(Numeric, 'validate_numeric', $subsetting) { -%>

Error: Could not retrieve catalog from remote server: Error 500 on
SERVER: Server Error: Evaluation Error: Error while evaluating a
Function Call, validate_legacy(validate_numeric) expects a Numeric
value, got String at
/etc/puppetlabs/code/modules/bacula/templates/etc/bacula/bacula-dir.conf
.epp:25:7 on node bsdhelmut1164

What is the proper way to check if something is numeric?

Thank you!

Ben Ford

unread,
Aug 6, 2018, 4:31:58 PM8/6/18
to puppet...@googlegroups.com
First off, you should use data types with your class and then you won't have to check--the compile won't even succeed unless you've got the right data type.

class foo (
    Integer $subsetting,
) {
    # $subsetting is guaranteed to be numeric here
    ....
}

Second, you can use the is_a function, or the ~= equality matcher.



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/xn0lddxg1g5gp4001%40news.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Sebastian Hempel

unread,
Aug 7, 2018, 2:41:49 PM8/7/18
to puppet...@googlegroups.com
Hi,

> What is the proper way to check if something is numeric?

Starting with Puppet 4 you can define parameters of classes and defines
with a data type.

class myclass(
Integer $subsetting
)

IMHO this is the prefered way to "check" the type of a variable. You can
also specify the range of the allowed values [1].

[1]
https://puppet.com/docs/puppet/5.5/lang_data_number.html#the-integer-data-type
--
-- *Sebastian Hempel*
Veilchenweg 4 · 95195 Röslau · Germany
eMail: sebasti...@gmail.com
Google+: http://gplus.to/ithempel
Reply all
Reply to author
Forward
0 new messages