Usage of OTRS Modules in perll script

English! place to talk about development, programming and coding
Post Reply
mohan
Znuny newbie
Posts: 19
Joined: 10 May 2010, 07:20
Znuny Version: 2.4

Usage of OTRS Modules in perll script

Post by mohan »

Hi,

I'm using the otrs modules in the perl script. This is the code.

Code: Select all

#! /usr/bin/perl
use lib "/opt/otrs/Kernel/System";
use Kernel::System::User;
User->GetUserData();
If I run this code in the command prompt, I get this error.
Can't locate Kernel/System/User.pm in @INC (@INC contains: /opt/otrs/Kernel/System /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at p8.pl line 3.
BEGIN failed--compilation aborted at p8.pl line 3.
Please someone help me in solving this problem?

Thanking you in advance.
Mike_B
Moderator
Posts: 266
Joined: 12 Jan 2010, 18:16
Znuny Version: CVS HEAD

Re: Usage of OTRS Modules in perll script

Post by Mike_B »

try

Code: Select all

use lib "/opt/otrs/";
because your lib root is otherwise pointing at the wrong location.
huntingbears.nl - @michielbeijen on Twitter
mohan
Znuny newbie
Posts: 19
Joined: 10 May 2010, 07:20
Znuny Version: 2.4

Re: Usage of OTRS Modules in perll script

Post by mohan »

Thank you Mike,
I have used another method, This is the code

Code: Select all

#! /usr/bin/perl
use lib "/opt/otrs";
use Kernel::System::User qw(GetUserData);
my $u = Kernel::System::User->GetUserData();
print $u,"\n";
But I'm getting this error
Can't use string ("Kernel::System::User") as a HASH ref while "strict refs" in use at /opt/otrs/Kernel/System/User.pm line 149.

AND ALSO THIS METHOD

Code: Select all

#! /usr/bin/perl
use lib "/opt/otrs";
use Kernel::System::User qw(GetUserData);
my $u = User->GetUserData();
print $u,"\n";

error is
Can't locate object method "GetUserData" via package "User" (perhaps you forgot to load "User"?) at p13.pl line 4.
Please help me in solving this problem
Mike_B
Moderator
Posts: 266
Joined: 12 Jan 2010, 18:16
Znuny Version: CVS HEAD

Re: Usage of OTRS Modules in perl script

Post by Mike_B »

It seems like you don't have much experience writing Perl scripts, right? That's no problem, here's an example that I hope is useful for you.

Please check out the following snippet. Please note that it assumes it is placed in the /opt/otrs/bin directory. If you want to have it somewhere else, modify the line that says use lib accordingly.

Code: Select all

#!/usr/bin/perl -w
# --
# otrs.printUser - Prints some user data
# Copyright (C) 2001-2010 xxx, http://otrs.org/
# --
# $Id$
# --
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU AFFERO General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin) . "/Kernel/cpan-lib";

use Getopt::Std;
use Kernel::Config;
use Kernel::System::Encode;
use Kernel::System::Log;
use Kernel::System::Main;
use Kernel::System::DB;
use Kernel::System::Time;
use Kernel::System::User;

my %Opts = ();
getopt( 'h', \%Opts );
if ( $Opts{h} || !$ARGV[0]) {
    print "otrs.printUser - print some user details\n";
    print "Copyright (C) 2001-2010 xxx, http://otrs.org/\n";
    print "usage: otrs.printUser [username] \n";
    exit 1;
}

# create common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject);
$CommonObject{EncodeObject} = Kernel::System::Encode->new(%CommonObject);
$CommonObject{LogObject} =
  Kernel::System::Log->new( %CommonObject, LogPrefix => 'otrs.printUser', );
$CommonObject{MainObject} = Kernel::System::Main->new(%CommonObject);
$CommonObject{TimeObject} = Kernel::System::Time->new(%CommonObject);
$CommonObject{DBObject}   = Kernel::System::DB->new(%CommonObject);
$CommonObject{UserObject} = Kernel::System::User->new(%CommonObject);

my $User = shift;

# fetch the data for the user
my %User = $CommonObject{UserObject}->GetUserData( User => $User, );

if ( !%User ) {
    print "User $User does not exist.\n";
    exit 1;
}

print "User:\t\t$User\n";
print "First name:\t$User{UserFirstname}\n";
print "Last name:\t$User{UserLastname}\n";
print "Email:\t\t$User{UserEmail}\n";

exit 0;
huntingbears.nl - @michielbeijen on Twitter
mohan
Znuny newbie
Posts: 19
Joined: 10 May 2010, 07:20
Znuny Version: 2.4

Re: Usage of OTRS Modules in perll script

Post by mohan »

Thank you Mike,
Yes you are right, my perl scripting knowledge is very low. Thank you for ur help. I got an Idea how to solve it.
Thousands of Thanks. I might need further help from you. So please keep on helping me, till I become like you.
Post Reply