473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Perl noob tries to translate a scipt to PHP - need some help

Hello, I have found a perl script and need to "translate" this to PHP. I try to do it step by step and the first part of it is this function (the whole script is found at http://nonsense.sourceforge.net/):

Expand|Select|Wrap|Line Numbers
  1. ### Load and parse a datafile, slurping the contents into the %pool hash
  2. sub LoadDataFile { 
  3.    my $file = shift;
  4.    $file = SafeFile( $file ) if $cgi_mode;
  5.    open IN, $file or die "Error opening $file... $!\n";
  6.    local $/ = '';
  7.  
  8.    SECTION: while( <IN> ) { 
  9.       my( @temp ) = split /\n/, $_;
  10.       my $key = shift @temp;
  11.       $pool{$key} = [ @temp ];
  12.    }
  13.    close IN;
  14. }
$_ seems to hold the actual lines one after another during the while loop.

Expand|Select|Wrap|Line Numbers
  1. my( @temp ) = split /\n/, $_;
Why is $_ splitted by \n here? $_ should only contain one line from the input file already, right?

Expand|Select|Wrap|Line Numbers
  1. my $key = shift @temp;
Okay, $key is now the current element of the @temp array. But since @temp should only contain the current line from the input file, $key should be equal to the current line from the input file. (?!)

Expand|Select|Wrap|Line Numbers
  1. $pool{$key} = [ @temp ];
This one I don't understand at all. Which value is given the elemt of $pool with the key $key here? What does an array like @temp in [] mean? I thought [] normally contains the key for an element of an array like $pool[$key]...

What would helpme is a way to look at the contents of $pool after the while loop is done, but I don't know how to do that in perl. print $pool; doesn't work.

While this function works fine in the whole scipt, I tried to put it in a separate file to examine how it works. So my whole file would be

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use CGI;
  5. my $q = new CGI;
  6. open(INFILE,  "test.data")   or die "Can't open input.txt: $!";
  7.  
  8. # print header and start the markup output
  9.  
  10. print $q->header( "text/html" ),$q->start_html( "hello from perl cgi!" );
  11.  
  12. while (<INFILE>) {     # assigns each line in turn to $_ 
  13.     print $q->p("Just read in this line: $_");
  14.     my( @temp ) = split /\n/, $_;
  15.     print $q->strong("temp = @temp<br />");
  16.     my $key = shift @temp;
  17.     print $q->strong("key = $key<br />");
  18.     #$pool{$key} = [ @temp ];
  19. }
  20. print $q->end_html;
But as soon as I uncomment the line with $pool, I get an Internal Server Error. Why is this not working in a standalone script?

I hope anybody roughly understands what I mean and what I'd like to know. I know my english is bad and so it's very hard to write about a script language that I don't understand either ;-)
Oct 24 '07 #1
6 1975
eWish
971 Expert 512MB
Welcome to TSDN!

Add the following lines to your script.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use CGI::Carp qw/fatalsToBrowser/; #For Debugging Only
One error you will get is caused by the fact that you have not delcared the hash $pool{$key} you are trying to use.
Expand|Select|Wrap|Line Numbers
  1. my %pool;
This page is a great resource when you get error like this. Helpful in tracking down some of the standard problems.
Oct 24 '07 #2
numberwhun
3,509 Expert Mod 2GB
Welcome to TSDN!

Add the following lines to your script.
Expand|Select|Wrap|Line Numbers
  1. use warnings;
  2. use CGI::Carp qw/fatalsToBrowser/; #For Debugging Only
One error you will get is caused by the fact that you have not delcared the hash $pool{$key} you are trying to use.
Expand|Select|Wrap|Line Numbers
  1. my %pool;
This page is a great resource when you get error like this. Helpful in tracking down some of the standard problems.
He he he, isn't PHP right up your alley?

Regards,

Jeff
Oct 24 '07 #3
eWish
971 Expert 512MB
He he he, isn't PHP right up your alley?

Regards,

Jeff
Yes...that is why I handle all of the PHP related questions.
Oct 24 '07 #4
Thank you, that helped.

In php I can output the elements of an array with the print_r function. Is there an equivalent in perl to do this? I'd like to output the elements of $pool{$key}, but if I use print I only get something like that:

@pool{$key} = ARRAY(0x822dae4)

What part of the @temp arry is
Expand|Select|Wrap|Line Numbers
  1. $pool{$key} = [ @temp ];
assigning to $pool{$key}? I wonder what an array in these braces [ ] mean...
Oct 25 '07 #5
eWish
971 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. my @array = (qw(Hello Every One));
  2. print @array; # print entire array
  3. print $array[0]; #print the first element of the array
  4.  
  5.  
  6. $pool{$key} # is a hash element.
Oct 25 '07 #6
numberwhun
3,509 Expert Mod 2GB
Thank you, that helped.

In php I can output the elements of an array with the print_r function. Is there an equivalent in perl to do this? I'd like to output the elements of $pool{$key}, but if I use print I only get something like that:

@pool{$key} = ARRAY(0x822dae4)

What part of the @temp arry is
Expand|Select|Wrap|Line Numbers
  1. $pool{$key} = [ @temp ];
assigning to $pool{$key}? I wonder what an array in these braces [ ] mean...
When accessing an array in Perl, the array is called @array, but when accessing a specific element, it is accessed like "$array[0]", where the element number, starting at zero, is in square brackets.

The curly braces { and } are used when accessing hashes, not arrays

I suggest you find a good book that will show you the basics and go from there.

Regards,

Jeff
Oct 25 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Piotr Turkowski | last post by:
Hi! I've got some code in Perl and I have to have it in C, but my knowlege of Perl is < 0 :-(, so I need your help. here's the code. Thanks in advance. decrypt.pl #!/usr/local/bin/perl...
1
by: sm00thcrimnl13 | last post by:
if i have windows 2000 and know how to write perl scripts, how to i actuvate the script through perl?
3
by: FLOTServer | last post by:
Here's my problem: I run a gameserver that runs the game "Medal of Honor". On the game server is log file which contains all of the data from the players for that day (kills, deaths, etc...). I...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
3
by: sir.linying | last post by:
My php script is to call perl scipt which makes use of Spreadsheet::ParseExcel module to parse Excel file. I am able to launch php script from command line so that perl script can run and properly...
0
by: gedsta | last post by:
Hi all I am a total noob and its my 2nd post, first of all thank you for letting me use these forums as a source of information, and hopefully once i get my head round perl i maybe able to help...
4
by: Ignoramus6539 | last post by:
There were some strange requests to my server asking for config.php file (which I do not have in the requested location). I did some investigation. Seems to be a virus written in perl,...
1
by: Ken Browning | last post by:
I have been working with Perl for a while now, but have not used it with XML - I am an XML noob. I want to be able to read an XML schema file and initialize an instance of the data item...
2
saranjegan
by: saranjegan | last post by:
Hello, Am in need to access a serial port from perl script, pastly am used C related CGI scripts am just shifted to perl , i need to know about its support with serial port so i can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.