[SOLVED] Help me understand this Perl script please. It's not homework.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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.
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
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.
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.
...
But I do not see 'shift' in the script either. So, what's the problem ? I.e. why are you asking about 'shift' ?
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.
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.
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 )
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.
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.
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 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.
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.
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):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.