3 # $Id: YCP.pm 33405 2006-10-13 13:12:42Z mvidner $
7 YaST::YCP - a binary interface between Perl and YCP
11 use YaST::YCP qw(:DATA :LOGGING);
13 YaST::YCP::Import ("SCR");
14 my $m = SCR->Read (".sysconfig.displaymanager.DISPLAYMANAGER");
15 SCR->Write (".sysconfig.kernel.CRASH_OFTEN", Boolean (1));
19 YaST has a richer and stricter data type system than Perl.
21 Note that the stdio-communicating agents, based on the modules
22 L<YaST::SCRAgent|YaST::SCRAgent> and L<ycp|ycp>, have a similar but
23 not the same data type mapping.
25 When the language binding knows what type to expect, eg. when passing
26 an argument to a YCP function, it will convert a Perl scalar to the
29 On the other hand, if the type is not known, expressed
30 in YCP as C<any>, scalars will be passed as strings. If you want
31 a specific data type, use one of the data classes like
32 L<YaST::YCP::Integer|/Integer>. Of course these work also when
39 Has only one value, C<nil>, which is represented as C<undef>.
40 Any data type can have C<nil> as a value.
44 A union of all data types. Any data type can be assigned to it.
46 =item string, integer, float, boolean
48 B<YCP to Perl:> Becomes a scalar
50 B<Perl to YCP:> Any scalar will become a string
51 (even if it looks like a number).
52 Use L</String>, L</Integer>, L</Float> or L</Boolean>
53 if you want a specific data type.
55 =item list E<lt>TE<gt>
57 B<YCP to Perl:> A list becomes a reference to an array.
58 (Note that it refers to a B<copy>.)
60 B<Perl to YCP:> A reference to an array becomes a list.
61 I<This was different before SL9.1 Beta1:>
62 Perl functions returning multiple values should not return a list
63 but a reference to it. YCP will always set a scalar calling context,
64 even if the result is assigned to a list.
66 =item map E<lt>T1, T2E<gt>
68 B<YCP to Perl:> A map becomes a reference to a hash.
69 (Note that it refers to a B<copy>.)
71 B<Perl to YCP:> A reference to a hash becomes a map.
75 B<YCP to Perl:> NOT IMPLEMENTED YET.
77 B<Perl to YCP:> If a path is expected, a scalar like C<".foo.bar">
78 will be converted to C<.foo.bar>.
79 Otherwise use L</Path> (which is NOT IMPLEMENTED YET).
83 B<YCP to Perl:> Becomes a L</Symbol>.
85 B<Perl to YCP:> If a symbol is expected, a scalar like C<"foo">
86 will be converted to C<`foo>.
87 Otherwise use L</Symbol>.
91 B<YCP to Perl:> Becomes a L</Term>.
93 B<Perl to YCP:> Use L</Term>.
97 B<YCP to Perl:> Becomes a scalar.
99 B<Perl to YCP:> If a byteblock is expected, a scalar like C<"\0\1">
100 will be converted to C<#[0001]>.
101 Otherwise use L</Byteblock>.
103 =item locale, block E<lt>TE<gt>, ...
111 The DATA tag (in C<use YaST::YCP qw(:DATA)>) imports the data
112 constructor functions such as Boolean, Symbol or Term.
122 our @ISA = qw(Exporter);
123 my @e_data = qw(Boolean Byteblock Integer Float String Symbol Term);
124 my @e_logging = qw(y2debug y2milestone y2warning y2error y2security y2internal);
125 our @EXPORT_OK = (@e_data, @e_logging, "sformat");
126 our %EXPORT_TAGS = ( DATA => [@e_data], LOGGING => [@e_logging] );
130 $olddebug = YaST::YCP::debug (1);
132 YaST::YCP::debug ($olddebug);
134 Enables miscellaneous unscpecified debugging
150 ## calls boot_YaST__YCP
152 XSLoader::load ('YaST::YCP');
156 YaST::YCP::init_ui ();
157 YaST::YCP::init_ui "qt";
159 Initializes the user interface, "ncurses" (the default) or "qt".
163 # ensure that the ncurses window is closed
164 # and wfm and its agents are closed (#39519)
166 close_components (); # XS
171 YaST::YCP::Import "Namespace";
172 Namespace->foo ("bar");
174 Imports a YaST namespace (in YCP or Perl or any supported language).
175 Equivalent to YCP C<import>, similar to Perl C<use>.
177 If C<Namespace> is in YCP, its constructor is executed later than if
178 it were imported from YCP. This can have subtle effects, for example
179 in testsuites. To get closer to the YCP import behavior, call
180 C<Import> from a C<BEGIN> block.
187 print "Importing $package\n" if debug;
190 # let it get our autoload
191 *{"${package}::AUTOLOAD"} = \&YaST::YCP::Autoload::AUTOLOAD;
196 These functions go via liby2util and thus use log.conf.
197 See also ycp::y2milestone.
199 The multiple arguments are simply joined by a space.
201 y2debug ($message, $message2, ...)
202 y2milestone ($message, $message2, ...)
203 y2warning ($message, $message2, ...)
204 y2error ($message, $message2, ...)
205 y2security ($message, $message2, ...)
206 y2internal ($message, $message2, ...)
210 sub y2_logger_helper ($@)
213 # look _two_ frames up for the subroutine
214 # when called from the main script, it will be undef
215 my ($package, $filename, $line, $subroutine) = caller (2);
216 # look _one_ frame up for file and line
217 # (is it because of optimization?)
218 ($package, $filename, $line) = caller (1);
220 y2_logger ($level, "Perl", $filename, $line, $subroutine || "",
224 sub y2debug (@) { y2_logger_helper (0, @_); }
225 sub y2milestone (@) { y2_logger_helper (1, @_); }
226 sub y2warning (@) { y2_logger_helper (2, @_); }
227 sub y2error (@) { y2_logger_helper (3, @_); }
228 sub y2security (@) { y2_logger_helper (4, @_); }
229 sub y2internal (@) { y2_logger_helper (5, @_); }
233 Implements the sformat YCP builtin:
235 C<sformat ('%2 %% %1', "a", "b")> returns C<'b % a'>
237 It is useful mainly for messages marked for translation.
244 # now the % indices can be used for @_
247 # g: global, replace all occurences
248 # e: expression, not a string
249 $format =~ s{%([1-9%])}{
250 ($1 eq '%') ? '%' : $_[$1]
256 # shortcuts for the data types
257 # for POD see packages below
261 return new YaST::YCP::Boolean (@_);
266 return new YaST::YCP::Byteblock (@_);
271 return new YaST::YCP::Integer (@_);
276 return new YaST::YCP::Float (@_);
281 return new YaST::YCP::String (@_);
286 return new YaST::YCP::Symbol (@_);
291 return new YaST::YCP::Term (@_);
294 # by defining AUTOLOAD in a separate package, undefined functions in
295 # the main one will be detected
296 package YaST::YCP::Autoload;
301 # cannot rely on UNIVERSAL::AUTOLOAD getting automatically called
302 # http://www.rocketaware.com/perl/perldelta/Deprecated_Inherited_C_AUTOLOAD.htm
304 # Gets called instead of all functions in Import'ed modules
305 # It assumes a normal function, not a class or instance method
310 # strip $self on the way from Perl to YCP,
311 # just as it is added in the reverse direction
313 print "$himself $AUTOLOAD (", join (", ", @_), ")\n" if YaST::YCP::debug;
315 my @components = split ("::", $AUTOLOAD);
316 my $func = pop (@components);
317 return YaST::YCP::call_ycp (join ("::", @components), $func, @_);
322 $b = YaST::YCP::Boolean (1);
324 print $b->value, "\n";
325 SCR::Write (".foo", $b);
329 package YaST::YCP::Boolean;
334 # a Boolean is just a blessed reference to a scalar
340 return bless \$val, $class
346 # see "Constructors and Instance Methods" in perltoot
348 if (@_) { $$self = shift; }
354 A chunk of binary data.
356 use YaST::YCP qw(:DATA);
358 read ($dev_random_fh, $r, 100);
360 $b->value ("Hello\0world\0");
361 print $b->value, "\n";
366 package YaST::YCP::Byteblock;
371 # a Byteblock is just a blessed reference to a scalar
372 # just like Boolean, so use it!
374 our @ISA = qw (YaST::YCP::Boolean);
378 An explicitly typed integer, useful to put in heterogenous data structures.
380 use YaST::YCP qw(:DATA);
382 $i = Integer ("42 and more");
383 $i->value ("43, actually");
384 print $i->value, "\n";
389 package YaST::YCP::Integer;
395 # an Integer is just a blessed reference to a scalar
396 # just like Boolean, so use it!
398 our @ISA = qw (YaST::YCP::Boolean);
402 An explicitly typed float, useful to put in heterogenous data structures.
404 use YaST::YCP qw(:DATA);
406 $f = Float ("3.41 is PI");
407 $f->value ("3.14 is PI");
408 print $f->value, "\n";
413 package YaST::YCP::Float;
419 # a Float is just a blessed reference to a scalar
420 # just like Boolean, so use it!
422 our @ISA = qw (YaST::YCP::Boolean);
432 An explicitly typed string, useful to put in heterogenous data structures.
434 use YaST::YCP qw(:DATA);
438 print $s->value, "\n";
443 package YaST::YCP::String;
448 # a String is just a blessed reference to a scalar
449 # just like Boolean, so use it!
451 our @ISA = qw (YaST::YCP::Boolean);
455 use YaST::YCP qw(:DATA);
457 $s = Symbol ("next");
459 print $s->value, "\n";
460 return Term ("id", $s);
464 package YaST::YCP::Symbol;
470 # a Symbol is just a blessed reference to a scalar
471 # just like Boolean, so use it!
473 our @ISA = qw (YaST::YCP::Boolean);
477 $t = new YaST::YCP::Term("CzechBox", "Accept spam", new YaST::YCP::Boolean(0));
478 $t->name ("CheckBox");
479 print $t->args->[0], "\n";
480 UIx::OpenDialog ($t);
484 package YaST::YCP::Term;
489 # a Term has a name and arguments
496 return bless { name => $name, args => $args }, $class
502 # see "Constructors and Instance Methods" in perltoot
504 if (@_) { $self->{name} = shift; }
505 return $self->{name};
511 # see "Constructors and Instance Methods" in perltoot
513 if (@_) { @{ $self->{args} } = @_; }
515 # because I don't want to process multiple return values,
516 # I return it as a reference
517 return $self->{args};