LinuxQuestions.org
Help answer threads with 0 replies.
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 10-16-2007, 11:03 AM   #1
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Rep: Reputation: 30
Perl Multiline Variables and inline PHP


Hi all

I am trying to write a script in Perl that would generate a PHP site that is actually not that complicated. My only problem is that I do not have the option to operate with 2 files so I need to have everything in 1 Perl file. I need to have the PHP file inside the Perl file in some form of variable or something like that. Is it possible to put a php code like that into a var in perl?
PHP Code:
<? 
$google 
"Hello World";
print 
$google;
etc...
?>
Thx...
 
Old 10-16-2007, 01:07 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
What you could do is have a single output variable, and constantly concatenate to it depending on the flow control:
Code:
...
my $var = '<font size="2">';
my $bold = '<b>';
my $italic = '<i>';

$var .= $bold if ($style == 'bold');
$var .= $italic if ($option == 'italic');

# Close the open tags...
$var .= '</i></b></font>';
...
The one problem you will almost certainly run into with this, is that you may not always know which tags are open or closed. You will probably have to incorporate a function to scan through a line (before it's printed) and ordinally close any open tags (ie. <i> was the last opened tag, so close it first) by simply taking the tag opening string, and inserting a forward slash "/" after the opening "<". The sub-problem that arises from this is tags with extended information - "<font size=2 color=...>" - and having to deal with taking out everything in between "<tagname" and the closing ">", but that could easily be taken care of with a regular expression.

 
Old 10-16-2007, 02:09 PM   #3
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
I went through the regular expressions again and realized that there are differences between the ' and " signs. When I tried this:
Code:
$var = "<?
print 'Hello';
$ccol = sad;
?>";
i bumped into an error. Then I replaced the " to ' and the ' to \' that way it worked without the use strict part of the code. I got only a warning. Why isn't it working with the use strict; stuff too? What is wrong with it?
 
Old 10-17-2007, 05:52 AM   #4
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
I believe that the solutions to my problem is the following:
Code:
$var = <<EOF;
<?
    whatever php code comes here
?>
EOF
print $var;
 
Old 10-17-2007, 06:59 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
See also the qw operator: http://perldoc.perl.org/perlop.html#...Like-Operators
 
Old 10-17-2007, 08:05 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
also, quite neat and readable is:

Code:
#!/usr/bin/perl

print while <DATA>;


__DATA__
put your
php 
code in
here
 
Old 10-17-2007, 08:20 AM   #7
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by bigearsbilly View Post
also, quite neat and readable is:

Code:
#!/usr/bin/perl

print while <DATA>;


__DATA__
put your
php 
code in
here
It is readable, however there are one thing that I do not get. What are the __ signs and how shall I interpret it? Why do u refer to the DATA as <DATA>.
Also I found loops like while(<>) that is in infinite loop similar to for(;. What is the background of it? I am no perl programmer (yet) but I am eager to learn this language but I cant go to the next step as long as I bump into such things.
It turned out that I have got more than one problems, heh?
 
Old 10-17-2007, 08:37 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
__DATA__

is a tag, which means all after this is data not perl script
this DATA is accessible using the DATA filehandle.

so it's exactly like reading a separate file but it's embedded in your script.
 
Old 10-17-2007, 09:00 AM   #9
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Ahh... that is cool... Probably it helped a lot.
Do peeps usually encode the __DATA__ tag? Like Base64 or something like that?
 
Old 10-17-2007, 09:24 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
er, hmmm, er,

I guess you could do what you like, never tried not knowing nothing about Base64 encoding.

For example,
I was playing about, bored and made a google querier,
which substitutes the command line query into the DATA section...

(note this is at work, so using a proxy server)

Code:
#!/usr/bin/perl
use IO::Socket;

@ARGV or die "search for?\n";
my $proxy = "xna4.intra.yy.com:8080";
my $port = "8080";

$sock = IO::Socket::INET->new(   PeerAddr => $proxy,
                                 PeerPort => 'http(80)',
                                 Proto    => 'tcp');

$" = "+"; # put '+' instead of spaces
$query = "@ARGV";

while (<DATA>) {
    s/SEARCH_PATTERN/$query/;
    $sock->send($_);
}


while($sock->read($data, 1024)) {
     print $data;
}



__DATA__
GET http://www.google.co.uk/search?q=SEARCH_PATTERN HTTP/1.0
Proxy-Connection: Keep-Alive
User-Agent: Billy-Browser
Host: www.google.co.uk
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
 
Old 10-17-2007, 09:30 AM   #11
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Yup... that __DATA__ thing is the one that works that best. The file is now ready to be used. Low quality code but it does what it is supposed to do. thanx guys for the help
 
Old 10-17-2007, 09:39 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I use it quite a lot, it's also very good for example data
for filter programs,
so when 6 months later you go back and say "what the hell does this do?"
you'll have an example to run.
 
Old 10-17-2007, 10:34 AM   #13
vargadanis
Member
 
Registered: Sep 2006
Posts: 248

Original Poster
Rep: Reputation: 30
Maybe, I would post, the entire code here so that u can comment it and say what are the thing that u would do in a different way, and why is ur solution better. U think u could do this for me?
 
Old 10-18-2007, 02:04 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I could try.
mind you I don't know php
 
  


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
Are PHP session variables held in memory when you leave a PHP site? Locura Programming 11 11-16-2008 08:37 PM
Adding users with PHP (pass php variables to Expect script) Jayla Programming 1 10-20-2006 10:44 AM
Perl variables nbp Programming 2 11-02-2003 09:33 AM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM
Perl print Multiline help! CragStar Programming 1 02-06-2002 08:48 AM

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

All times are GMT -5. The time now is 06:52 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