LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2010, 08:11 PM   #1
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
Help me understand this Perl script please. It's not homework.


Hi,


I'm trying to get webalizer to analyse some log files. The server uses virtual hosts and has log rotations on and also uses turbopanel (now known as simple control panel).

Because of this, the documentation is limited and webalizer works in a weird way.

I found this perl script under turbopanel called webalizerrun.pl the code is as follows:

Code:
#!/usr/bin/perl
$WEBALIZER = "/usr/bin/webalizer";

chomp($var = shift);
$wdir = "$var/conf/webalizer";

opendir(DIR, $wdir) or die "Unable to read $wdir: $!";
while (defined($domain = readdir(DIR))) {
  $dir = "$wdir/$domain";
  $conf = "$dir/webalizer.conf";
  $log = "/var/log/httpd/" . $domain . "_access_log";
  if ((-d $dir) && (-f $conf) && (-f $log)) {
    chdir($dir);
    system($WEBALIZER, "-Q");
  }
}
exit 0;

Here's what I want to do, and I believe I can do this using this code with slight modifications.

As of now, the log files for each site is in the folder specified above with the file named as "domain-name_access_log" and then the log rotation just adds a number to the end of that.

I want use this perl script to run webalizer for a particular site and have its output be placed in directory.

Any help deciphering this perl script is appreciated, specifically I'm trying to figure out the following:

1.) Line 4: chomp($var = shift): I know chomp is used to remove trailing characters, but what character in this case? How may I find that out? Also what does $var = shift do inside chomp?

2.) Line 8: What exactly does the readdir function do? What does it return to $domain?

The rest seems similar to csh, checks if it's dir or file and then changes to the directory and runs webalizer on that directory.


Thanks
 
Old 08-02-2010, 08:39 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vxc69 View Post
Hi,

1.) Line 4: chomp($var = shift): I know chomp is used to remove trailing characters, but what character in this case? How may I find that out? Also what does $var = shift do inside chomp?

2.) Line 8: What exactly does the readdir function do? What does it return to $domain?
Thanks
read the documentation!.

perldoc -f readdir
perldoc -f chomp
 
1 members found this post helpful.
Old 08-02-2010, 09:17 PM   #3
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Thanks for that handy tip. Some questions:

1.) It says chomp "removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the "English" module)." I know next to nothing about perl. What is the English Module? Where can I find it and how may I find the value of $INPUT_RECORD_SEPARATOR?

2.) According to perldoc, shift without an ARRAY specified, carries out shift on the "@_" array. According to what I've read, @_ is a special array that lists arguments supplied to a sub routine. There are no sub routines in this perl script. I'm confused regarding that.


Some help on this is appreciated.


Thanks again.

Last edited by vxc69; 08-02-2010 at 09:19 PM.
 
Old 08-02-2010, 09:26 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
Thanks for that handy tip. Some questions:

1.) It says chomp "removes any trailing string that corresponds to the current value of $/ (also known as $INPUT_RECORD_SEPARATOR in the "English" module)." I know next to nothing about perl. What is the English Module? Where can I find it and how may I find the value of $INPUT_RECORD_SEPARATOR?
perldoc perlvar
perldoc English
 
1 members found this post helpful.
Old 08-02-2010, 09:29 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
...
2.) According to perldoc, shift without an ARRAY specified, carries out shift on the "@_" array. According to what I've read, @_ is a special array that lists arguments supplied to a sub routine. There are no sub routines in this perl script. I'm confused regarding that.
...
But I do not see 'shift' in the script either. So, what's the problem ? I.e. why are you asking about 'shift' ?
 
Old 08-02-2010, 09:30 PM   #6
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Quote:
chomp($var = shift);
That line. Cheers for the help.
 
Old 08-02-2010, 09:34 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
That line. Cheers for the help.
In my case 'perldoc -f shift' produces 8 lines of text; the answer to your question is in line #4.
 
0 members found this post helpful.
Old 08-02-2010, 09:38 PM   #8
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Sergei Steshenko View Post
In my case 'perldoc -f shift' produces 8 lines of text; the answer to your question is in line #4.
Yeah I read that, see my previous post:

Quote:
According to perldoc, shift without an ARRAY specified, carries out shift on the "@_" array. According to what I've read, @_ is a special array that lists arguments supplied to a sub routine. There are no sub routines in this perl script. I'm confused regarding that.
Still confused. Don't worry about it as I sense a condescending tone. This isn't a priority, was just merely asking for some help.

Last edited by vxc69; 08-02-2010 at 09:41 PM.
 
Old 08-02-2010, 09:45 PM   #9
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
Yeah I read that, see my previous post:



Still confused. Don't worry about it as I sense a condescending tone. This isn't a priority, was just merely asking for some help.
Please copy-paste contents of 'perldoc -f shift' with line numbers for reference. For line numbers press '-N' without the quotes while in 'perldoc -f shift' window.

Or copy-paste the http://perldoc.perl.org/functions/shift.html page.
 
Old 08-02-2010, 09:47 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by vxc69 View Post
That line. Cheers for the help.
if you read perldoc -f shift, you come across this
Quote:
If ARRAY is omitted, shifts the @_ array within the lexical scope of subroutines and formats, and the @ARGV array outside of a subroutine
chomp($var=shift) occurs right after the script is executed. in that case, it is "shifting" the @ARGV array (ie, the arguments passed to the script when executed )
 
1 members found this post helpful.
Old 08-02-2010, 09:52 PM   #11
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by Sergei Steshenko View Post
Please copy-paste contents of 'perldoc -f shift' with line numbers for reference. For line numbers press '-N' without the quotes while in 'perldoc -f shift' window.

Or copy-paste the http://perldoc.perl.org/functions/shift.html page.
Code:
 0 shift ARRAY
 1 shift   Shifts the first value of the array off and returns it,
 2 shortening the array by 1 and moving everything down.  If there 
 3 are no elements in the array, returns the undefined value. If
 4 ARRAY is omitted, shifts the @_ array within the lexical scope
 5 of subroutines and formats, and the @ARGV array outside of a
 6 subroutine and also within the lexical scopes established by
 7 the "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}",
 8 "UNITCHECK {}" and "END {}" constructs.
 9
10 See also "unshift", "push", and "pop".  "shift" and "unshift"
11 do the same thing to the left end of an array that "pop" and
12 "push" do to the right end.
 
Old 08-02-2010, 09:52 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
if you read perldoc -f shift, you come across this


chomp($var=shift) occurs right after the script is executed. in that case, it is "shifting" the @ARGV array (ie, the arguments passed to the script when executed )
To generalize: in English end of sentence is denoted by '.' (ASCII 056 46 2E), so sentences need to be read to the very end.

But that's why I asked to copy-paste the output of 'perldoc -f shift' - to verify that the sentence in its entirety in the OP's case is the same as in my case. I.e. I wanted evidence to support or disprove my suspicion that the sentence in question hasn't been read to the very end.
 
0 members found this post helpful.
Old 08-02-2010, 09:54 PM   #13
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
Code:
 0 shift ARRAY
 1 shift   Shifts the first value of the array off and returns it,
 2 shortening the array by 1 and moving everything down.  If there 
 3 are no elements in the array, returns the undefined value. If
 4 ARRAY is omitted, shifts the @_ array within the lexical scope
 5 of subroutines and formats, and the @ARGV array outside of a
 6 subroutine and also within the lexical scopes established by
 7 the "eval STRING", "BEGIN {}", "INIT {}", "CHECK {}",
 8 "UNITCHECK {}" and "END {}" constructs.
 9
10 See also "unshift", "push", and "pop".  "shift" and "unshift"
11 do the same thing to the left end of an array that "pop" and
12 "push" do to the right end.
The item in red is the answer.
 
0 members found this post helpful.
Old 08-02-2010, 09:57 PM   #14
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by ghostdog74 View Post
if you read perldoc -f shift, you come across this


chomp($var=shift) occurs right after the script is executed. in that case, it is "shifting" the @ARGV array (ie, the arguments passed to the script when executed )
Ah, I see. I know nothing about perl. Didn't know "outside a subroutine" implied arguments passed to the script.

Thanks for clearing that up.

The script gets passed the following:
Code:
webalizerrun.pl /var/turbpanel >/dev/null 2>&1

Last edited by vxc69; 08-02-2010 at 09:58 PM.
 
Old 08-02-2010, 10:08 PM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vxc69 View Post
Ah, I see. I know nothing about perl. Didn't know "outside a subroutine" implied arguments passed to the script.

Thanks for clearing that up.

The script gets passed the following:
Code:
webalizerrun.pl /var/turbpanel >/dev/null 2>&1
"Outside the subroutine" is self-explanatory. "arguments passed to the script" is namely @ARGV which is explicitly mentioned in 'perldoc -f shift'. @ARGV itself is described in 'perldoc perlvar'.

"I know nothing about perl" translates into visiting http://perldoc.perl.org/ and reading (see the left column):

http://perldoc.perl.org/index-overview.html
http://perldoc.perl.org/perlintro.html
http://perldoc.perl.org/perlop.html
http://perldoc.perl.org/perlvar.html
;

also 'perldoc perlsyn'.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I cannot understand this perl script mq15 Linux - Server 3 10-12-2009 12:44 AM
Perl Script..Difficult to understand ?? ajeetraina Linux - General 2 02-20-2008 11:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration