如何使用perl模板工具包获取用户名

发布于 2024-12-14 13:46:28 字数 1681 浏览 1 评论 0原文

我在从 Windows 系统获取用户名时遇到问题。我尝试在 perl 中使用 getlogin 函数并打印我的用户名,但我的问题是如何在模板工具包中访问此用户名。我尝试这样做,

 #!/usr/bin/perl
 use warnings;
 use strict;
 use Data::Dumper; 
   use XML::Simple;
 use Template;
  my $username = getlogin || getpwuid($<) || "veeru";

   my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml", ForceArray=>['dat','employee','experience']);
  print $username;

 my $template = Template->new();
my $filename = 'output1.tex';
  $template->process(\*DATA, $data, $filename)
|| die "Template process failed: ", $template->error(), "\n";

system( "pdflatex $filename" );
  __DATA__
 \documentclass[a4paper,leqno,twoside]{article}
 \usepackage[latin1]{inputenc}
 \usepackage[english]{babel}
 \begin{document}

 Issued by {Name}
 \issuedby{ [% username %] }

 % Document title. Use \doctitleShort{} to insert a shorter title in the header.
 \doctitle{employee information of thie"scr"company}
 \doctitleShort{\@doctitle}

[% FOREACH comp IN company %]
[% comp.name %] 
[% comp.location%]
employeedata:
[% FOREACH employee IN comp.domain.java.employee %]

[% employee.name %][% employee.number %]

[% FOREACH obj IN data%]

[% FOREACH beha IN obj.employee %]

[% IF beha.number == employee.number && beha.name == employee.name %] 

 [% beha.address %],

  [% LAST %]
 [% END %]
   [% END %]
 [% END %]
 [% END %]
 [% END %]
  [% END %]
 \end{document}

但它没有在 pdf 中打印用户名,而是在控制台上打印用户名,所以我在模板过程中访问用户名变量时犯了错误。请告诉我如何在模板中使用该用户名变量,如何在 pdf 中打印它。

我的第二个问题是

\doctitle{employee information of thie"scr"company}

上面的行文档标题是在模板过程中编写的,我需要从 Perl 代码访问标题如何做到这一点。任何人都可以帮助我,因为这是我第一次使用模板过程。

I have problem with getting username form windows system. I tried using getlogin function in perl and printing it printing my user name , but my problem is how can I access this username in template toolkit. I tried like this

 #!/usr/bin/perl
 use warnings;
 use strict;
 use Data::Dumper; 
   use XML::Simple;
 use Template;
  my $username = getlogin || getpwuid(
lt;) || "veeru";

   my $xml = new XML::Simple;
my $data = $xml->XMLin("data.xml", ForceArray=>['dat','employee','experience']);
  print $username;

 my $template = Template->new();
my $filename = 'output1.tex';
  $template->process(\*DATA, $data, $filename)
|| die "Template process failed: ", $template->error(), "\n";

system( "pdflatex $filename" );
  __DATA__
 \documentclass[a4paper,leqno,twoside]{article}
 \usepackage[latin1]{inputenc}
 \usepackage[english]{babel}
 \begin{document}

 Issued by {Name}
 \issuedby{ [% username %] }

 % Document title. Use \doctitleShort{} to insert a shorter title in the header.
 \doctitle{employee information of thie"scr"company}
 \doctitleShort{\@doctitle}

[% FOREACH comp IN company %]
[% comp.name %] 
[% comp.location%]
employeedata:
[% FOREACH employee IN comp.domain.java.employee %]

[% employee.name %][% employee.number %]

[% FOREACH obj IN data%]

[% FOREACH beha IN obj.employee %]

[% IF beha.number == employee.number && beha.name == employee.name %] 

 [% beha.address %],

  [% LAST %]
 [% END %]
   [% END %]
 [% END %]
 [% END %]
 [% END %]
  [% END %]
 \end{document}

but its not printing username in pdf , it printing username on console, so I did mistake in accessing username variable in template process. please tell me how to use that username variable in template, how to print that in pdf.

my second problem is

\doctitle{employee information of thie"scr"company}

in the above line document title is written in template process, I need to access title from perl code how to do this.can any one help me because this my first time using template process.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两个我 2024-12-21 13:46:28

通过传递给模板的 hashref(即 $data),您需要包含在模板中使用的所有变量。模板中的变量必须在 hashref 中具有与您要使用的值相对应的键。

因此,对于 username

$data->{username} = $username;
$template->process(\*DATA, $data, $filename);

同样,对于 doctitle,您可以在 Perl 代码中设置它(从而能够访问它):

$data->{doctitle} = 'employee information of thie"scr"company';

...并在您的模板:

\doctitle{[% doctitle %]}

With the hashref that you pass to the template (i.e. $data), you need to include any variables that you use in your template. The variables in your template must have a corresponding key in the hashref with the value that you want to use.

So for username:

$data->{username} = $username;
$template->process(\*DATA, $data, $filename);

Similarly, for the doctitle, you can set it in your perl code (and thus be able to access it):

$data->{doctitle} = 'employee information of thie"scr"company';

...and use it in your template:

\doctitle{[% doctitle %]}
北风几吹夏 2024-12-21 13:46:28

请在此处查看process子例程的API文档。正如您所看到的,第二个参数是哈希引用。您所要做的就是将用户名、文档标题和 data.xml 添加到该哈希中。

Look at the API documentation of process subroutine here. As you can see the second argument is a hash reference. All you have to do is to add username, document title, and data.xml to that hash.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文